This commit is contained in:
2025-11-05 16:21:05 +08:00
commit 00967a138d
24 changed files with 1930 additions and 0 deletions

0
Application/__init__.py Normal file
View File

62
Application/app.py Normal file
View File

@@ -0,0 +1,62 @@
from ..Convention.Runtime.GlobalConfig import ProjectConfig, ConsoleFrontColor
from ..CoreModules.flags import set_internal_verbose
from .web import app
from argparse import ArgumentParser
from typing import *
import sys
import uvicorn
def main() -> int:
config = ProjectConfig()
parser = ArgumentParser()
parser.add_argument("--main-webhook-url", type=str, default=config.FindItem("main_webhook_url", ""))
parser.add_argument("--host", type=str, default=config.FindItem("host", "0.0.0.0"))
parser.add_argument("--port", type=int, default=config.FindItem("port", 8000))
parser.add_argument("--verbose", type=bool, default=config.FindItem("verbose", False))
args = parser.parse_args()
config.SaveProperties()
if "help" in args:
parser.print_help()
return 0
# region Main Webhook URL
webhook_url = args.main_webhook_url
if not webhook_url or webhook_url == "":
config.Log("Fatal", f"{ConsoleFrontColor.RED}Main webhook URL is not set{ConsoleFrontColor.RESET}")
return 1
config.Log("Info", f"{ConsoleFrontColor.GREEN}Main webhook URL: {webhook_url}{ConsoleFrontColor.RESET}")
# endregion Main Webhook URL
# region Verbose
verbose = args.verbose
set_internal_verbose(verbose)
config.Log("Info", f"{ConsoleFrontColor.GREEN}Verbose: {verbose}{ConsoleFrontColor.RESET}")
# endregion Verbose
# region Server
host = args.host
port = args.port
config.Log("Info", f"{ConsoleFrontColor.GREEN}Server: {host}:{port}{ConsoleFrontColor.RESET}")
# endregion Server
uvicorn.run(app, host=host, port=port,
limit_concurrency=5,
log_level="info")
return 0
if __name__ == "__main__":
sys.exit(main())
__all__ = ["main"]

80
Application/web.py Normal file
View 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"]