还是随项目启动。解决对象传递问题
This commit is contained in:
@@ -1,6 +1,13 @@
|
|||||||
enable = true
|
enable = true
|
||||||
|
[server]
|
||||||
host = "0.0.0.0"
|
host = "0.0.0.0"
|
||||||
port = 8888
|
port = 8888
|
||||||
|
|
||||||
|
[auth]
|
||||||
username = "admin"
|
username = "admin"
|
||||||
password = "admin123"
|
password = "admin123"
|
||||||
|
|
||||||
|
[database]
|
||||||
|
# 如果为空,则使用主项目的数据库配置
|
||||||
|
# 如果设置了值,则使用自己的数据库配置
|
||||||
auto_start = "True"
|
auto_start = "True"
|
||||||
@@ -18,25 +18,69 @@ from db.stats_db import StatsDBOperator
|
|||||||
from utils.wechat.contact_manager import ContactManager
|
from utils.wechat.contact_manager import ContactManager
|
||||||
from robot_cmd.robot_command import GroupBotManager, Feature, PermissionStatus
|
from robot_cmd.robot_command import GroupBotManager, Feature, PermissionStatus
|
||||||
|
|
||||||
|
# 在导入部分添加
|
||||||
|
import toml
|
||||||
|
import os.path
|
||||||
|
|
||||||
class DashboardServer:
|
class DashboardServer:
|
||||||
"""统计看板服务器"""
|
"""统计看板服务器"""
|
||||||
|
|
||||||
def __init__(self, host: str = "0.0.0.0", port: int = 8888,
|
def __init__(self, host: str = None, port: int = None,
|
||||||
username: str = "admin", password: str = "admin123"):
|
username: str = None, password: str = None,
|
||||||
self.host = host
|
robot_instance=None):
|
||||||
self.port = port
|
# 加载配置文件
|
||||||
self.username = username
|
self.config = self._load_dashboard_config()
|
||||||
self.password = password
|
|
||||||
|
# 优先使用传入的参数,其次使用配置文件中的参数
|
||||||
|
self.host = host or self.config.get("server", {}).get("host", "0.0.0.0")
|
||||||
|
self.port = port or self.config.get("server", {}).get("port", 8888)
|
||||||
|
self.username = username or self.config.get("auth", {}).get("username", "admin")
|
||||||
|
self.password = password or self.config.get("auth", {}).get("password", "admin123")
|
||||||
|
|
||||||
self.logger = logging.getLogger("DashboardServer")
|
self.logger = logging.getLogger("DashboardServer")
|
||||||
|
self.logger.info(f"Dashboard配置加载完成: 服务器将运行在 {self.host}:{self.port}")
|
||||||
|
|
||||||
# 初始化数据库连接
|
# 如果提供了robot实例,则使用其对象
|
||||||
self._init_database()
|
if robot_instance:
|
||||||
|
self.db_manager = robot_instance.db_manager
|
||||||
|
self.contact_manager = robot_instance.contact_manager
|
||||||
|
# 使用robot的GroupBotManager实例
|
||||||
|
self.gbm = robot_instance.gbm
|
||||||
|
self.logger.info("使用Robot实例的对象进行初始化")
|
||||||
|
else:
|
||||||
|
# 初始化数据库连接
|
||||||
|
self._init_database()
|
||||||
|
# 获取联系人管理器实例
|
||||||
|
self.contact_manager = ContactManager.get_instance()
|
||||||
|
self.logger.info("独立初始化数据库和联系人管理器")
|
||||||
|
|
||||||
# 获取联系人管理器实例
|
|
||||||
self.contact_manager = ContactManager.get_instance()
|
|
||||||
self.app = self._create_app()
|
self.app = self._create_app()
|
||||||
self._stop_event = threading.Event()
|
self._stop_event = threading.Event()
|
||||||
self._server = None # 存储服务器实例
|
self._server = None # 存储服务器实例
|
||||||
|
|
||||||
|
def _load_dashboard_config(self):
|
||||||
|
"""加载Dashboard配置文件"""
|
||||||
|
try:
|
||||||
|
config_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'config.toml')
|
||||||
|
if os.path.exists(config_path):
|
||||||
|
with open(config_path, 'r', encoding='utf-8') as f:
|
||||||
|
return toml.load(f)
|
||||||
|
else:
|
||||||
|
# 如果配置文件不存在,创建默认配置
|
||||||
|
default_config = {
|
||||||
|
"server": {"host": "0.0.0.0", "port": 8888},
|
||||||
|
"auth": {"username": "admin", "password": "admin123"}
|
||||||
|
}
|
||||||
|
with open(config_path, 'w', encoding='utf-8') as f:
|
||||||
|
toml.dump(default_config, f)
|
||||||
|
return default_config
|
||||||
|
except Exception as e:
|
||||||
|
print(f"加载Dashboard配置文件失败: {e}")
|
||||||
|
# 返回默认配置
|
||||||
|
return {
|
||||||
|
"server": {"host": "0.0.0.0", "port": 8888},
|
||||||
|
"auth": {"username": "admin", "password": "admin123"}
|
||||||
|
}
|
||||||
|
|
||||||
def _load_config(self):
|
def _load_config(self):
|
||||||
"""从配置文件加载配置"""
|
"""从配置文件加载配置"""
|
||||||
|
|||||||
@@ -1,19 +0,0 @@
|
|||||||
@echo off
|
|
||||||
cd /d %~dp0
|
|
||||||
echo Starting WeChatRobot Admin Dashboard...
|
|
||||||
|
|
||||||
echo [INFO] Trying to update code from Git...
|
|
||||||
cd /d F:\python\WeChatRobot
|
|
||||||
git pull
|
|
||||||
if %errorlevel% neq 0 (
|
|
||||||
echo [WARNING] Git update failed, continuing with current code...
|
|
||||||
) else (
|
|
||||||
echo [INFO] Code updated successfully!
|
|
||||||
)
|
|
||||||
|
|
||||||
echo [INFO] Installing Python dependencies...
|
|
||||||
pip install -r requirements.txt
|
|
||||||
|
|
||||||
cd /d %~dp0
|
|
||||||
python dashboard_start.py
|
|
||||||
pause
|
|
||||||
@@ -1,80 +0,0 @@
|
|||||||
import os
|
|
||||||
import sys
|
|
||||||
import logging
|
|
||||||
import tomli
|
|
||||||
import argparse
|
|
||||||
|
|
||||||
# 添加项目根目录到系统路径
|
|
||||||
sys.path.append(os.path.abspath(os.path.dirname(os.path.dirname(__file__))))
|
|
||||||
|
|
||||||
from admin.dashboard.server import DashboardServer
|
|
||||||
|
|
||||||
def setup_logging():
|
|
||||||
"""设置日志"""
|
|
||||||
logging.basicConfig(
|
|
||||||
level=logging.INFO,
|
|
||||||
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
|
||||||
handlers=[
|
|
||||||
logging.StreamHandler(),
|
|
||||||
logging.FileHandler(os.path.join(os.path.dirname(__file__), 'dashboard.log'))
|
|
||||||
]
|
|
||||||
)
|
|
||||||
|
|
||||||
def load_config():
|
|
||||||
"""加载TOML配置"""
|
|
||||||
config_path = os.path.join(os.path.dirname(__file__), 'dashboard', 'config.toml')
|
|
||||||
if not os.path.exists(config_path):
|
|
||||||
print(f"配置文件不存在: {config_path}")
|
|
||||||
return None
|
|
||||||
|
|
||||||
try:
|
|
||||||
with open(config_path, 'rb') as f:
|
|
||||||
config = tomli.load(f)
|
|
||||||
return config
|
|
||||||
except Exception as e:
|
|
||||||
print(f"加载配置文件失败: {e}")
|
|
||||||
return None
|
|
||||||
|
|
||||||
def main():
|
|
||||||
"""主函数"""
|
|
||||||
parser = argparse.ArgumentParser(description='启动WeChatRobot管理后台')
|
|
||||||
parser.add_argument('--host', type=str, help='服务器主机地址')
|
|
||||||
parser.add_argument('--port', type=int, help='服务器端口')
|
|
||||||
parser.add_argument('--username', type=str, help='管理员用户名')
|
|
||||||
parser.add_argument('--password', type=str, help='管理员密码')
|
|
||||||
args = parser.parse_args()
|
|
||||||
|
|
||||||
# 设置日志
|
|
||||||
setup_logging()
|
|
||||||
logger = logging.getLogger("Dashboard")
|
|
||||||
|
|
||||||
# 加载配置
|
|
||||||
config = load_config()
|
|
||||||
if not config:
|
|
||||||
logger.error("无法加载配置,使用默认配置")
|
|
||||||
config = {}
|
|
||||||
|
|
||||||
# 命令行参数优先级高于配置文件
|
|
||||||
host = args.host or config.get('host', '0.0.0.0')
|
|
||||||
port = args.port or config.get('port', 8888)
|
|
||||||
username = args.username or config.get('username', 'admin')
|
|
||||||
password = args.password or config.get('password', 'admin123')
|
|
||||||
|
|
||||||
# 检查是否启用
|
|
||||||
if not config.get('enable', True):
|
|
||||||
logger.info("管理后台已禁用,退出程序")
|
|
||||||
return
|
|
||||||
|
|
||||||
# 创建并启动服务器
|
|
||||||
server = DashboardServer(
|
|
||||||
host=host,
|
|
||||||
port=port,
|
|
||||||
username=username,
|
|
||||||
password=password
|
|
||||||
)
|
|
||||||
|
|
||||||
logger.info(f"启动管理后台服务器,访问地址: http://{host}:{port}")
|
|
||||||
server.run()
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
main()
|
|
||||||
15
main.py
15
main.py
@@ -55,6 +55,21 @@ def main(chat_type: int):
|
|||||||
# 让机器人一直跑
|
# 让机器人一直跑
|
||||||
robot.keep_running_and_block_process()
|
robot.keep_running_and_block_process()
|
||||||
|
|
||||||
|
# 在启动Dashboard的部分
|
||||||
|
|
||||||
|
# 启动Dashboard服务器
|
||||||
|
dashboard_server = None
|
||||||
|
try:
|
||||||
|
# 创建Dashboard服务器实例,共享robot对象
|
||||||
|
from admin.dashboard.server import DashboardServer
|
||||||
|
dashboard_server = DashboardServer(robot_instance=robot)
|
||||||
|
|
||||||
|
# 在单独的线程中启动Dashboard服务器
|
||||||
|
dashboard_thread = threading.Thread(target=dashboard_server.run, daemon=True)
|
||||||
|
dashboard_thread.start()
|
||||||
|
robot.LOG.info(f"Dashboard服务器已在 http://{dashboard_server.host}:{dashboard_server.port} 启动")
|
||||||
|
except Exception as e:
|
||||||
|
robot.LOG.error(f"Dashboard服务器启动失败: {e}")
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
parser = ArgumentParser()
|
parser = ArgumentParser()
|
||||||
|
|||||||
Reference in New Issue
Block a user