80 lines
2.4 KiB
Python
80 lines
2.4 KiB
Python
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() |