- 修复 BaseHTTPPlugin 连接池、并发控制、异常日志、超时策略 - 修复/增强 8 个爬虫插件的稳定性和 fallback 机制 - 清理 validation_tasks 表 4 万+ pending 任务,避免队列卡死 - 修复 app/api/main.py 缺失全局 app 实例导致的 500 错误 - 提升前端 Axios 超时到 120 秒,避免请求断开 - 修复插件统计持久化和调度器生命周期问题
59 lines
1.7 KiB
Python
59 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
|
|
|
|
|
|
app = create_app()
|