refactor(app): restructure project to avoid circular imports

This commit is contained in:
InfinityPacer
2024-09-25 02:20:12 +08:00
parent 88394005e5
commit cf4c6b2d40
6 changed files with 223 additions and 194 deletions

31
app/factory.py Normal file
View File

@@ -0,0 +1,31 @@
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from app.core.config import settings
from app.startup.lifecycle import lifespan
def create_app() -> FastAPI:
"""
创建并配置 FastAPI 应用实例。
"""
app = FastAPI(
title=settings.PROJECT_NAME,
openapi_url=f"{settings.API_V1_STR}/openapi.json",
lifespan=lifespan
)
# 配置 CORS 中间件
app.add_middleware(
CORSMiddleware,
allow_origins=settings.ALLOWED_HOSTS,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
return app
# 创建 FastAPI 应用实例
app = create_app()