Files
2026-06-04 10:55:23 +08:00

37 lines
1.0 KiB
Python

from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from app.api import health
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",
root_path=settings.app_root_path,
docs_url="/docs",
redoc_url="/redoc",
)
app.add_middleware(
CORSMiddleware,
allow_origins=settings.cors_allow_origins,
allow_origin_regex=settings.cors_allow_origin_regex or None,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
app.include_router(health.router, prefix="/health", tags=["health"])
app.include_router(api_router, prefix=settings.api_v1_prefix)
register_exception_handlers(app)
return app
app = create_app()