diff --git a/app/__pycache__/main.cpython-310.pyc b/app/__pycache__/main.cpython-310.pyc index 18c4917..b28e0ea 100644 Binary files a/app/__pycache__/main.cpython-310.pyc and b/app/__pycache__/main.cpython-310.pyc differ diff --git a/app/main.py b/app/main.py index 39810ea..48adc8f 100644 --- a/app/main.py +++ b/app/main.py @@ -1,4 +1,5 @@ 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 ( @@ -9,18 +10,37 @@ from app.exception_handlers import ( 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" + 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("/") diff --git a/run.py b/run.py index 4753965..c58c9b3 100644 --- a/run.py +++ b/run.py @@ -1,4 +1,4 @@ import uvicorn if __name__ == "__main__": - uvicorn.run("app.main:app", host="127.0.0.1", port=8000, reload=True) + uvicorn.run("app.main:app", host="127.0.0.1", port=8000, reload=True, log_level="warning")