将看板功能独立,方便独立维护功能。

This commit is contained in:
liuwei
2025-03-27 11:39:21 +08:00
parent 7bd97b579f
commit dac1848629
2 changed files with 244 additions and 127 deletions

View File

@@ -3,6 +3,7 @@ import os
import sys
import threading
import time
import yaml
from datetime import datetime
# 添加项目根目录到系统路径,确保可以导入项目模块
@@ -27,17 +28,91 @@ class DashboardServer:
self.username = username
self.password = password
self.logger = logging.getLogger("DashboardServer")
# 使用单例模式获取数据库连接
self.db_manager = DBConnectionManager.get_instance()
self.stats_db = StatsDBOperator(self.db_manager)
self.message_storage = MessageStorageDB(self.db_manager)
# 初始化数据库连接
self._init_database()
# 获取联系人管理器实例
self.contact_manager = ContactManager.get_instance()
self.app = self._create_app()
self._stop_event = threading.Event()
self._server = None # 存储服务器实例
def _load_config(self):
"""从配置文件加载配置"""
try:
# 获取项目根目录的config.yaml文件路径
config_path = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..', 'config.yaml'))
self.logger.info(f"正在加载配置文件: {config_path}")
with open(config_path, 'r', encoding='utf-8') as f:
config = yaml.safe_load(f)
return config
except Exception as e:
self.logger.error(f"加载配置文件失败: {e}")
# 返回默认配置
return {
"db_config": {
"pool_name": "wechat_boot_pool",
"pool_size": 10,
"host": "localhost",
"user": "root",
"password": "password",
"database": "message_archive",
"charset": "utf8mb4",
"use_unicode": True,
"get_warnings": True,
"pool_reset_session": True
},
"redis_config": {
"host": "localhost",
"port": 6379,
"db": 0,
"decode_responses": True
}
}
def _init_database(self):
"""初始化数据库连接"""
try:
# 加载配置
config = self._load_config()
# 获取数据库配置
db_config = config.get("db_config", {})
redis_config = config.get("redis_config", {})
self.logger.info("正在初始化数据库连接...")
self.logger.info(f"数据库主机: {db_config.get('host')}, 数据库: {db_config.get('database')}")
# 初始化数据库连接管理器
self.db_manager = DBConnectionManager.get_instance(
pool_name=db_config.get("pool_name"),
pool_size=db_config.get("pool_size"),
host=db_config.get("host"),
user=db_config.get("user"),
password=db_config.get("password"),
database=db_config.get("database"),
charset=db_config.get("charset"),
use_unicode=db_config.get("use_unicode"),
get_warnings=db_config.get("get_warnings"),
pool_reset_session=db_config.get("pool_reset_session"),
redis_host=redis_config.get("host"),
redis_port=redis_config.get("port"),
redis_db=redis_config.get("db"),
redis_decode_responses=redis_config.get("decode_responses")
)
# 初始化数据库操作类
self.stats_db = StatsDBOperator(self.db_manager)
self.message_storage = MessageStorageDB(self.db_manager)
self.logger.info("数据库连接初始化成功")
except Exception as e:
self.logger.error(f"初始化数据库连接失败: {e}")
raise
def _create_app(self) -> Flask:
"""创建Flask应用"""
app = Flask(__name__)