servii-backend/api.py

170 lines
6.0 KiB
Python
Raw Normal View History

2024-06-21 18:44:33 +00:00
import http
2024-06-23 22:38:07 +00:00
from enum import Enum
from typing import Dict, Optional
2024-06-21 18:44:33 +00:00
from flask import Flask, Response, jsonify, request
2024-06-14 21:33:55 +00:00
from flask_cors import CORS
2024-06-21 18:44:33 +00:00
2024-06-14 21:33:55 +00:00
import generic_executor
app = Flask(__name__)
CORS(app)
cors = CORS(app, origins=['*'])
2024-06-20 16:57:04 +00:00
2024-06-23 22:38:07 +00:00
class ActionType(Enum):
ACCOUNT_CREATE = '/AccountCreate'
SERVER_CREATE = '/ServerCreate'
2024-06-21 18:44:33 +00:00
def generic_response_maker(status_code: http.HTTPStatus) -> tuple[Response, int]:
match status_code:
case http.HTTPStatus.CREATED:
message = jsonify({'message': 'Creation successful.'})
case http.HTTPStatus.INTERNAL_SERVER_ERROR:
message = jsonify({'message': 'Internal Server Error.'})
case http.HTTPStatus.NO_CONTENT:
message = jsonify({'message': 'Deletion successful.'})
case http.HTTPStatus.ACCEPTED:
message = jsonify({'message': 'Action successful.'})
case http.HTTPStatus.BAD_REQUEST:
message = jsonify({'message': 'Bad Request, the property you tried to modify is not valid.'})
case http.HTTPStatus.NOT_FOUND:
message = jsonify({'message': 'Server not found.'})
case http.HTTPStatus.UNSUPPORTED_MEDIA_TYPE:
message = jsonify({'message': 'Unsupported Media Type / No JSON payload'})
case http.HTTPStatus.OK:
message = jsonify({'message': 'Success.'})
case _:
status_code = http.HTTPStatus.BAD_GATEWAY
message = jsonify({'message': 'Bad Gateway.'})
return message, status_code.value
2024-06-23 22:38:07 +00:00
def parse_and_validate_request(required_keys: set) -> Optional[dict]:
data = request.get_json()
if not data:
raise Exception("Empty request body.")
missing_keys = required_keys - set(data.keys())
if missing_keys:
raise Exception("Missing keys: {}".format(missing_keys))
return data
2024-06-14 21:33:55 +00:00
@app.route('/AccountCreate', methods=['POST'])
2024-06-21 18:44:33 +00:00
def account_create() -> tuple[Response, int]:
2024-06-14 21:33:55 +00:00
data: Dict[str, str] = request.get_json()
if not data:
2024-06-21 18:44:33 +00:00
return generic_response_maker(http.HTTPStatus.UNSUPPORTED_MEDIA_TYPE)
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-21 18:44:33 +00:00
status: http.HTTPStatus = generic_executor.account_create(port)
return generic_response_maker(status)
2024-06-14 21:33:55 +00:00
2024-06-20 16:57:04 +00:00
2024-06-14 21:33:55 +00:00
@app.route('/ServerCreate', methods=['POST'])
2024-06-21 18:44:33 +00:00
def server_create() -> tuple[Response, int]:
2024-06-14 21:33:55 +00:00
data: Dict[str, str] = request.get_json()
if not data:
2024-06-21 18:44:33 +00:00
return generic_response_maker(http.HTTPStatus.UNSUPPORTED_MEDIA_TYPE)
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-21 18:44:33 +00:00
status: http.HTTPStatus = generic_executor.server_create(port, name, version)
return generic_response_maker(status)
2024-06-14 21:33:55 +00:00
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:
2024-06-21 18:44:33 +00:00
return generic_response_maker(http.HTTPStatus.UNSUPPORTED_MEDIA_TYPE)
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-21 18:44:33 +00:00
status: http.HTTPStatus = generic_executor.server_delete(port, name)
return generic_response_maker(status)
2024-06-14 21:33:55 +00:00
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:
2024-06-21 18:44:33 +00:00
return generic_response_maker(http.HTTPStatus.UNSUPPORTED_MEDIA_TYPE)
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-21 18:44:33 +00:00
status: http.HTTPStatus = generic_executor.account_delete(port)
return generic_response_maker(status)
2024-06-14 21:33:55 +00:00
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:
2024-06-21 18:44:33 +00:00
return generic_response_maker(http.HTTPStatus.UNSUPPORTED_MEDIA_TYPE)
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-21 18:44:33 +00:00
status: http.HTTPStatus = generic_executor.server_run(port, name)
return generic_response_maker(status)
2024-06-14 21:33:55 +00:00
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:
2024-06-21 18:44:33 +00:00
return generic_response_maker(http.HTTPStatus.UNSUPPORTED_MEDIA_TYPE)
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-21 18:44:33 +00:00
status: http.HTTPStatus = generic_executor.server_stop(port, name)
return generic_response_maker(status)
2024-06-14 21:33:55 +00:00
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:
2024-06-21 18:44:33 +00:00
return generic_response_maker(http.HTTPStatus.UNSUPPORTED_MEDIA_TYPE)
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')
2024-06-21 18:44:33 +00:00
status: http.HTTPStatus = generic_executor.update_property(port, name, prop, value)
2024-06-14 21:33:55 +00:00
print(f'The server {name} at port {port} changed {prop} to {value}')
2024-06-21 18:44:33 +00:00
return generic_response_maker(status)
2024-06-14 21:33:55 +00:00
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:
2024-06-21 18:44:33 +00:00
return generic_response_maker(http.HTTPStatus.UNSUPPORTED_MEDIA_TYPE)
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-21 18:44:33 +00:00
status: http.HTTPStatus = generic_executor.run_command(port, _command)
return generic_response_maker(status)
2024-06-14 21:33:55 +00:00
if __name__ == '__main__':
app.run(host='0.0.0.0', port=3005, debug=True)