gate-editor-tool/script.py
2024-12-15 00:30:04 +01:00

50 lines
1.4 KiB
Python

import yaml
from flask import Flask, request, jsonify
app = Flask(__name__)
CONFIG_FILE = "config.yml"
def load_config():
with open(CONFIG_FILE, "r") as f:
return yaml.safe_load(f)
def save_config(config):
with open(CONFIG_FILE, "w") as f:
yaml.dump(config, f, default_flow_style=False)
@app.route('/', methods=['POST'])
def api_endpoint():
try:
data = request.get_json()
action = data.get('action')
config = load_config()
if action == "ListRoutes":
return jsonify({"routes": config['config']['lite']['routes']})
elif action == "UpdateRoutes":
new_routes = data.get('routes')
if not isinstance(new_routes, list):
return jsonify({"error": "Invalid routes format"}), 400
config['config']['lite']['routes'] = new_routes
save_config(config)
return jsonify({"message": "Routes updated successfully"})
else:
return jsonify({"error": "Invalid action"}), 400
except FileNotFoundError:
return jsonify({"error": "Config file not found"}), 500
except yaml.YAMLError as e:
return jsonify({"error": f"Error parsing YAML: {e}"}), 500
except Exception as e:
return jsonify({"error": f"An error occurred: {e}"}), 500
if __name__ == "__main__":
app.run(debug=True, host='0.0.0.0', port=5000)