from fastapi import FastAPI, Request from starlette.middleware.cors import CORSMiddleware from fastapi.responses import JSONResponse from api.logger import setup_logger from api.routes import router # 导入router而不是单独的函数 logger = setup_logger(__name__) def create_app(): app = FastAPI() # 配置CORS app.add_middleware( CORSMiddleware, allow_origins=["*"], allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) # 添加路由 app.include_router(router) @app.exception_handler(Exception) async def global_exception_handler(request: Request, exc: Exception): logger.error(f"An error occurred: {str(exc)}") return JSONResponse( status_code=500, content={"message": "An internal server error occurred."}, ) return app app = create_app()