[~] New /UpdateProperties route

Signed-off-by: Charles Le Maux <charles.le-maux@epitech.eu>
This commit is contained in:
Charles Le Maux 2024-07-08 21:05:07 +01:00
parent 02692d3dac
commit 828010de25
4 changed files with 29 additions and 10 deletions

View File

@ -12,6 +12,7 @@
Port: <label for="accountPort"></label><input type="number" id="accountPort"><br>
Name: <label for="serverName"></label><input type="text" id="serverName"><br>
Version: <label for="serverVersion"></label><input type="text" id="serverVersion"><br>
Framework: <label for="serverVersion"></label><input type="text" id="serverFramework"><br>
<button type="button" class="actionButton" data-action="AccountCreate">Create Account</button>
<button type="button" class="actionButton" data-action="AccountDelete">Delete Account</button>
<button type="button" class="actionButton" data-action="ServerCreate">Create Server</button>
@ -25,7 +26,7 @@
<form id="updatePropertyForm">
Property: <label for="update_property"></label><input type="text" id="update_property"><br>
Value: <label for="update_value"></label><input type="text" id="update_value"><br>
<button type="button" class="actionButton" data-action="UpdateProperty">Update Property</button>
<button type="button" class="actionButton" data-action="UpdateProperties">Update Property</button>
</form>
<h2>Send Command</h2>
@ -47,8 +48,8 @@ document.addEventListener('DOMContentLoaded', () => {
buttons.forEach(button => {
button.addEventListener('click', async event => {
const action = button.dataset.action;
const token = "CJal6RJnc7Po2vxdj3DYVhrHZQL2";
const framework = "paper";
const token = "q8QbdYWr1gX32cs1q8P3rrpy3vk1";
const framework = document.getElementById('serverFramework').value;
const subdomain = document.getElementById('subdomain').value;
const email = document.getElementById('accountEmail').value;
const port = document.getElementById('accountPort').value;
@ -57,6 +58,7 @@ document.addEventListener('DOMContentLoaded', () => {
const prop = document.getElementById('update_property').value;
const value = document.getElementById('update_value').value;
const command = document.getElementById('command').value;
const props = [[prop, value], ["max-players", "27"]];
let data = {};
switch(action) {
case 'FetchServers':
@ -80,8 +82,8 @@ document.addEventListener('DOMContentLoaded', () => {
case 'ServerStop':
data = {port, name, token};
break;
case 'UpdateProperty':
data = {port, name, prop, value, token};
case 'UpdateProperties':
data = {port, name, props, value, token};
break;
case 'Command':
data = {port, name, command, token};
@ -98,7 +100,7 @@ document.addEventListener('DOMContentLoaded', () => {
});
function sendRequest(endpoint, payload) {
return fetch(`http://localhost:3000/${endpoint}`, {
return fetch(`http://127.0.0.1:3000/${endpoint}`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'

4
app.py
View File

@ -42,7 +42,7 @@ def generic_response_maker(status_code: http.HTTPStatus, _message: str = None) -
'''
valid, user_id = firebase_manager.verify_jwt_token(data['token'])
TODO : replace 52 by the given statement.
TODO : replace 53 by the given statement.
'''
@ -86,7 +86,7 @@ route_handlers = {
'AccountDelete': generic_executor.account_delete,
'ServerRun': generic_executor.server_run,
'ServerStop': generic_executor.server_stop,
'UpdateProperty': generic_executor.update_property,
'UpdateProperties': generic_executor.update_properties,
'Command': generic_executor.run_command,
}

View File

@ -166,11 +166,12 @@ def server_stop(user: UserRecord, name: str) -> tuple[HTTPStatus, str or None]:
except Exception as e:
if port:
mc_manager.stop_server_forcefully(port)
return HTTPStatus.OK, f"Successfully stopped server {name}."
return HTTPStatus.INTERNAL_SERVER_ERROR, f"Error occurred when stopping server {name}."
def update_property(user: UserRecord, name: str, prop: str, value: str) -> tuple[HTTPStatus, str or None]:
property_file_path: str = f"users/{user.uid}/{name}/server.properties"
def update_property(uid: str, name: str, prop: str, value: str) -> tuple[HTTPStatus, str or None]:
property_file_path: str = f"users/{uid}/{name}/server.properties"
try:
file_manager.update_server_property(property_file_path, prop, value)
return HTTPStatus.OK, f"Successfully set '{prop}' to '{value}'."
@ -183,6 +184,19 @@ def update_property(user: UserRecord, name: str, prop: str, value: str) -> tuple
return HTTPStatus.INTERNAL_SERVER_ERROR, f"Unhandled error: {type(e).__name__}, {str(e)}"
def update_properties(user: UserRecord, name: str, props: list[tuple[str, str]]) -> tuple[HTTPStatus, str or None]:
errors: list[str] = []
for prop, value in props:
if prop not in MinecraftServerManager.allowed_properties:
return HTTPStatus.FORBIDDEN, f"Property '{prop}' not allowed."
status, message = update_property(uid=user.uid, name=name, prop=prop, value=value)
if status != HTTPStatus.OK:
errors.append(message)
if len(errors) > 0:
return HTTPStatus.IM_A_TEAPOT, errors
return HTTPStatus.OK, f"Successfully updated server {name}."
def run_command(user: UserRecord, command: str, name: str) -> tuple[HTTPStatus, str or None]:
try:
port = firebase_manager.get_server_port(user.uid)

View File

@ -3,6 +3,9 @@ import shlex
class MinecraftServerManager:
allowed_properties: list[str] = ["difficulty", "gamemode", "force-gamemode", "hardcore", "generate-structures",
"motd", "pvp", "online-mode", "max-players"]
def __init__(self):
self.servers: dict = {}
self.servers_count: int = 0