from flask import Flask, request, jsonify, abort import os app = Flask(__name__) TOKENS_FILE = "tokens.txt" CONFIG_DIR = "configs" @app.route('/getconfig', methods=['POST']) def get_config(): # Überprüfe, ob die erforderlichen Daten im Request vorhanden sind if not request.json or 'token' not in request.json or 'data' not in request.json: abort(400, 'Bad request. JSON data with "token" and "data" is required.') token = request.json['token'] data = request.json['data'] # Überprüfe, ob der Token gültig ist if not is_valid_token(token): abort(401, 'Unauthorized. Invalid token.') # Speichere die Daten in einem Datei für das entsprechende Token save_data(token, data) # Lade die Konfigurationsdatei für das Token config = load_config(token) return jsonify(config) def is_valid_token(token): # Überprüfe, ob der Token in der tokens.txt Datei vorhanden ist with open(TOKENS_FILE, 'r') as file: tokens = file.read().splitlines() return token in tokens def save_data(token, data): # Erstelle das Verzeichnis für die Konfigurationsdateien, falls es nicht existiert if not os.path.exists(CONFIG_DIR): os.makedirs(CONFIG_DIR) # Speichere die Daten in einer Datei für das entsprechende Token with open(os.path.join(CONFIG_DIR, f'{token}.json'), 'w') as file: file.write(jsonify(data)) def load_config(token): config_file = os.path.join(CONFIG_DIR, f'{token}.json') # Lade die Konfigurationsdatei für das Token if os.path.exists(config_file): with open(config_file, 'r') as file: return json.load(file) else: return {} if __name__ == '__main__': app.run(debug=True)