后端重构: - 新增分层架构:API Routes -> Services -> Repositories -> Infrastructure - 彻底移除全局单例,全面采用 FastAPI 依赖注入 - 新增 api/ 目录拆分路由(proxies, plugins, scheduler, settings, stats) - 新增 services/ 业务逻辑层:ProxyService, PluginService, SchedulerService, ValidatorService, SettingsService - 新增 repositories/ 数据访问层:ProxyRepository, SettingsRepository, PluginSettingsRepository - 新增 models/ 层:Pydantic Schemas + Domain Models - 重写 core/config.py:采用 Pydantic Settings 管理配置 - 新增 core/db.py:基于 asynccontextmanager 的连接管理,支持数据库迁移 - 新增 core/exceptions.py:统一业务异常体系 插件系统重构(核心): - 新增 core/plugin_system/:BaseCrawlerPlugin + PluginRegistry - 采用显式注册模式(装饰器 + plugins/__init__.py),类型安全、测试友好 - 新增 plugins/base.py:BaseHTTPPlugin 通用 HTTP 爬虫基类 - 迁移全部 7 个插件到新架构(fate0, proxylist_download, ip3366, ip89, kuaidaili, speedx, yundaili) - 插件状态持久化到 plugin_settings 表 任务调度重构: - 新增 core/tasks/queue.py:ValidationQueue + WorkerPool - 解耦爬取与验证:爬虫只负责爬取,代理提交队列后由 Worker 异步验证 - 调度器定时从数据库拉取存量代理并分批投入验证队列 前端调整: - 新增 frontend/src/services/ 层拆分 API 调用逻辑 - 调整 stores/ 和 views/ 使用 Service 层 - 保持 API 兼容性,页面无需大幅修改 其他: - 新增 main.py 作为新入口 - 新增 DESIGN.md 架构设计文档 - 更新 requirements.txt 增加 pydantic-settings
60 lines
1.4 KiB
Python
60 lines
1.4 KiB
Python
"""全局配置 - 使用 Pydantic Settings 支持环境变量和 .env 文件"""
|
|
import os
|
|
from typing import List
|
|
from pydantic_settings import BaseSettings, SettingsConfigDict
|
|
|
|
|
|
class Settings(BaseSettings):
|
|
model_config = SettingsConfigDict(
|
|
env_file=".env",
|
|
env_file_encoding="utf-8",
|
|
extra="ignore",
|
|
)
|
|
|
|
# 数据库配置
|
|
db_path: str = "db/proxies.sqlite"
|
|
|
|
# API 服务配置
|
|
host: str = "0.0.0.0"
|
|
port: int = 9949
|
|
|
|
# 验证器配置
|
|
validator_timeout: int = 5
|
|
validator_max_concurrency: int = 200
|
|
validator_connect_timeout: int = 3
|
|
|
|
# 爬虫配置
|
|
crawler_num_validators: int = 50
|
|
crawler_max_queue_size: int = 500
|
|
|
|
# 日志配置
|
|
log_level: str = "INFO"
|
|
log_dir: str = "logs"
|
|
|
|
# 导出配置
|
|
export_max_records: int = 10000
|
|
|
|
# 代理评分配置
|
|
score_valid: int = 10
|
|
score_invalid: int = -5
|
|
score_min: int = 0
|
|
score_max: int = 100
|
|
|
|
# 插件配置
|
|
plugins_dir: str = "plugins"
|
|
|
|
# CORS 配置
|
|
cors_origins: str = "http://localhost:8080,http://localhost:5173"
|
|
|
|
@property
|
|
def cors_origins_list(self) -> List[str]:
|
|
return [origin.strip() for origin in self.cors_origins.split(",") if origin.strip()]
|
|
|
|
@property
|
|
def base_dir(self) -> str:
|
|
return os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
|
|
|
|
|
# 全局配置实例(启动时加载一次)
|
|
settings = Settings()
|