servii-backend/api.py

120 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
from typing import Dict
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.AccountCreate(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.ServerCreate(port, name, version)
return jsonify({'message': 'OK'}), 200
@app.route('/ServerDelete', methods=['POST'])
def server_delete() -> 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')
print(f'Port received for server deletion: {port}')
print(f'Server name to delete: {name}')
generic_executor.ServerDelete(port, name)
return jsonify({'message': 'OK'}), 200
@app.route('/AccountDelete', methods=['POST'])
def account_delete() -> Response:
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.AccountDelete(port)
return jsonify({'message': 'OK'}), 200
@app.route('/ServerRun', methods=['POST'])
def server_run() -> 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')
print(f'Port received for server launch: {port}')
print(f'Server name to launch: {name}')
generic_executor.ServerRun(port, name)
return jsonify({'message': 'OK'}), 200
@app.route('/ServerStop', methods=['POST'])
def server_stop() -> 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')
print(f'Port received for server shutdown: {port}')
print(f'Server name to shut down: {name}')
generic_executor.ServerStop(port, name)
return jsonify({'message': 'OK'}), 200
@app.route('/UpdateProperty', methods=['POST'])
def update_property() -> Response:
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.UpdateProperty(port, name, prop, value)
return jsonify({'message': 'OK'}), 200
@app.route('/Command', methods=['POST'])
def command() -> Response:
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.RunCommand(port, command)
return jsonify({'message': 'OK'}), 200
if __name__ == '__main__':
app.run(host='0.0.0.0', port=3005, debug=True)