2026-09-08 19:17:35 +08:00
|
|
|
"""应用入口:四库异步生命周期 + 中间件 + 全局异常 + 路由装配。"""
|
|
|
|
|
from contextlib import asynccontextmanager
|
2026-09-12 17:48:17 +08:00
|
|
|
import asyncio
|
2026-09-08 19:17:35 +08:00
|
|
|
|
2026-09-10 23:21:30 +08:00
|
|
|
import uvicorn
|
2026-09-08 19:17:35 +08:00
|
|
|
from fastapi import FastAPI
|
|
|
|
|
|
|
|
|
|
from api.router import api_router
|
|
|
|
|
from config import database
|
2026-09-11 17:31:16 +08:00
|
|
|
from rag.milvus_collections import ensure_collections
|
2026-09-11 10:47:01 +08:00
|
|
|
from service.customer_agent.bootstrap import (
|
|
|
|
|
build_default_knowledge_upload_service,
|
|
|
|
|
build_default_runtime,
|
|
|
|
|
)
|
2026-09-11 22:38:15 +08:00
|
|
|
from service.client_agent.bootstrap import build_default_runtime as build_client_runtime
|
2026-09-12 17:48:17 +08:00
|
|
|
from service.client_agent.idle_archive_worker import IdleArchiveWorker
|
2026-09-08 19:17:35 +08:00
|
|
|
from utils.exceptions import register_exception_handlers
|
|
|
|
|
from utils.logger import setup_logging
|
|
|
|
|
from utils.request_id import RequestIdMiddleware
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@asynccontextmanager
|
|
|
|
|
async def lifespan(app: FastAPI):
|
2026-09-11 17:31:16 +08:00
|
|
|
setup_logging()
|
|
|
|
|
await ensure_collections()
|
|
|
|
|
app.state.customer_agent_runtime = build_default_runtime()
|
2026-09-11 22:38:15 +08:00
|
|
|
app.state.client_agent_runtime = build_client_runtime()
|
2026-09-12 17:48:17 +08:00
|
|
|
app.state.client_agent_archive_worker = IdleArchiveWorker(
|
|
|
|
|
redis=app.state.client_agent_runtime.redis,
|
|
|
|
|
memory_service=app.state.client_agent_runtime.memory_service,
|
|
|
|
|
)
|
|
|
|
|
await app.state.client_agent_archive_worker.recover_due_index()
|
|
|
|
|
app.state.client_agent_archive_task = asyncio.create_task(
|
|
|
|
|
app.state.client_agent_archive_worker.run()
|
|
|
|
|
)
|
2026-09-11 17:31:16 +08:00
|
|
|
app.state.knowledge_upload_service = build_default_knowledge_upload_service()
|
2026-09-08 19:17:35 +08:00
|
|
|
yield
|
2026-09-12 17:48:17 +08:00
|
|
|
await app.state.client_agent_archive_worker.stop()
|
|
|
|
|
await app.state.client_agent_archive_task
|
2026-09-11 17:31:16 +08:00
|
|
|
await database.dispose()
|
2026-09-08 19:17:35 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
app = FastAPI(title="智能公募基金系统", version="0.1.0", lifespan=lifespan)
|
|
|
|
|
|
|
|
|
|
app.add_middleware(RequestIdMiddleware)
|
|
|
|
|
register_exception_handlers(app)
|
|
|
|
|
app.include_router(api_router)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.get("/")
|
|
|
|
|
async def root():
|
2026-09-11 22:38:15 +08:00
|
|
|
return {"message": "智能公募基金系统 API", "docs": "/docs"}
|
2026-09-12 17:48:17 +08:00
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
uvicorn.run(app, host="127.0.0.1", port=8000)
|