- Set default API port to 18080 in config.py - Add configurable validation_targets to SettingsSchema and DEFAULT_SETTINGS - Update ValidatorService to support runtime test URL updates - Hot-reload validation_targets from DB on startup and on settings save - Add domestic fallback URLs (baidu.com, qq.com) to reduce foreign dependency risk - Update Settings.vue to allow adding/removing validator target URLs in UI
56 lines
2.3 KiB
Python
56 lines
2.3 KiB
Python
"""设置相关路由"""
|
|
from fastapi import APIRouter, Request
|
|
from app.core.db import get_db
|
|
from app.repositories.settings_repo import SettingsRepository
|
|
from app.models.schemas import SettingsSchema
|
|
from app.api.common import success_response
|
|
from app.core.log import logger
|
|
|
|
router = APIRouter(prefix="/api/settings", tags=["settings"])
|
|
settings_repo = SettingsRepository()
|
|
|
|
|
|
@router.get("")
|
|
async def get_settings():
|
|
async with get_db() as db:
|
|
settings = await settings_repo.get_all(db)
|
|
return success_response("获取设置成功", settings)
|
|
|
|
|
|
@router.post("")
|
|
async def save_settings(request: SettingsSchema, http_request: Request):
|
|
async with get_db() as db:
|
|
success = await settings_repo.save(db, request.model_dump())
|
|
if not success:
|
|
raise RuntimeError("保存设置失败")
|
|
|
|
# 热更新运行中调度器的间隔时间
|
|
scheduler = getattr(http_request.app.state, "scheduler", None)
|
|
worker_pool = getattr(http_request.app.state, "worker_pool", None)
|
|
validator = getattr(http_request.app.state, "validator", None)
|
|
|
|
if scheduler:
|
|
new_interval = request.validate_interval_minutes
|
|
if scheduler.interval_minutes != new_interval:
|
|
scheduler.interval_minutes = new_interval
|
|
logger.info(f"Scheduler interval updated to {new_interval} minutes")
|
|
|
|
# 热更新 Worker 池大小
|
|
if worker_pool and worker_pool.worker_count != request.default_concurrency:
|
|
await worker_pool.resize(request.default_concurrency)
|
|
logger.info(f"Worker pool resized to {request.default_concurrency}")
|
|
|
|
# 热更新验证器超时和并发(下次验证时生效)
|
|
if validator:
|
|
validator._init_timeout = request.validation_timeout
|
|
validator._init_connect_timeout = request.validation_timeout
|
|
validator._init_max_concurrency = request.default_concurrency
|
|
if request.validation_targets:
|
|
validator.update_test_urls(request.validation_targets)
|
|
# 重新创建 semaphore 和 session
|
|
validator._semaphore = None
|
|
await validator.close()
|
|
logger.info(f"Validator config updated: timeout={request.validation_timeout}, concurrency={request.default_concurrency}, targets={request.validation_targets}")
|
|
|
|
return success_response("保存设置成功", request.model_dump())
|