main.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. from contextlib import asynccontextmanager
  2. from fastapi import FastAPI
  3. from fastapi.middleware.cors import CORSMiddleware
  4. from app.api.v1 import documents, export
  5. from app.core.exceptions import register_exception_handlers
  6. @asynccontextmanager
  7. async def lifespan(app: FastAPI):
  8. # 启动时:可在此初始化连接池等
  9. yield
  10. # 关闭时:清理资源
  11. def create_app() -> FastAPI:
  12. app = FastAPI(
  13. title="Axonix Text Editor API",
  14. version="0.1.0",
  15. description="文本编辑器后端 API —— 阶段 0:文档 CRUD + 导出下载",
  16. lifespan=lifespan,
  17. )
  18. # CORS(开发阶段放开,生产按需收紧)
  19. app.add_middleware(
  20. CORSMiddleware,
  21. allow_origins=["*"],
  22. allow_credentials=True,
  23. allow_methods=["*"],
  24. allow_headers=["*"],
  25. )
  26. # 全局异常处理
  27. register_exception_handlers(app)
  28. # 路由注册
  29. app.include_router(documents.router, prefix="/api/v1")
  30. app.include_router(export.router, prefix="/api/v1")
  31. return app
  32. app = create_app()