refactor: 移除不必要的 CORS 中间件配置,优化异常处理函数格式

This commit is contained in:
2026-01-13 15:32:10 +08:00
parent c6f16de569
commit 3059f9c259

16
main.py
View File

@@ -33,16 +33,6 @@ app = FastAPI(
debug=appmeta.debug, debug=appmeta.debug,
openapi_url="/openapi.json" if appmeta.debug else None, openapi_url="/openapi.json" if appmeta.debug else None,
) )
# 配置 CORS 中间件
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # 开发环境允许所有来源,生产环境应该限制为具体域名
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
=======
# 添加跨域 CORS 中间件,仅在调试模式下启用,以允许所有来源访问 API # 添加跨域 CORS 中间件,仅在调试模式下启用,以允许所有来源访问 API
if appmeta.debug: if appmeta.debug:
from fastapi.middleware.cors import CORSMiddleware from fastapi.middleware.cors import CORSMiddleware
@@ -53,10 +43,12 @@ if appmeta.debug:
allow_methods=["*"], allow_methods=["*"],
allow_headers=["*"], allow_headers=["*"],
) )
>>>>>>> d2c914cff89e566b0b7ab7a2655ae72905f888f5
@app.exception_handler(Exception) @app.exception_handler(Exception)
async def handle_unexpected_exceptions(request: Request, exc: Exception) -> NoReturn: async def handle_unexpected_exceptions(
request: Request,
exc: Exception
) -> NoReturn:
""" """
捕获所有未经处理的 FastAPI 异常,防止敏感信息泄露。 捕获所有未经处理的 FastAPI 异常,防止敏感信息泄露。
""" """