feat: 接入fastapi
This commit is contained in:
+38
@@ -0,0 +1,38 @@
|
||||
"""FastAPI 应用入口"""
|
||||
|
||||
import asyncio
|
||||
from contextlib import asynccontextmanager
|
||||
|
||||
from fastapi import FastAPI
|
||||
|
||||
from app.api.agent import router
|
||||
from app.core.exceptions import register_exception_handlers
|
||||
|
||||
|
||||
@asynccontextmanager
|
||||
async def lifespan(application: FastAPI):
|
||||
from app.clients.decision import close_decision_engine
|
||||
from app.observability.tracing import flush_tracing
|
||||
from app.processor.graph import build_graph
|
||||
from app.services.agent import AgentService
|
||||
|
||||
application.state.agent_service = AgentService(build_graph())
|
||||
try:
|
||||
yield
|
||||
finally:
|
||||
# 共享客户端只在应用退出时关闭,追踪刷新放到线程避免阻塞事件循环
|
||||
try:
|
||||
await close_decision_engine()
|
||||
finally:
|
||||
await asyncio.to_thread(flush_tracing)
|
||||
|
||||
|
||||
def create_app() -> FastAPI:
|
||||
application = FastAPI(title="Screen Agent API", lifespan=lifespan)
|
||||
# 统一处理请求校验、HTTP 错误和未捕获异常
|
||||
register_exception_handlers(application)
|
||||
application.include_router(router)
|
||||
return application
|
||||
|
||||
|
||||
app = create_app()
|
||||
Reference in New Issue
Block a user