Niansuh commited on
Commit
e333feb
·
verified ·
1 Parent(s): 080065c

Update api/app.py

Browse files
Files changed (1) hide show
  1. api/app.py +40 -34
api/app.py CHANGED
@@ -1,34 +1,40 @@
1
- from fastapi import FastAPI, Request
2
- from starlette.middleware.cors import CORSMiddleware
3
- from fastapi.responses import JSONResponse
4
- from api.logger import setup_logger
5
- from api.routes import router # 导入router而不是单独的函数
6
-
7
- logger = setup_logger(__name__)
8
-
9
- def create_app():
10
- app = FastAPI()
11
-
12
- # 配置CORS
13
- app.add_middleware(
14
- CORSMiddleware,
15
- allow_origins=["*"],
16
- allow_credentials=True,
17
- allow_methods=["*"],
18
- allow_headers=["*"],
19
- )
20
-
21
- # 添加路由
22
- app.include_router(router)
23
-
24
- @app.exception_handler(Exception)
25
- async def global_exception_handler(request: Request, exc: Exception):
26
- logger.error(f"An error occurred: {str(exc)}")
27
- return JSONResponse(
28
- status_code=500,
29
- content={"message": "An internal server error occurred."},
30
- )
31
-
32
- return app
33
-
34
- app = create_app()
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, Request
2
+ from starlette.middleware.cors import CORSMiddleware
3
+ from fastapi.responses import JSONResponse
4
+ from api.logger import setup_logger
5
+ from api.routes import router
6
+
7
+ logger = setup_logger(__name__)
8
+
9
+ def create_app():
10
+ app = FastAPI(
11
+ title="Production API",
12
+ docs_url="/docs", # Customize docs path if necessary
13
+ redoc_url="/redoc",
14
+ openapi_url="/openapi.json",
15
+ )
16
+
17
+ # CORS settings
18
+ app.add_middleware(
19
+ CORSMiddleware,
20
+ allow_origins=["*"],
21
+ allow_credentials=True,
22
+ allow_methods=["*"],
23
+ allow_headers=["*"],
24
+ )
25
+
26
+ # Include routes
27
+ app.include_router(router)
28
+
29
+ # Global exception handler for better error reporting
30
+ @app.exception_handler(Exception)
31
+ async def global_exception_handler(request: Request, exc: Exception):
32
+ logger.error(f"An error occurred: {str(exc)}")
33
+ return JSONResponse(
34
+ status_code=500,
35
+ content={"message": "An internal server error occurred."},
36
+ )
37
+
38
+ return app
39
+
40
+ app = create_app()