🎯 重构目标: - 将 3700+ 行单文件拆分为多个模块 - 清晰的职责分离,便于维护和扩展 📁 模块结构: - config.py: 配置常量和路径 - database.py: 数据库操作 - utils.py: 工具函数(日志、进程、cron等) - routes_cron.py: Cron管理API - routes_system.py: 系统监控API - routes_config.py: 配置管理API - routes_email.py: 邮件通知API - routes_project.py: 项目管理API - templates.py: HTML模板 - app.py: 主入口文件 ✨ 优势: - 易于维护:每个模块职责明确 - 易于扩展:新增功能只需添加对应模块 - 易于测试:模块独立,便于单元测试 - 易于理解:新人能快速上手 📝 更新 README.md,详细说明项目结构和API接口
150 lines
5.2 KiB
Python
150 lines
5.2 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
配置管理 API 路由
|
|
"""
|
|
|
|
import os
|
|
import json
|
|
import shutil
|
|
from datetime import datetime
|
|
from flask import jsonify, request
|
|
from config import PROJECTS_FILE, ALERT_CONFIG_FILE
|
|
from utils import log_message
|
|
|
|
|
|
def load_full_config():
|
|
"""加载完整配置(合并项目和预警配置)"""
|
|
# 加载 projects.json 完整数据
|
|
try:
|
|
with open(PROJECTS_FILE, 'r', encoding='utf-8') as f:
|
|
projects_data = json.load(f)
|
|
except:
|
|
projects_data = {'server': {}, 'projects': [], 'external_services': []}
|
|
|
|
# 加载预警配置
|
|
alert_config = {}
|
|
if os.path.exists(ALERT_CONFIG_FILE):
|
|
try:
|
|
with open(ALERT_CONFIG_FILE, 'r', encoding='utf-8') as f:
|
|
alert_config = json.load(f)
|
|
except:
|
|
pass
|
|
|
|
# 合并配置
|
|
config = {
|
|
"version": "1.0",
|
|
"exported_at": datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
|
|
"server": projects_data.get('server', {}),
|
|
"projects": projects_data.get('projects', []),
|
|
"external_services": projects_data.get('external_services', []),
|
|
"web_settings": alert_config.get('web_settings', {}),
|
|
"alerts": alert_config.get('thresholds', {})
|
|
}
|
|
return config
|
|
|
|
|
|
def save_full_config(config):
|
|
"""保存完整配置"""
|
|
# 保存 projects 部分
|
|
projects_data = {
|
|
"server": config.get('server', {}),
|
|
"projects": config.get('projects', []),
|
|
"external_services": config.get('external_services', [])
|
|
}
|
|
with open(PROJECTS_FILE, 'w', encoding='utf-8') as f:
|
|
json.dump(projects_data, f, ensure_ascii=False, indent=2)
|
|
|
|
# 保存预警配置和网页设置
|
|
alert_config = {
|
|
"web_settings": config.get('web_settings', {}),
|
|
"thresholds": config.get('alerts', {})
|
|
}
|
|
with open(ALERT_CONFIG_FILE, 'w', encoding='utf-8') as f:
|
|
json.dump(alert_config, f, ensure_ascii=False, indent=2)
|
|
|
|
return True
|
|
|
|
|
|
def register_config_routes(app):
|
|
"""注册配置管理相关路由"""
|
|
|
|
@app.route('/api/config/export', methods=['GET'])
|
|
def api_config_export():
|
|
"""导出配置文件"""
|
|
try:
|
|
config = load_full_config()
|
|
return jsonify(config)
|
|
except Exception as e:
|
|
return jsonify({'error': str(e)}), 500
|
|
|
|
@app.route('/api/config/import', methods=['POST'])
|
|
def api_config_import():
|
|
"""导入配置文件"""
|
|
try:
|
|
config = request.get_json()
|
|
if not config:
|
|
return jsonify({'error': '无效的配置数据'}), 400
|
|
|
|
# 验证配置结构
|
|
required_keys = ['server', 'projects']
|
|
for key in required_keys:
|
|
if key not in config:
|
|
return jsonify({'error': f'缺少必要字段: {key}'}), 400
|
|
|
|
# 备份当前配置
|
|
backup_time = datetime.now().strftime('%Y%m%d_%H%M%S')
|
|
if os.path.exists(PROJECTS_FILE):
|
|
shutil.copy(PROJECTS_FILE, f'{PROJECTS_FILE}.bak.{backup_time}')
|
|
if os.path.exists(ALERT_CONFIG_FILE):
|
|
shutil.copy(ALERT_CONFIG_FILE, f'{ALERT_CONFIG_FILE}.bak.{backup_time}')
|
|
|
|
# 保存新配置
|
|
save_full_config(config)
|
|
|
|
log_message(f"配置导入成功: {len(config.get('projects', []))} 个项目")
|
|
|
|
return jsonify({
|
|
'message': '配置导入成功',
|
|
'projects_count': len(config.get('projects', [])),
|
|
'services_count': len(config.get('external_services', []))
|
|
})
|
|
except Exception as e:
|
|
return jsonify({'error': str(e)}), 500
|
|
|
|
@app.route('/api/web-settings', methods=['GET'])
|
|
def api_get_web_settings():
|
|
"""获取网页设置"""
|
|
try:
|
|
if os.path.exists(ALERT_CONFIG_FILE):
|
|
with open(ALERT_CONFIG_FILE, 'r', encoding='utf-8') as f:
|
|
config = json.load(f)
|
|
web_settings = config.get('web_settings', {})
|
|
return jsonify(web_settings)
|
|
return jsonify({'external_ip': '121.40.164.32', 'refresh_interval': 30})
|
|
except Exception as e:
|
|
return jsonify({'error': str(e)}), 500
|
|
|
|
@app.route('/api/web-settings', methods=['POST'])
|
|
def api_save_web_settings():
|
|
"""保存网页设置"""
|
|
try:
|
|
settings = request.get_json()
|
|
|
|
# 加载现有配置
|
|
existing_config = {}
|
|
if os.path.exists(ALERT_CONFIG_FILE):
|
|
try:
|
|
with open(ALERT_CONFIG_FILE, 'r', encoding='utf-8') as f:
|
|
existing_config = json.load(f)
|
|
except:
|
|
pass
|
|
|
|
# 更新网页设置
|
|
existing_config['web_settings'] = settings
|
|
|
|
with open(ALERT_CONFIG_FILE, 'w', encoding='utf-8') as f:
|
|
json.dump(existing_config, f, ensure_ascii=False, indent=2)
|
|
|
|
return jsonify({'message': '保存成功'})
|
|
except Exception as e:
|
|
return jsonify({'error': str(e)}), 500 |