mirror of
https://github.com/snailyp/gemini-balance.git
synced 2026-05-12 02:19:59 +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` 进行了相关调整以支持新功能。
```
96 lines
2.7 KiB
Python
96 lines
2.7 KiB
Python
"""
|
||
应用程序工厂模块,负责创建和配置FastAPI应用程序实例
|
||
"""
|
||
from contextlib import asynccontextmanager
|
||
from fastapi import FastAPI
|
||
from fastapi.staticfiles import StaticFiles
|
||
|
||
from app.config.config import settings, sync_initial_settings
|
||
from app.log.logger import get_application_logger
|
||
from app.middleware.middleware import setup_middlewares
|
||
from app.exception.exceptions import setup_exception_handlers
|
||
from app.router.routes import setup_routers
|
||
from app.service.key.key_manager import get_key_manager_instance
|
||
from app.core.initialization import initialize_app
|
||
from app.database.connection import connect_to_db, disconnect_from_db
|
||
from app.database.initialization import initialize_database
|
||
from app.scheduler.key_checker import start_scheduler, stop_scheduler # 导入调度器函数
|
||
|
||
logger = get_application_logger()
|
||
|
||
@asynccontextmanager
|
||
async def lifespan(app: FastAPI):
|
||
"""
|
||
应用程序生命周期管理器
|
||
|
||
Args:
|
||
app: FastAPI应用实例
|
||
"""
|
||
# 启动事件
|
||
logger.info("Application starting up...")
|
||
try:
|
||
# 初始化数据库
|
||
initialize_database()
|
||
logger.info("Database initialized successfully")
|
||
|
||
# 连接到数据库
|
||
await connect_to_db()
|
||
|
||
# 同步初始配置(DB优先,然后同步回DB)
|
||
await sync_initial_settings()
|
||
|
||
# 初始化KeyManager (使用可能已从DB更新的settings)
|
||
await get_key_manager_instance(settings.API_KEYS)
|
||
logger.info("KeyManager initialized successfully")
|
||
except Exception as e:
|
||
logger.error(f"Failed to initialize application: {str(e)}")
|
||
raise
|
||
|
||
# 启动调度器
|
||
start_scheduler()
|
||
logger.info("Scheduler started successfully.")
|
||
|
||
yield # 应用程序运行期间
|
||
|
||
# 关闭事件
|
||
logger.info("Application shutting down...")
|
||
|
||
# 停止调度器
|
||
stop_scheduler()
|
||
logger.info("Scheduler stopped.")
|
||
|
||
# 断开数据库连接
|
||
await disconnect_from_db()
|
||
|
||
def create_app() -> FastAPI:
|
||
"""
|
||
创建并配置FastAPI应用程序实例
|
||
|
||
Returns:
|
||
FastAPI: 配置好的FastAPI应用程序实例
|
||
"""
|
||
# 初始化应用程序
|
||
initialize_app()
|
||
|
||
# 创建FastAPI应用
|
||
app = FastAPI(
|
||
title="Gemini Balance API",
|
||
description="Gemini API代理服务,支持负载均衡和密钥管理",
|
||
version="1.0.0",
|
||
lifespan=lifespan
|
||
)
|
||
|
||
# 配置静态文件
|
||
app.mount("/static", StaticFiles(directory="app/static"), name="static")
|
||
|
||
# 配置中间件
|
||
setup_middlewares(app)
|
||
|
||
# 配置异常处理器
|
||
setup_exception_handlers(app)
|
||
|
||
# 配置路由
|
||
setup_routers(app)
|
||
|
||
return app
|