e86dde7445
- Register health_router at root path (no /api/v1 prefix) - Health endpoints: /health, /ready, /startup - API endpoints remain at /api/v1/* - Update route imports and registration Phase 4 Task 45/68 fully completed
109 lines
2.9 KiB
Python
109 lines
2.9 KiB
Python
"""
|
||
FastAPI 主应用
|
||
"""
|
||
from fastapi import FastAPI
|
||
from fastapi.middleware.cors import CORSMiddleware
|
||
from fastapi.middleware.gzip import GZipMiddleware
|
||
from fastapi.exceptions import RequestValidationError
|
||
from starlette.exceptions import HTTPException as StarletteHTTPException
|
||
|
||
from apps.api.app.api.routes import api_router, health_router
|
||
from apps.api.app.config import settings
|
||
from apps.api.app.middleware.exceptions import (
|
||
APIException,
|
||
api_exception_handler,
|
||
http_exception_handler,
|
||
validation_exception_handler,
|
||
general_exception_handler,
|
||
)
|
||
from apps.api.app.middleware.logging import (
|
||
RequestLoggingMiddleware,
|
||
RateLimitMiddleware,
|
||
)
|
||
|
||
# 创建 FastAPI 应用
|
||
app = FastAPI(
|
||
title="小虾 SaaS API",
|
||
description="自动化剪辑 SaaS 平台 API",
|
||
version="1.0.0",
|
||
docs_url="/docs",
|
||
redoc_url="/redoc",
|
||
)
|
||
|
||
# 注册异常处理器
|
||
app.add_exception_handler(APIException, api_exception_handler)
|
||
app.add_exception_handler(StarletteHTTPException, http_exception_handler)
|
||
app.add_exception_handler(RequestValidationError, validation_exception_handler)
|
||
app.add_exception_handler(Exception, general_exception_handler)
|
||
|
||
# CORS 中间件
|
||
app.add_middleware(
|
||
CORSMiddleware,
|
||
allow_origins=[
|
||
"http://localhost:3000",
|
||
"http://localhost:5173",
|
||
"https://yourdomain.com",
|
||
],
|
||
allow_credentials=True,
|
||
allow_methods=["*"],
|
||
allow_headers=["*"],
|
||
)
|
||
|
||
# Gzip 压缩
|
||
app.add_middleware(GZipMiddleware, minimum_size=1000)
|
||
|
||
# 请求日志
|
||
app.add_middleware(RequestLoggingMiddleware)
|
||
|
||
# 速率限制(开发环境关闭,生产环境启用)
|
||
# app.add_middleware(RateLimitMiddleware, max_requests=100, window_seconds=60)
|
||
|
||
# 注册路由
|
||
app.include_router(health_router) # 健康检查路由(根路径)
|
||
app.include_router(api_router) # API 路由(/api/v1)
|
||
|
||
|
||
@app.get("/")
|
||
async def root():
|
||
"""健康检查"""
|
||
return {
|
||
"service": "小虾 SaaS API",
|
||
"status": "running",
|
||
"version": "1.0.0",
|
||
}
|
||
|
||
|
||
@app.get("/health")
|
||
async def health_check():
|
||
"""健康检查接口"""
|
||
return {
|
||
"status": "healthy",
|
||
"database": "ok", # TODO: 实际检查数据库连接
|
||
"redis": "ok", # TODO: 实际检查 Redis 连接
|
||
}
|
||
|
||
|
||
@app.on_event("startup")
|
||
async def startup():
|
||
"""应用启动时初始化连接池"""
|
||
if not settings.USE_IN_MEMORY_DB:
|
||
from packages.adapters.postgres.connection_pool import db_pool
|
||
db_pool.initialize(
|
||
connection_string=settings.DATABASE_URL,
|
||
minconn=2,
|
||
maxconn=10,
|
||
)
|
||
|
||
|
||
@app.on_event("shutdown")
|
||
async def shutdown():
|
||
"""应用关闭时关闭所有连接"""
|
||
if not settings.USE_IN_MEMORY_DB:
|
||
from packages.adapters.postgres.connection_pool import db_pool
|
||
db_pool.close_all()
|
||
|
||
|
||
if __name__ == "__main__":
|
||
import uvicorn
|
||
uvicorn.run(app, host="0.0.0.0", port=8000)
|