后端优化: - 合并 api/routes/stats.py 到 api/routes/proxies.py,统计接口变更为 /api/proxies/stats - 内联 services/settings_service.py:settings.py 和 scheduler.py 直接使用 SettingsRepository - 简化 repositories/proxy_repo.py:提取 _row_to_proxy 辅助函数,消除重复构造代码 - 更新 api/lifespan.py 和 api/deps.py,移除对 settings_service 的依赖 - 从 requirements.txt 移除 websockets 依赖(已废弃的 WebSocket 功能残留) 前端适配: - 更新 frontend/src/api/index.js:stats 接口路径同步为 /api/proxies/stats - 清理 api/index.js 中未使用的 createRequestConfig 和多余 JSDoc 注释 脚本优化: - 移除 script/stop.bat 末尾的 pause,避免自动化调用时挂起
42 lines
1.3 KiB
Python
42 lines
1.3 KiB
Python
"""应用生命周期管理"""
|
|
from contextlib import asynccontextmanager
|
|
from fastapi import FastAPI
|
|
from core.db import init_db, get_db
|
|
from core.config import settings as app_settings
|
|
from core.log import logger
|
|
from api.deps import create_scheduler_service
|
|
from repositories.settings_repo import SettingsRepository
|
|
|
|
settings_repo = SettingsRepository()
|
|
|
|
|
|
@asynccontextmanager
|
|
async def lifespan(app: FastAPI):
|
|
"""应用启动和关闭时的生命周期管理"""
|
|
# 初始化数据库
|
|
await init_db()
|
|
|
|
# 创建调度器并挂载到 app.state
|
|
scheduler_service = create_scheduler_service()
|
|
app.state.scheduler_service = scheduler_service
|
|
app.state.validation_queue = scheduler_service.validation_queue
|
|
|
|
# 加载设置并决定是否启动调度器
|
|
try:
|
|
async with get_db() as db:
|
|
settings = await settings_repo.get_all(db)
|
|
scheduler_service.interval_minutes = settings.get(
|
|
"validate_interval_minutes", app_settings.validator_timeout
|
|
)
|
|
if settings.get("auto_validate", True):
|
|
await scheduler_service.start()
|
|
except Exception as e:
|
|
logger.error(f"Failed to load settings on startup: {e}")
|
|
|
|
logger.info("API server started")
|
|
yield
|
|
|
|
# 关闭调度器
|
|
await scheduler_service.stop()
|
|
logger.info("API server shutdown")
|