40 lines
1.2 KiB
Python
40 lines
1.2 KiB
Python
from fastapi import FastAPI
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
from contextlib import asynccontextmanager
|
|
from app.config import settings
|
|
|
|
@asynccontextmanager
|
|
async def lifespan(app: FastAPI):
|
|
# 启动时:初始化 DB 连接池、Redis 事件总线与外部适配器
|
|
print(f"Starting {settings.PROJECT_NAME} in [{settings.ENVIRONMENT}] mode...")
|
|
yield
|
|
# 关闭时:优雅断开连接
|
|
print(f"Shutting down {settings.PROJECT_NAME}...")
|
|
|
|
app = FastAPI(
|
|
title=settings.PROJECT_NAME,
|
|
version=settings.VERSION,
|
|
lifespan=lifespan,
|
|
docs_url="/docs" if settings.ENVIRONMENT != "production" else None,
|
|
redoc_url="/redoc" if settings.ENVIRONMENT != "production" else None
|
|
)
|
|
|
|
# CORS 配置
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=settings.CORS_ORIGINS,
|
|
allow_credentials=True,
|
|
allow_methods=["*"],
|
|
allow_headers=["*"],
|
|
)
|
|
|
|
@app.get("/health", tags=["System"])
|
|
async def health_check():
|
|
"""生产探活与健康检查端点 (Blue-Green Probe)"""
|
|
return {
|
|
"status": "ok",
|
|
"service": "wtfx-backend",
|
|
"version": settings.VERSION,
|
|
"environment": settings.ENVIRONMENT
|
|
}
|