Files
banban/mini-program/app/main.py

73 lines
2.2 KiB
Python

import os
import logging
import sys
from pathlib import Path
from fastapi import FastAPI
if __package__ in (None, ""):
# Make `python app/main.py` behave like module execution from the project root.
project_root = Path(__file__).resolve().parent.parent
project_root_str = str(project_root)
if project_root_str not in sys.path:
sys.path.insert(0, project_root_str)
from app.db import check_db_connection, close_db_engine, init_db_tables
from app.logging_setup import configure_logging
from app.middleware.auth import install_auth_middleware
from app.middleware.request_log import install_request_logging_middleware
from app.routers.auth import router as auth_router
from app.routers.wechat_auth import router as wechat_auth_router
from app.routers.health import router as health_router
from app.routers.messages import router as messages_router
from app.routers.parents import router as parents_router
from app.routers.children import router as children_router
from app.routers.bindings import router as bindings_router
from app.settings import settings
configure_logging()
logger = logging.getLogger("app.main")
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()
init_db_tables()
logger.info("application started", extra={"event": "app_start"})
@app.on_event("shutdown")
def on_shutdown() -> None:
logger.info("application shutting down", extra={"event": "app_shutdown"})
close_db_engine()
install_auth_middleware(app)
install_request_logging_middleware(app)
app.include_router(wechat_auth_router)
app.include_router(health_router)
app.include_router(messages_router)
app.include_router(parents_router)
app.include_router(children_router)
app.include_router(bindings_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))),
)