from fastapi import FastAPI import asyncio from fastapi.responses import JSONResponse from contextlib import asynccontextmanager from ..CoreModules.middleware import ConcurrencyLimitMiddleware from ..CoreModules.plugin_interface import ImportPlugins from ..CoreRouters import callback, health from ..Convention.Runtime.GlobalConfig import * from ..Convention.Runtime.Architecture import Architecture config = ProjectConfig() APP_CONFIG = { "docs_url": "/docs", } @asynccontextmanager async def lifespan(app: FastAPI): """应用生命周期管理""" # 启动 config.Log("Info", "应用启动中...") # 初始化数据 config.Log("Info", "初始化数据...") # 启动后台清理 config.Log("Info", "启动后台清理...") yield # 关闭 try: config.Log("Info", "关闭应用...") # await cleanup_task except asyncio.CancelledError: pass finally: config.Log("Info", "关闭应用完成...") # db.close() def generate_app(kwargs: dict) -> FastAPI: ''' 生成FastAPI应用 ''' kwargs.update(**APP_CONFIG) app = FastAPI(**kwargs, lifespan=lifespan) # 添加并发限制中间件 app.add_middleware(ConcurrencyLimitMiddleware) # 注册路由 app.include_router(callback.router, prefix="/api", tags=["callback"]) app.include_router(health.router, tags=["health"]) ImportPlugins(app, config.FindItem("plugin_dir", "Plugins")) # 注册至框架中 Architecture.Register(FastAPI, app, lambda: None) config.SaveProperties() return app app: FastAPI = generate_app(config.FindItem("app_config", {})) @app.get("/") async def root(): """根路径""" app_name = config.FindItem("app_name", "Application") app_version = config.FindItem("app_version", "0.0.0") config.SaveProperties() return JSONResponse({ "message": app_name, "version": app_version, "status": "running" }) @app.exception_handler(Exception) async def global_exception_handler(request, exc): """全局异常处理""" config.Log("Error", f"未捕获的异常: {exc}", exc_info=True) return JSONResponse( status_code=500, content={"error": "Internal Server Error", "detail": str(exc)} ) # 除了从本模块导出的app使用API实例外, 还可以从Architecture.Get(FastAPI)获取 __all__ = ["app"]