mirror of
https://github.com/snailyp/gemini-balance.git
synced 2026-05-17 23:47:35 +08:00
主要变更:
1. **数据库集成**:
* 引入 MySQL 数据库支持,使用 SQLAlchemy 和 `databases` 库持久化存储应用程序设置。
* 添加了 `app/database` 目录,包含数据库连接、模型和初始化逻辑。
* 更新 `requirements.txt` 添加数据库相关依赖 (`pymysql`, `sqlalchemy`, `aiomysql`, `databases`, `python-dotenv`)。
2. **配置管理重构**:
* 重构 `ConfigService` (`app/service/config/config_service.py`),使其从数据库加载和保存设置,并支持从 `.env` 文件同步初始配置到数据库。
* 修改 `Settings` 模型 (`app/config/config.py`) 以包含数据库连接信息,并添加了从数据库加载/同步配置的逻辑。
* 配置相关的路由 (`app/router/config_routes.py`) 更新为异步,并调用新的 `ConfigService` 方法。
* `KeyManager` (`app/service/key/key_manager.py`) 现在可以在配置更新后重置和重新初始化。
3. **错误日志查看器**:
* 新增 `/logs` 页面 (`app/templates/error_logs.html`) 用于展示应用程序错误日志。
* 添加了相应的路由 (`app/router/log_routes.py`)、静态资源 (`app/static/css/error_logs.css`, `app/static/js/error_logs.js`) 和日志记录器 (`app/log/logger.py`)。
* 在配置页面和密钥管理页面的导航栏中添加了指向日志页面的链接。
4. **异步操作**:
* 将配置服务和相关路由转换为异步 (`async def`) 以支持异步数据库操作。
5. **其他**:
* 更新了应用程序初始化逻辑 (`app/core/application.py`, `app/core/initialization.py`) 以包含数据库连接的建立和关闭。
49 lines
1.8 KiB
Python
49 lines
1.8 KiB
Python
"""
|
|
配置路由模块
|
|
"""
|
|
from typing import Any, Dict
|
|
from fastapi import APIRouter, HTTPException, Request
|
|
from fastapi.responses import RedirectResponse
|
|
|
|
from app.core.security import verify_auth_token
|
|
from app.log.logger import get_config_routes_logger
|
|
from app.service.config.config_service import ConfigService
|
|
|
|
# 创建路由
|
|
router = APIRouter(prefix="/api/config", tags=["config"])
|
|
|
|
logger = get_config_routes_logger()
|
|
|
|
|
|
@router.get("", response_model=Dict[str, Any])
|
|
async def get_config(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 config page")
|
|
return RedirectResponse(url="/", status_code=302)
|
|
return await ConfigService.get_config()
|
|
|
|
|
|
@router.put("", response_model=Dict[str, Any])
|
|
async def update_config(config_data: Dict[str, Any], 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 config page")
|
|
return RedirectResponse(url="/", status_code=302)
|
|
try:
|
|
return await ConfigService.update_config(config_data)
|
|
except Exception as e:
|
|
raise HTTPException(status_code=400, detail=str(e))
|
|
|
|
|
|
@router.post("/reset", response_model=Dict[str, Any])
|
|
async def reset_config(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 config page")
|
|
return RedirectResponse(url="/", status_code=302)
|
|
try:
|
|
return await ConfigService.reset_config()
|
|
except Exception as e:
|
|
raise HTTPException(status_code=400, detail=str(e))
|