ijsbreker/server.py

68 lines
2 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
"""
Flask server voor IJsbreker spel
Serveert statische bestanden en biedt API endpoints voor config management
"""
from flask import Flask, send_from_directory, request, jsonify
from flask_cors import CORS
import json
import os
app = Flask(__name__)
CORS(app) # Enable CORS voor alle routes
CONFIG_FILE = 'config.json'
# Serve static files
@app.route('/')
def serve_index():
return send_from_directory('.', 'index.html')
@app.route('/<path:path>')
def serve_static(path):
if os.path.exists(path):
return send_from_directory('.', path)
return "File not found", 404
# API endpoint om config te laden
@app.route('/api/config', methods=['GET'])
def get_config():
try:
with open(CONFIG_FILE, 'r', encoding='utf-8') as f:
config = json.load(f)
return jsonify(config)
except Exception as e:
return jsonify({'error': str(e)}), 500
# API endpoint om config op te slaan
@app.route('/api/save-config', methods=['POST'])
def save_config():
try:
data = request.json
# Validatie: check of stellingen array bestaat
if 'stellingen' not in data:
return jsonify({'error': 'Geen stellingen gevonden'}), 400
# Filter lege stellingen eruit
data['stellingen'] = [
s for s in data['stellingen']
if s.get('links', '').strip() or s.get('rechts', '').strip()
]
# Schrijf naar config.json met mooie formatting
with open(CONFIG_FILE, 'w', encoding='utf-8') as f:
json.dump(data, f, indent=2, ensure_ascii=False)
return jsonify({'success': True, 'message': 'Config opgeslagen!'})
except Exception as e:
return jsonify({'error': str(e)}), 500
if __name__ == '__main__':
print("🎯 IJsbreker Server gestart!")
print("📝 Editor: http://localhost:8000/editor.html")
print("🎮 Spel: http://localhost:8000/")
print("\nDruk Ctrl+C om te stoppen")
app.run(debug=True, host='0.0.0.0', port=8000)