[~] Firestore : added servers running states

Signed-off-by: Charles Le Maux <charles.le-maux@epitech.eu>
This commit is contained in:
Charles Le Maux 2024-06-29 22:54:47 +01:00
parent ed9ca9aba3
commit 858343637f
3 changed files with 16 additions and 1 deletions

View File

@ -100,7 +100,7 @@ document.addEventListener('DOMContentLoaded', () => {
}); });
function sendRequest(endpoint, payload) { function sendRequest(endpoint, payload) {
return fetch(`https://176.165.62.226:3000/${endpoint}`, { return fetch(`http://localhost:3000/${endpoint}`, {
method: 'POST', method: 'POST',
headers: { headers: {
'Content-Type': 'application/json' 'Content-Type': 'application/json'

View File

@ -134,6 +134,12 @@ def delete_user(user_id: str):
user_ref.delete() user_ref.delete()
def update_server_running_state(user_id: str, server_name: str, state: bool):
user_ref = firestore_database.collection('users').document(user_id).collection('servers').document(server_name)
if user_ref.get().get('running') != state:
user_ref.update({'running': state})
def log_exception_to_firestore(exception: Exception = None, user_id: str = None, data: dict = None): def log_exception_to_firestore(exception: Exception = None, user_id: str = None, data: dict = None):
new_id: str = datetime.now().strftime('%Y-%m-%d %H:%M:%S %Z%z') new_id: str = datetime.now().strftime('%Y-%m-%d %H:%M:%S %Z%z')
log_entry = { log_entry = {

View File

@ -137,6 +137,10 @@ def server_run(user: UserRecord, name: str) -> tuple[HTTPStatus, str or None]:
if server_id is None: if server_id is None:
return HTTPStatus.OK, f"You cannot run multiples instances at this time." return HTTPStatus.OK, f"You cannot run multiples instances at this time."
mc_manager.servers[server_id]['port'] = int(port) mc_manager.servers[server_id]['port'] = int(port)
try:
firebase_manager.update_server_running_state(user_id, name, True)
except Exception as e:
return HTTPStatus.INTERNAL_SERVER_ERROR, f"Error updating server {name}'s running state in database. {type(e).__name__}{str(e)}."
return HTTPStatus.ACCEPTED, f"Successfully started server {name}." return HTTPStatus.ACCEPTED, f"Successfully started server {name}."
except Exception as e: except Exception as e:
print(f"Error when running server: {e}") print(f"Error when running server: {e}")
@ -145,8 +149,13 @@ def server_run(user: UserRecord, name: str) -> tuple[HTTPStatus, str or None]:
def server_stop(user: UserRecord, name: str) -> tuple[HTTPStatus, str or None]: def server_stop(user: UserRecord, name: str) -> tuple[HTTPStatus, str or None]:
port = None port = None
user_id = user.uid
try: try:
port = firebase_manager.get_server_port(user.uid) port = firebase_manager.get_server_port(user.uid)
try:
firebase_manager.update_server_running_state(user_id, name, False)
except Exception:
pass
if port is None: if port is None:
return HTTPStatus.NOT_FOUND, f"Server {name} not found in firestore." return HTTPStatus.NOT_FOUND, f"Server {name} not found in firestore."
exists: bool = mc_manager.stop_server(port) exists: bool = mc_manager.stop_server(port)