Files
fastapi/backend/app/main.py
T

39 lines
1.0 KiB
Python
Raw Normal View History

from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from app.api.router import api_router
from app.core.config import settings
from app.core.errors import register_exception_handlers
def create_app() -> FastAPI:
"""应用工厂:创建 FastAPI 实例并挂载中间件、路由和异常处理器。"""
app = FastAPI(
title=settings.app_name,
debug=settings.app_debug,
version="0.1.0",
docs_url="/docs",
redoc_url="/redoc",
)
app.add_middleware(
CORSMiddleware,
allow_origins=[
"http://127.0.0.1:5173",
"http://localhost:5173",
"http://127.0.0.1:5174",
"http://localhost:5174",
],
allow_origin_regex=r"^http://(127\.0\.0\.1|localhost):\d+$",
allow_credentials=False,
allow_methods=["*"],
allow_headers=["*"],
)
app.include_router(api_router, prefix=settings.api_v1_prefix)
register_exception_handlers(app)
return app
app = create_app()