添加数据库连接

This commit is contained in:
ChengCan
2026-03-26 10:26:25 +08:00
parent d5c397b68c
commit f9ca4a32a3
3 changed files with 58 additions and 0 deletions

View File

@@ -4,10 +4,12 @@ from fastapi import FastAPI
try:
# For module mode: `uvicorn app.main:app`
from app.db import check_db_connection, close_db_engine
from app.routers.health import router as health_router
from app.settings import settings
except ModuleNotFoundError:
# For script mode: `python app/main.py` or VS Code "Run Python File"
from db import check_db_connection, close_db_engine
from routers.health import router as health_router
from settings import settings
@@ -18,6 +20,15 @@ def create_app() -> FastAPI:
version=settings.app_version,
description=settings.app_description,
)
@app.on_event("startup")
def on_startup() -> None:
check_db_connection()
@app.on_event("shutdown")
def on_shutdown() -> None:
close_db_engine()
app.include_router(health_router)
return app