[~] 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) {
return fetch(`https://176.165.62.226:3000/${endpoint}`, {
return fetch(`http://localhost:3000/${endpoint}`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'

View File

@ -134,6 +134,12 @@ def delete_user(user_id: str):
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):
new_id: str = datetime.now().strftime('%Y-%m-%d %H:%M:%S %Z%z')
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:
return HTTPStatus.OK, f"You cannot run multiples instances at this time."
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}."
except Exception as 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]:
port = None
user_id = user.uid
try:
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:
return HTTPStatus.NOT_FOUND, f"Server {name} not found in firestore."
exists: bool = mc_manager.stop_server(port)