Files
banban/mini-program/app/main.py
2026-03-26 10:26:25 +08:00

47 lines
1.1 KiB
Python

import os
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
def create_app() -> FastAPI:
app = FastAPI(
title=settings.app_name,
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
app = create_app()
if __name__ == "__main__":
import uvicorn
uvicorn.run(
app,
host=os.getenv("HOST", settings.host),
port=int(os.getenv("PORT", str(settings.port))),
)