- Add Free_Proxy_Website-style fpw_* plugins and register them - Per-plugin crawl timeout (crawl_timeout_seconds=120); remove global crawl_timeout setting - Validator: fix connect vs total timeout on save; SOCKS session LRU cache; drop redundant semaphore - Validation handler uses single DB connection; batch upsert after crawl; WorkerPool put_nowait - Remove unused max_retries from settings API/UI; settings maintenance SQL + init_db cleanup of deprecated keys - WebSocket dashboard stats; ProxyList pool_filter and API alignment - POST /api/proxies/delete-one for IPv6-safe deletes; task poll stops on 404 - pytest uses PROXYPOOL_DB_PATH=db/proxies.test.sqlite so tests do not wipe production DB - .gitignore: explicit proxies.test.sqlite patterns; fix plugin_service ValidationException import Made-with: Cursor
65 lines
2.1 KiB
Python
65 lines
2.1 KiB
Python
"""FastAPI 应用工厂"""
|
||
import asyncio
|
||
import sys
|
||
|
||
# Windows 上默认 Proactor 事件循环易导致 httpx 异步出站 ConnectTimeout,与同步请求表现不一致
|
||
if sys.platform == "win32":
|
||
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
|
||
|
||
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, http_exception_handler, pydantic_validation_handler, general_exception_handler
|
||
from app.core.exceptions import ProxyPoolException
|
||
from pydantic import ValidationError
|
||
from starlette.exceptions import HTTPException as StarletteHTTPException
|
||
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,
|
||
allow_credentials=True,
|
||
allow_methods=["*"],
|
||
allow_headers=["*"],
|
||
)
|
||
|
||
# 异常处理
|
||
app.add_exception_handler(ProxyPoolException, proxy_pool_exception_handler)
|
||
app.add_exception_handler(StarletteHTTPException, http_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
|
||
return {
|
||
"status": "healthy",
|
||
"timestamp": datetime.now().isoformat(),
|
||
"database": "connected",
|
||
"scheduler": "running" if scheduler.running else "stopped",
|
||
"version": "2.0.0",
|
||
}
|
||
|
||
return app
|