重构: 迁移后端代码到 app 目录,前端移动到 WebUI,添加完整测试套件
主要变更: - 后端代码从根目录迁移到 app/ 目录 - 前端代码从 frontend/ 重命名为 WebUI/ - 更新所有导入路径以适配新结构 - 提取公共 API 响应函数到 app/api/common.py - 精简验证器服务代码 - 更新启动脚本和文档 测试: - 新增完整测试套件 (tests/) - 单元测试: 模型、仓库层 - 集成测试: 覆盖所有 22+ API 端点 - E2E 测试: 4个完整工作流场景 - 添加 pytest 配置和测试运行脚本
This commit is contained in:
30
app/models/__init__.py
Normal file
30
app/models/__init__.py
Normal file
@@ -0,0 +1,30 @@
|
||||
"""数据模型包"""
|
||||
from .domain import ProxyRaw, Proxy, PluginInfo
|
||||
from .schemas import (
|
||||
ProxyCreate,
|
||||
ProxyResponse,
|
||||
PluginResponse,
|
||||
SettingsSchema,
|
||||
CrawlResult,
|
||||
ProxyListRequest,
|
||||
ProxyDeleteItem,
|
||||
BatchDeleteRequest,
|
||||
PluginToggleRequest,
|
||||
ExportRequest,
|
||||
)
|
||||
|
||||
__all__ = [
|
||||
"ProxyRaw",
|
||||
"Proxy",
|
||||
"PluginInfo",
|
||||
"ProxyCreate",
|
||||
"ProxyResponse",
|
||||
"PluginResponse",
|
||||
"SettingsSchema",
|
||||
"CrawlResult",
|
||||
"ProxyListRequest",
|
||||
"ProxyDeleteItem",
|
||||
"BatchDeleteRequest",
|
||||
"PluginToggleRequest",
|
||||
"ExportRequest",
|
||||
]
|
||||
42
app/models/domain.py
Normal file
42
app/models/domain.py
Normal file
@@ -0,0 +1,42 @@
|
||||
"""领域模型 - 纯数据结构,不依赖任何框架"""
|
||||
from dataclasses import dataclass, field
|
||||
from datetime import datetime
|
||||
from typing import Optional
|
||||
|
||||
|
||||
@dataclass
|
||||
class ProxyRaw:
|
||||
"""爬虫爬取的原始代理数据"""
|
||||
ip: str
|
||||
port: int
|
||||
protocol: str = "http"
|
||||
|
||||
def __post_init__(self):
|
||||
self.protocol = self.protocol.lower().strip()
|
||||
if self.protocol not in ("http", "https", "socks4", "socks5"):
|
||||
self.protocol = "http"
|
||||
|
||||
|
||||
@dataclass
|
||||
class Proxy:
|
||||
"""数据库中的代理实体"""
|
||||
ip: str
|
||||
port: int
|
||||
protocol: str
|
||||
score: int
|
||||
response_time_ms: Optional[float] = None
|
||||
last_check: Optional[datetime] = None
|
||||
created_at: Optional[datetime] = None
|
||||
|
||||
|
||||
@dataclass
|
||||
class PluginInfo:
|
||||
"""插件元数据"""
|
||||
id: str
|
||||
name: str
|
||||
display_name: str
|
||||
description: str
|
||||
enabled: bool
|
||||
last_run: Optional[datetime] = None
|
||||
success_count: int = 0
|
||||
failure_count: int = 0
|
||||
105
app/models/schemas.py
Normal file
105
app/models/schemas.py
Normal file
@@ -0,0 +1,105 @@
|
||||
"""Pydantic 模型 - 用于 API 请求/响应校验"""
|
||||
from pydantic import BaseModel, Field, field_validator
|
||||
from typing import Optional, List
|
||||
|
||||
|
||||
class ProxyCreate(BaseModel):
|
||||
ip: str
|
||||
port: int = Field(ge=1, le=65535)
|
||||
protocol: str = "http"
|
||||
score: int = Field(default=10, ge=0, le=100)
|
||||
|
||||
@field_validator("protocol")
|
||||
@classmethod
|
||||
def validate_protocol(cls, v: str):
|
||||
v = v.lower().strip()
|
||||
if v not in ("http", "https", "socks4", "socks5"):
|
||||
raise ValueError("protocol must be http, https, socks4 or socks5")
|
||||
return v
|
||||
|
||||
|
||||
class ProxyResponse(BaseModel):
|
||||
ip: str
|
||||
port: int
|
||||
protocol: str
|
||||
score: int
|
||||
last_check: Optional[str] = None
|
||||
|
||||
|
||||
class PluginResponse(BaseModel):
|
||||
id: str
|
||||
name: str
|
||||
display_name: str
|
||||
description: str
|
||||
enabled: bool
|
||||
last_run: Optional[str] = None
|
||||
success_count: int = 0
|
||||
failure_count: int = 0
|
||||
|
||||
|
||||
class SettingsSchema(BaseModel):
|
||||
crawl_timeout: int = Field(default=30, ge=5, le=120)
|
||||
validation_timeout: int = Field(default=10, ge=3, le=60)
|
||||
max_retries: int = Field(default=3, ge=0, le=10)
|
||||
default_concurrency: int = Field(default=50, ge=10, le=200)
|
||||
min_proxy_score: int = Field(default=0, ge=0, le=100)
|
||||
proxy_expiry_days: int = Field(default=7, ge=1, le=30)
|
||||
auto_validate: bool = True
|
||||
validate_interval_minutes: int = Field(default=30, ge=5, le=1440)
|
||||
|
||||
|
||||
class CrawlResult(BaseModel):
|
||||
plugin_id: str
|
||||
proxy_count: int
|
||||
valid_count: int
|
||||
invalid_count: int = 0
|
||||
|
||||
|
||||
class ProxyListRequest(BaseModel):
|
||||
page: int = Field(default=1, ge=1)
|
||||
page_size: int = Field(default=20, ge=1, le=100)
|
||||
protocol: Optional[str] = None
|
||||
min_score: int = Field(default=0, ge=0)
|
||||
max_score: Optional[int] = Field(default=None, ge=0)
|
||||
sort_by: str = "last_check"
|
||||
sort_order: str = "DESC"
|
||||
|
||||
@field_validator("protocol")
|
||||
@classmethod
|
||||
def validate_protocol(cls, v):
|
||||
if v is not None and v.lower() not in ("http", "https", "socks4", "socks5"):
|
||||
raise ValueError("协议类型必须是 http, https, socks4 或 socks5")
|
||||
return v.lower() if v else v
|
||||
|
||||
@field_validator("sort_by")
|
||||
@classmethod
|
||||
def validate_sort_by(cls, v):
|
||||
if v not in ("ip", "port", "protocol", "score", "last_check"):
|
||||
raise ValueError("排序字段必须是 ip, port, protocol, score 或 last_check")
|
||||
return v
|
||||
|
||||
@field_validator("sort_order")
|
||||
@classmethod
|
||||
def validate_sort_order(cls, v):
|
||||
if v.upper() not in ("ASC", "DESC"):
|
||||
raise ValueError("排序方式必须是 ASC 或 DESC")
|
||||
return v.upper()
|
||||
|
||||
|
||||
class ProxyDeleteItem(BaseModel):
|
||||
ip: str
|
||||
port: int = Field(ge=1, le=65535)
|
||||
|
||||
|
||||
class BatchDeleteRequest(BaseModel):
|
||||
proxies: List[ProxyDeleteItem] = Field(max_length=1000)
|
||||
|
||||
|
||||
class PluginToggleRequest(BaseModel):
|
||||
enabled: bool
|
||||
|
||||
|
||||
class ExportRequest(BaseModel):
|
||||
format: str = Field(pattern=r"^(csv|txt|json)$")
|
||||
protocol: Optional[str] = None
|
||||
limit: int = Field(default=10000, ge=1, le=100000)
|
||||
Reference in New Issue
Block a user