Init
This commit is contained in:
80
Application/web.py
Normal file
80
Application/web.py
Normal file
@@ -0,0 +1,80 @@
|
||||
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, private
|
||||
from ..Convention.Runtime.GlobalConfig import *
|
||||
from ..Convention.Runtime.Architecture import Architecture
|
||||
|
||||
config = ProjectConfig()
|
||||
|
||||
@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(APP_CONFIG: dict) -> FastAPI:
|
||||
'''
|
||||
生成FastAPI应用
|
||||
'''
|
||||
app = FastAPI(**APP_CONFIG, lifespan=lifespan)
|
||||
|
||||
# 添加并发限制中间件
|
||||
app.add_middleware(ConcurrencyLimitMiddleware)
|
||||
|
||||
# 注册路由
|
||||
app.include_router(callback.router, prefix="/api", tags=["callback"])
|
||||
app.include_router(health.router, tags=["health"])
|
||||
app.include_router(private.router, prefix="/api", tags=["private"])
|
||||
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():
|
||||
"""根路径"""
|
||||
return JSONResponse({
|
||||
"message": config.FindItem("app_name", "Application"),
|
||||
"version": config.FindItem("app_version", "0.0.0"),
|
||||
"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"]
|
||||
Reference in New Issue
Block a user