mkcontrol-app/app.py

82 lines
2.5 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# app.py
import os
import json
import logging
from flask import Flask, render_template, redirect, url_for, send_from_directory, request, jsonify
app = Flask(__name__)
# Konfiguration
app.config['CONFIG_DIR'] = 'configs'
os.makedirs(app.config['CONFIG_DIR'], exist_ok=True)
# Logging
logging.basicConfig(
filename='app.log',
level=logging.INFO,
format='%(asctime)s | %(levelname)-8s | %(message)s',
datefmt='%Y-%m-%d %H:%M:%S'
)
logger = logging.getLogger(__name__)
def load_configs():
"""Liest alle .json Dateien aus dem configs-Ordner"""
configs = []
if not os.path.exists(app.config['CONFIG_DIR']):
return configs
for filename in os.listdir(app.config['CONFIG_DIR']):
if filename.lower().endswith('.json'):
path = os.path.join(app.config['CONFIG_DIR'], filename)
try:
with open(path, 'r', encoding='utf-8') as f:
data = json.load(f)
data['filename'] = filename
# Optional: name fallback, falls nicht vorhanden
data.setdefault('name', filename.replace('.json', '').replace('_', ' ').title())
configs.append(data)
except Exception as e:
logger.error(f"Fehler beim Laden von {filename}: {e}")
return sorted(configs, key=lambda x: x.get('name', ''))
@app.route('/')
def index():
configs = load_configs()
return render_template('index.html', configs=configs)
@app.route('/load_config/<filename>')
def load_config(filename):
# Später: Config in Session speichern oder global (für Entwicklung erstmal redirect)
# Hier nur redirect zur Steuerseite echte Logik kommt später
path = os.path.join(app.config['CONFIG_DIR'], filename)
if not os.path.exists(path):
return "Konfigurationsdatei nicht gefunden", 404
# Für den Moment nur redirect später speichern wir current_config
return redirect(url_for('control_page', filename=filename))
@app.route('/control/<filename>')
def control_page(filename):
# Später: Config laden und übergeben
# Aktuell nur Dummy-Template
return render_template('control.html', filename=filename)
@app.route('/configs/<path:filename>')
def serve_config_file(filename):
"""Bilder und andere Dateien aus configs/ ausliefern"""
return send_from_directory(app.config['CONFIG_DIR'], filename)
@app.route('/admin')
def admin():
return render_template('admin.html')
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5050, debug=True)