Files
AIRegulation-DocAnalysis/backend/app/api/main.py
T

146 lines
4.4 KiB
Python
Raw Normal View History

2026-05-14 15:07:34 +08:00
"""FastAPI application entrypoint."""
from contextlib import AsyncExitStack, asynccontextmanager
2026-05-14 15:07:34 +08:00
from fastapi import FastAPI, Request
from fastapi.encoders import jsonable_encoder
2026-05-14 15:07:34 +08:00
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse
from loguru import logger
2026-06-05 18:00:31 +08:00
from app.api.middleware.audit import AuditMiddleware
2026-05-14 15:07:34 +08:00
from app.api.models import ErrorResponse
from app.api.routes import api_router
from app.config.logging import setup_logging
from app.config.settings import settings
from app.mcp.server import build_mcp_asgi_app
from app.shared.bootstrap import cleanup_runtime_dependencies, preload_runtime_dependencies
from app.shared.errors import VectorStoreSchemaError
# Keep module behavior explicit so the backend flow stays easy to audit.
2026-05-14 15:07:34 +08:00
setup_logging(level="INFO" if not settings.debug else "DEBUG")
# Built once at module scope so both lifespan() and app.mount() below reference
# the same instance — mounting a second, separately-built instance would start
# a second, unrelated MCP session manager.
mcp_app = build_mcp_asgi_app()
2026-05-14 15:07:34 +08:00
@asynccontextmanager
async def lifespan(app: FastAPI):
"""Application lifecycle hooks."""
# FastMCP-style servers own a session manager that only starts via its own
# lifespan context. app.mount() does NOT propagate nested ASGI lifespans
# automatically (confirmed Starlette/ASGI limitation) — without this,
# every search_regulations call would fail because the MCP session
# manager was never started.
async with AsyncExitStack() as stack:
await stack.enter_async_context(mcp_app.router.lifespan_context(mcp_app))
2026-05-14 15:07:34 +08:00
logger.info(f"启动 {settings.app_name} v{settings.app_version}")
logger.info(f"调试模式: {settings.debug}")
logger.info("预加载LLM客户端...")
preload_runtime_dependencies()
2026-05-14 15:07:34 +08:00
yield
logger.info("应用关闭,执行清理...")
cleanup_runtime_dependencies()
2026-05-14 15:07:34 +08:00
app = FastAPI(
title=settings.app_name,
description=(
"AI+合规智能中枢 - 法律法规文档解析入库功能\n\n"
"支持PDF/DOCX文档解析、智能分块、向量嵌入、Milvus存储"
),
version=settings.app_version,
lifespan=lifespan,
docs_url="/docs",
redoc_url="/redoc",
)
2026-06-05 18:00:31 +08:00
# Tighten CORS — only allow configured origins.
# Set CORS_ALLOW_ORIGINS in .env to the real frontend URL in production.
_ORIGINS = [o.strip() for o in settings.cors_allow_origins.split(",") if o.strip()]
if not _ORIGINS:
_ORIGINS = ["http://localhost:5173"]
2026-05-14 15:07:34 +08:00
app.add_middleware(
CORSMiddleware,
2026-06-05 18:00:31 +08:00
allow_origins=_ORIGINS,
2026-05-14 15:07:34 +08:00
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
2026-06-05 18:00:31 +08:00
# Audit middleware logs every authenticated API call for compliance traceability.
app.add_middleware(AuditMiddleware)
2026-05-14 15:07:34 +08:00
app.include_router(api_router, prefix="/api/v1")
app.mount("/mcp", mcp_app)
2026-05-14 15:07:34 +08:00
@app.exception_handler(VectorStoreSchemaError)
async def vector_store_schema_exception_handler(request: Request, exc: VectorStoreSchemaError):
"""Return a stable JSON response for vector store schema/runtime errors."""
logger.error(f"向量库 schema 异常: {exc}")
return JSONResponse(
status_code=500,
content=jsonable_encoder(
ErrorResponse(
error="VectorStoreSchemaError",
message=str(exc),
)
),
)
2026-05-14 15:07:34 +08:00
@app.exception_handler(Exception)
async def global_exception_handler(request: Request, exc: Exception):
"""Global exception handler."""
logger.error(f"未处理的异常: {exc}")
return JSONResponse(
status_code=500,
content=jsonable_encoder(
ErrorResponse(
error="InternalServerError",
message=str(exc),
)
),
2026-05-14 15:07:34 +08:00
)
@app.get("/health", tags=["health"])
async def health_check():
"""Health check endpoint."""
return {
"status": "healthy",
"app": settings.app_name,
"version": settings.app_version,
}
@app.get("/", tags=["root"])
async def root():
"""Root endpoint."""
return {
"message": f"Welcome to {settings.app_name}",
"version": settings.app_version,
"docs": "/docs",
"health": "/health",
}
if __name__ == "__main__":
import uvicorn
uvicorn.run(
"app.api.main:app",
host=settings.api_host,
port=settings.api_port,
reload=settings.debug,
log_level="info",
)