from flask import Flask, current_app, make_response, request, jsonify, Response from flask_cors import CORS from typing import Dict, Tuple import generic_executor import json app = Flask(__name__) CORS(app) cors = CORS(app, origins=['*']) @app.route('/AccountCreate', methods=['POST']) def account_create() -> Response: data: Dict[str, str] = request.get_json() if not data: return jsonify({"error": "No JSON payload"}), 415 email: str = data.get('email') port: str = data.get('port') print(f'Email received: {email}') print(f'Port received: {port}') generic_executor.account_create(port) return jsonify({'message': 'OK'}), 200 @app.route('/ServerCreate', methods=['POST']) def server_create() -> Response: data: Dict[str, str] = request.get_json() if not data: return jsonify({"error": "No JSON payload"}), 415 port: str = data.get('port') name: str = data.get('name') version: str = data.get('version') print(f'Port received for server creation: {port}') print(f'Server name: {name}') print(f'Server version: {version}') generic_executor.server_create(port, name, version) return jsonify({'message': 'OK'}), 200 @app.route('/ServerDelete', methods=['POST']) def server_delete() -> tuple[Response, int]: data: Dict[str, str] = request.get_json() if not data: return jsonify({"error": "No JSON payload"}), 415 port: str = data.get('port') name: str = data.get('name') print(f'Port received for server deletion: {port}') print(f'Server name to delete: {name}') generic_executor.server_delete(port, name) return jsonify({'message': 'OK'}), 200 @app.route('/AccountDelete', methods=['POST']) def account_delete() -> tuple[Response, int]: data: Dict[str, str] = request.get_json() if not data: return jsonify({"error": "No JSON payload"}), 415 port: str = data.get('port') email: str = data.get('email') print(f'Port received for account deletion: {port}') print(f'Email of account to delete: {email}') generic_executor.account_delete(port) return jsonify({'message': 'OK'}), 200 @app.route('/ServerRun', methods=['POST']) def server_run() -> tuple[Response, int]: data: Dict[str, str] = request.get_json() if not data: return jsonify({"error": "No JSON payload"}), 415 port: str = data.get('port') name: str = data.get('name') print(f'Port received for server launch: {port}') print(f'Server name to launch: {name}') generic_executor.server_run(port, name) return jsonify({'message': 'OK'}), 200 @app.route('/ServerStop', methods=['POST']) def server_stop() -> tuple[Response, int]: data: Dict[str, str] = request.get_json() if not data: return jsonify({"error": "No JSON payload"}), 415 port: str = data.get('port') name: str = data.get('name') print(f'Port received for server shutdown: {port}') print(f'Server name to shut down: {name}') generic_executor.server_stop(port, name) return jsonify({'message': 'OK'}), 200 @app.route('/UpdateProperty', methods=['POST']) def update_property() -> tuple[Response, int]: data: Dict[str, str, str, str] = request.get_json() if not data: return jsonify({"error": "No JSON payload"}), 415 port: str = data.get('port') name: str = data.get('name') prop: str = data.get('property') value: str = data.get('value') print(f'The server {name} at port {port} changed {prop} to {value}') generic_executor.update_property(port, name, prop, value) return jsonify({'message': 'OK'}), 200 @app.route('/Command', methods=['POST']) def command() -> tuple[Response, int]: data: Dict[str, str, str] = request.get_json() if not data: return jsonify({"error": "No JSON payload"}), 415 port: str = data.get('port') _command: str = data.get('command') print(f'Server {port} executed command {_command}') generic_executor.run_command(port, _command) return jsonify({'message': 'OK'}), 200 if __name__ == '__main__': app.run(host='0.0.0.0', port=3005, debug=True)