mirror of
https://github.com/snailyp/gemini-balance.git
synced 2026-05-11 18:09:55 +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`) 以包含数据库连接的建立和关闭。
41 lines
1.0 KiB
Python
41 lines
1.0 KiB
Python
"""
|
|
应用程序初始化模块
|
|
"""
|
|
from pathlib import Path
|
|
from typing import List
|
|
|
|
from app.log.logger import get_initialization_logger
|
|
|
|
logger = get_initialization_logger()
|
|
|
|
|
|
def ensure_directories_exist(directories: List[str]) -> None:
|
|
"""
|
|
确保指定的目录存在,如果不存在则创建
|
|
|
|
Args:
|
|
directories: 要确保存在的目录列表
|
|
"""
|
|
for directory in directories:
|
|
try:
|
|
Path(directory).mkdir(parents=True, exist_ok=True)
|
|
logger.info(f"Ensured directory exists: {directory}")
|
|
except Exception as e:
|
|
logger.error(f"Failed to create directory {directory}: {str(e)}")
|
|
|
|
|
|
def initialize_app() -> None:
|
|
"""
|
|
初始化应用程序,确保所需的目录和文件都存在
|
|
"""
|
|
# 确保必要的目录存在
|
|
required_directories = [
|
|
"app/static/css",
|
|
"app/static/js",
|
|
"app/static/icons",
|
|
"app/templates",
|
|
]
|
|
|
|
ensure_directories_exist(required_directories)
|
|
logger.info("core initialization completed")
|