50 lines
1.4 KiB
Python
50 lines
1.4 KiB
Python
from fastapi import FastAPI
|
|
from contextlib import asynccontextmanager
|
|
from app.router import router
|
|
from app.utils.response_wrapper import standard_response
|
|
from app.exception_handlers import (
|
|
validation_exception_handler,
|
|
http_exception_handler,
|
|
general_exception_handler
|
|
)
|
|
from fastapi.exceptions import RequestValidationError
|
|
from starlette.exceptions import HTTPException as StarletteHTTPException
|
|
|
|
@asynccontextmanager
|
|
async def lifespan(app: FastAPI):
|
|
# 启动时运行的代码
|
|
banner = r"""
|
|
🚀 Vector Search API is up and running!
|
|
📚 Docs: http://localhost:8000/docs
|
|
🔍 OpenAPI: http://localhost:8000/openapi.json
|
|
📖 ReDoc: http://localhost:8000/redoc
|
|
"""
|
|
print(banner)
|
|
|
|
yield # 👈 这里控制 startup 和 shutdown 之间的生命周期
|
|
|
|
# (可选)关闭时运行的代码
|
|
# print("Shutting down Vector Search API...")
|
|
|
|
app = FastAPI(
|
|
title="Vector Search API",
|
|
version="1.0.0",
|
|
docs_url="/docs",
|
|
redoc_url="/redoc",
|
|
openapi_url="/openapi.json",
|
|
lifespan=lifespan
|
|
)
|
|
|
|
# 注册异常处理器
|
|
app.add_exception_handler(RequestValidationError, validation_exception_handler)
|
|
app.add_exception_handler(StarletteHTTPException, http_exception_handler)
|
|
app.add_exception_handler(Exception, general_exception_handler)
|
|
|
|
# 注册路由
|
|
app.include_router(router)
|
|
|
|
@app.get("/")
|
|
@standard_response
|
|
def root():
|
|
return {"message": "Vector Search API is running"}
|