主要变更: - 后端代码从根目录迁移到 app/ 目录 - 前端代码从 frontend/ 重命名为 WebUI/ - 更新所有导入路径以适配新结构 - 提取公共 API 响应函数到 app/api/common.py - 精简验证器服务代码 - 更新启动脚本和文档 测试: - 新增完整测试套件 (tests/) - 单元测试: 模型、仓库层 - 集成测试: 覆盖所有 22+ API 端点 - E2E 测试: 4个完整工作流场景 - 添加 pytest 配置和测试运行脚本
56 lines
1.7 KiB
Python
56 lines
1.7 KiB
Python
"""FastAPI 应用工厂"""
|
|
from fastapi import FastAPI
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
from app.api.lifespan import lifespan
|
|
from app.api.routes import api_router
|
|
from app.api.errors import proxy_pool_exception_handler, pydantic_validation_handler, general_exception_handler
|
|
from app.core.exceptions import ProxyPoolException
|
|
from pydantic import ValidationError
|
|
from app.core.config import settings as app_settings
|
|
|
|
# 导入并注册所有插件(显式注册模式)
|
|
import app.plugins
|
|
|
|
|
|
def create_app() -> FastAPI:
|
|
app = FastAPI(
|
|
title="代理池API",
|
|
version="2.0.0",
|
|
lifespan=lifespan,
|
|
)
|
|
|
|
# CORS
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=app_settings.cors_origins_list,
|
|
allow_credentials=True,
|
|
allow_methods=["*"],
|
|
allow_headers=["*"],
|
|
)
|
|
|
|
# 异常处理
|
|
app.add_exception_handler(ProxyPoolException, proxy_pool_exception_handler)
|
|
app.add_exception_handler(ValidationError, pydantic_validation_handler)
|
|
app.add_exception_handler(Exception, general_exception_handler)
|
|
|
|
# 路由
|
|
app.include_router(api_router)
|
|
|
|
@app.get("/")
|
|
async def root():
|
|
return {"message": "欢迎使用代理池API", "status": "running", "data": None}
|
|
|
|
@app.get("/health")
|
|
async def health_check():
|
|
from datetime import datetime
|
|
scheduler = app.state.scheduler_service
|
|
return {
|
|
"status": "healthy",
|
|
"timestamp": datetime.now().isoformat(),
|
|
"database": "connected",
|
|
"scheduler": "running" if scheduler.running else "stopped",
|
|
"version": "2.0.0",
|
|
}
|
|
|
|
return app
|