servii-backend/api.py

128 lines
4.0 KiB
Python
Raw Normal View History

2024-06-14 21:33:55 +00:00
from flask import Flask, current_app, make_response, request, jsonify, Response
from flask_cors import CORS
2024-06-20 16:52:23 +00:00
from typing import Dict, Tuple
2024-06-14 21:33:55 +00:00
import generic_executor
import json
app = Flask(__name__)
CORS(app)
cors = CORS(app, origins=['*'])
2024-06-20 16:57:04 +00:00
2024-06-14 21:33:55 +00:00
@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
2024-06-20 16:57:04 +00:00
2024-06-14 21:33:55 +00:00
email: str = data.get('email')
port: str = data.get('port')
print(f'Email received: {email}')
print(f'Port received: {port}')
2024-06-20 16:57:04 +00:00
generic_executor.account_create(port)
2024-06-14 21:33:55 +00:00
return jsonify({'message': 'OK'}), 200
2024-06-20 16:57:04 +00:00
2024-06-14 21:33:55 +00:00
@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
2024-06-20 16:57:04 +00:00
2024-06-14 21:33:55 +00:00
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}')
2024-06-20 16:57:04 +00:00
generic_executor.server_create(port, name, version)
2024-06-14 21:33:55 +00:00
return jsonify({'message': 'OK'}), 200
2024-06-20 16:57:04 +00:00
2024-06-14 21:33:55 +00:00
@app.route('/ServerDelete', methods=['POST'])
2024-06-20 16:57:04 +00:00
def server_delete() -> tuple[Response, int]:
2024-06-14 21:33:55 +00:00
data: Dict[str, str] = request.get_json()
if not data:
return jsonify({"error": "No JSON payload"}), 415
2024-06-20 16:57:04 +00:00
2024-06-14 21:33:55 +00:00
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}')
2024-06-20 16:57:04 +00:00
generic_executor.server_delete(port, name)
2024-06-14 21:33:55 +00:00
return jsonify({'message': 'OK'}), 200
2024-06-20 16:57:04 +00:00
2024-06-14 21:33:55 +00:00
@app.route('/AccountDelete', methods=['POST'])
2024-06-20 16:57:04 +00:00
def account_delete() -> tuple[Response, int]:
2024-06-14 21:33:55 +00:00
data: Dict[str, str] = request.get_json()
if not data:
return jsonify({"error": "No JSON payload"}), 415
2024-06-20 16:57:04 +00:00
2024-06-14 21:33:55 +00:00
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}')
2024-06-20 16:57:04 +00:00
generic_executor.account_delete(port)
2024-06-14 21:33:55 +00:00
return jsonify({'message': 'OK'}), 200
2024-06-20 16:57:04 +00:00
2024-06-14 21:33:55 +00:00
@app.route('/ServerRun', methods=['POST'])
2024-06-20 16:57:04 +00:00
def server_run() -> tuple[Response, int]:
2024-06-14 21:33:55 +00:00
data: Dict[str, str] = request.get_json()
if not data:
return jsonify({"error": "No JSON payload"}), 415
2024-06-20 16:57:04 +00:00
2024-06-14 21:33:55 +00:00
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}')
2024-06-20 16:57:04 +00:00
generic_executor.server_run(port, name)
2024-06-14 21:33:55 +00:00
return jsonify({'message': 'OK'}), 200
2024-06-20 16:57:04 +00:00
2024-06-14 21:33:55 +00:00
@app.route('/ServerStop', methods=['POST'])
2024-06-20 16:57:04 +00:00
def server_stop() -> tuple[Response, int]:
2024-06-14 21:33:55 +00:00
data: Dict[str, str] = request.get_json()
if not data:
return jsonify({"error": "No JSON payload"}), 415
2024-06-20 16:57:04 +00:00
2024-06-14 21:33:55 +00:00
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}')
2024-06-20 16:57:04 +00:00
generic_executor.server_stop(port, name)
2024-06-14 21:33:55 +00:00
return jsonify({'message': 'OK'}), 200
2024-06-20 16:57:04 +00:00
2024-06-14 21:33:55 +00:00
@app.route('/UpdateProperty', methods=['POST'])
2024-06-20 16:57:04 +00:00
def update_property() -> tuple[Response, int]:
2024-06-14 21:33:55 +00:00
data: Dict[str, str, str, str] = request.get_json()
if not data:
return jsonify({"error": "No JSON payload"}), 415
2024-06-20 16:57:04 +00:00
2024-06-14 21:33:55 +00:00
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}')
2024-06-20 16:57:04 +00:00
generic_executor.update_property(port, name, prop, value)
2024-06-14 21:33:55 +00:00
return jsonify({'message': 'OK'}), 200
2024-06-20 16:57:04 +00:00
2024-06-14 21:33:55 +00:00
@app.route('/Command', methods=['POST'])
2024-06-20 16:52:23 +00:00
def command() -> tuple[Response, int]:
2024-06-14 21:33:55 +00:00
data: Dict[str, str, str] = request.get_json()
if not data:
return jsonify({"error": "No JSON payload"}), 415
2024-06-20 16:57:04 +00:00
2024-06-14 21:33:55 +00:00
port: str = data.get('port')
2024-06-20 16:52:23 +00:00
_command: str = data.get('command')
print(f'Server {port} executed command {_command}')
2024-06-20 16:57:04 +00:00
generic_executor.run_command(port, _command)
2024-06-14 21:33:55 +00:00
return jsonify({'message': 'OK'}), 200
if __name__ == '__main__':
app.run(host='0.0.0.0', port=3005, debug=True)