Files
PWF/Application/web.py
2025-11-08 01:24:50 +08:00

93 lines
2.7 KiB
Python

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 ..CoreModules.clock_scheduler import get_clock_scheduler
from ..CoreRouters import callback, health
from ..Convention.Runtime.GlobalConfig import *
from ..Convention.Runtime.Architecture import Architecture
logger: ProjectConfig = Architecture.Get(ProjectConfig)
APP_CONFIG = {
"docs_url": "/docs",
}
@asynccontextmanager
async def lifespan(app: FastAPI):
"""应用生命周期管理"""
scheduler = get_clock_scheduler()
await scheduler.start()
# 启动
logger.Log("Info", "应用启动中...")
# 初始化数据
logger.Log("Info", "初始化数据...")
# 启动后台清理
logger.Log("Info", "启动后台清理...")
try:
yield
finally:
# 关闭
try:
logger.Log("Info", "关闭应用...")
await scheduler.stop()
except asyncio.CancelledError:
pass
finally:
logger.Log("Info", "关闭应用完成...")
def generate_app(kwargs: dict) -> FastAPI:
'''
生成FastAPI应用
'''
config: ProjectConfig = Architecture.Get(ProjectConfig)
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(logger.FindItem("app_config", {}))
@app.get("/")
async def root():
"""根路径"""
config: ProjectConfig = Architecture.Get(ProjectConfig)
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):
"""全局异常处理"""
logger.Log("Error", f"未捕获的异常: {exc}\n{format_traceback_info()}")
return JSONResponse(
status_code=500,
content={"error": "Internal Server Error", "detail": str(exc)}
)
logger.SaveProperties()
# 除了从本模块导出的app使用API实例外, 还可以从Architecture.Get(FastAPI)获取
__all__ = ["app"]