macroboard/app.py
Kumi 27bc11bcdb
feat: add Flask-based macro command app
Introduces a Flask web application allowing execution of shell commands via a macro interface.

- Includes a REST API with API key authentication to secure command execution.
- Provides basic routes for config retrieval and command execution.
- Interfaces with a dynamically generated HTML front-end using JavaScript to communicate with the backend.
- Utilizes a simple JSON configuration file to define available macro commands.
- Adds `.gitignore` to exclude virtual environment files, compiled Python files, and configuration files from version control.

Enables a platform to manage system operations efficiently and securely through a web-based interface.
2024-10-23 12:36:40 +02:00

40 lines
1.3 KiB
Python

from flask import Flask, jsonify, request, render_template
import subprocess
import json
app = Flask(__name__)
def load_config(config_path='config.json'):
with open(config_path, 'r') as file:
return json.load(file)
config_data = load_config()
API_KEY = config_data.get("api_key", "")
@app.route('/')
def index():
return render_template('index.html')
@app.route('/config', methods=['GET'])
def get_config():
# exclude API key for security reasons if needed
config_without_key = {k: v for k, v in config_data.items() if k != "api_key"}
return jsonify(config_without_key)
@app.route('/execute', methods=['POST'])
def execute_command():
api_key = request.headers.get('X-API-Key')
if api_key == API_KEY:
action = request.json.get('action')
# Find the associated command from the configuration
for button in config_data["buttons"]:
if button["command"] == action:
# Security: Ensure command validation to prevent injections
subprocess.run(action, shell=True)
return jsonify(success=True, message=f"Executed command: {action}")
return jsonify(success=False, message='Unknown action'), 400
else:
return jsonify(success=False, message='Unauthorized'), 401
if __name__ == "__main__":
app.run(host='0.0.0.0', port=5000, debug=True)