| 123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
- from contextlib import asynccontextmanager
- from fastapi import FastAPI
- from fastapi.middleware.cors import CORSMiddleware
- from app.api.v1 import documents, export
- from app.core.exceptions import register_exception_handlers
- @asynccontextmanager
- async def lifespan(app: FastAPI):
- # 启动时:可在此初始化连接池等
- yield
- # 关闭时:清理资源
- def create_app() -> FastAPI:
- app = FastAPI(
- title="Axonix Text Editor API",
- version="0.1.0",
- description="文本编辑器后端 API —— 阶段 0:文档 CRUD + 导出下载",
- lifespan=lifespan,
- )
- # CORS(开发阶段放开,生产按需收紧)
- app.add_middleware(
- CORSMiddleware,
- allow_origins=["*"],
- allow_credentials=True,
- allow_methods=["*"],
- allow_headers=["*"],
- )
- # 全局异常处理
- register_exception_handlers(app)
- # 路由注册
- app.include_router(documents.router, prefix="/api/v1")
- app.include_router(export.router, prefix="/api/v1")
- return app
- app = create_app()
|