mirror of
https://github.com/snailyp/gemini-balance.git
synced 2026-05-19 19:09:31 +08:00
feat: 添加密钥检查调度器并重构前端UI
主要变更:
- **调度器功能:**
- 集成 APScheduler 实现定时任务,用于定期检查API密钥的有效性。
- 在 `.env.example` 和 `app/config/config.py` 中添加了 `CHECK_INTERVAL_HOURS` 和 `TIMEZONE` 配置项。
- 在应用生命周期 (`app/core/application.py`) 中添加了调度器的启动和停止逻辑。
- 新增 `app/scheduler/` 目录及相关实现 (`key_checker.py`)。
- 新增 `app/router/scheduler_routes.py` 用于调度器相关API (如果未来需要)。
- 在 `requirements.txt` 中添加 `apscheduler` 依赖。
- **前端重构与改进:**
- 引入 `app/templates/base.html` 作为基础模板,统一页面结构和样式引入。
- 使用新的样式(推测为Tailwind CSS)重构了 `auth.html`, `config_editor.html`, `error_logs.html`, `keys_status.html` 页面,提升了UI一致性和响应式布局。
- 删除了旧的CSS文件 (`auth.css`, `config_editor.css`, `error_logs.css`, `keys_status.css`)。
- 更新了对应的 JavaScript 文件 (`config_editor.js`, `error_logs.js`, `keys_status.js`) 以适应新的HTML结构和交互。
- 在 `keys_status.html` 页面增加了按失败次数过滤密钥、批量重置失败次数、确认模态框等功能。
- 添加了新的 Logo 图片 (`logo.png`, `logo1.png`)。
- **其他:**
- 更新了 `app/router/routes.py` 以包含新的路由。
- 对 `app/service/key/key_manager.py` 和 `app/database/services.py` 进行了相关调整以支持新功能。
```
63 lines
2.4 KiB
Python
63 lines
2.4 KiB
Python
"""
|
|
定时任务控制路由模块
|
|
"""
|
|
|
|
from fastapi import APIRouter, Request, HTTPException, status # 移除 Depends, 添加 Request
|
|
from fastapi.responses import JSONResponse
|
|
|
|
from app.core.security import verify_auth_token # 导入 verify_auth_token
|
|
from app.scheduler.key_checker import start_scheduler, stop_scheduler
|
|
from app.log.logger import get_routes_logger # 使用路由日志记录器
|
|
|
|
logger = get_routes_logger()
|
|
|
|
router = APIRouter(
|
|
prefix="/api/scheduler",
|
|
tags=["Scheduler"]
|
|
# 移除全局依赖
|
|
)
|
|
|
|
# 认证检查的辅助函数
|
|
async def verify_token(request: Request):
|
|
auth_token = request.cookies.get("auth_token")
|
|
if not auth_token or not verify_auth_token(auth_token):
|
|
logger.warning("Unauthorized access attempt to scheduler API")
|
|
raise HTTPException(
|
|
status_code=status.HTTP_401_UNAUTHORIZED,
|
|
detail="Not authenticated",
|
|
headers={"WWW-Authenticate": "Bearer"},
|
|
)
|
|
|
|
@router.post("/start", summary="启动定时任务")
|
|
async def start_scheduler_endpoint(request: Request): # 添加 request 参数
|
|
"""Start the background scheduler task"""
|
|
"""
|
|
await verify_token(request) # 在函数开始处进行认证检查
|
|
"""
|
|
try:
|
|
logger.info("Received request to start scheduler.")
|
|
start_scheduler() # 调用 key_checker 中的函数
|
|
return JSONResponse(content={"message": "Scheduler started successfully."}, status_code=status.HTTP_200_OK)
|
|
except Exception as e:
|
|
logger.error(f"Error starting scheduler: {str(e)}", exc_info=True)
|
|
raise HTTPException(
|
|
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
|
detail=f"Failed to start scheduler: {str(e)}"
|
|
)
|
|
|
|
@router.post("/stop", summary="停止定时任务")
|
|
async def stop_scheduler_endpoint(request: Request): # 添加 request 参数
|
|
"""Stop the background scheduler task"""
|
|
"""
|
|
await verify_token(request) # 在函数开始处进行认证检查
|
|
"""
|
|
try:
|
|
logger.info("Received request to stop scheduler.")
|
|
stop_scheduler() # 调用 key_checker 中的函数
|
|
return JSONResponse(content={"message": "Scheduler stopped successfully."}, status_code=status.HTTP_200_OK)
|
|
except Exception as e:
|
|
logger.error(f"Error stopping scheduler: {str(e)}", exc_info=True)
|
|
raise HTTPException(
|
|
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
|
detail=f"Failed to stop scheduler: {str(e)}"
|
|
) |