mirror of
https://github.com/hubHarmony/servii-backend.git
synced 2024-11-17 21:40:31 +00:00
Merge branch 'master' into query-rcon-update
This commit is contained in:
commit
30c2ed6a2e
@ -100,7 +100,7 @@ document.addEventListener('DOMContentLoaded', () => {
|
||||
});
|
||||
|
||||
function sendRequest(endpoint, payload) {
|
||||
return fetch(`https://www.servii.fr/api/${endpoint}`, {
|
||||
return fetch(`http://127.0.0.1:3000/${endpoint}`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
|
3
app.py
3
app.py
@ -1,5 +1,6 @@
|
||||
import http
|
||||
import inspect
|
||||
from typing import Union
|
||||
|
||||
from flask import (Blueprint, Flask, Response, jsonify, request)
|
||||
from flask_cors import CORS
|
||||
@ -62,7 +63,7 @@ def authenticate_request(data: dict):
|
||||
return user
|
||||
|
||||
|
||||
def parse_and_validate_request(parameters: [str]) -> list[str] or None:
|
||||
def parse_and_validate_request(parameters: list[str]) -> Union[list[str], None]:
|
||||
args = []
|
||||
data = request.get_json()
|
||||
if not data:
|
||||
|
@ -1,4 +1,5 @@
|
||||
from datetime import datetime
|
||||
from typing import Union
|
||||
|
||||
import firebase_admin
|
||||
import jwt
|
||||
@ -36,7 +37,7 @@ def verify_jwt_token(token):
|
||||
return False, None
|
||||
|
||||
|
||||
def fetch_port() -> int or None:
|
||||
def fetch_port() -> Union[int, None]:
|
||||
servers_ref = firestore_database.collection("users")
|
||||
query = servers_ref.order_by("port", direction="DESCENDING").limit(1)
|
||||
highest_port_doc = next(query.stream(), None)
|
||||
@ -73,7 +74,7 @@ def get_user_field(user_id, field_name):
|
||||
return None
|
||||
|
||||
|
||||
def get_server_port(user_id: str) -> int or None:
|
||||
def get_server_port(user_id: str) -> Union[int, None]:
|
||||
try:
|
||||
servers_ref = firestore_database.collection('users').document(user_id)
|
||||
server_doc = servers_ref.get()
|
||||
|
@ -1,4 +1,5 @@
|
||||
from http import HTTPStatus
|
||||
from typing import Union
|
||||
|
||||
from cloudflare.types.dns import SRVRecord
|
||||
from firebase_admin.auth import UserRecord
|
||||
@ -11,7 +12,7 @@ from server_mc_manager import MinecraftServerManager
|
||||
mc_manager: MinecraftServerManager = MinecraftServerManager()
|
||||
|
||||
|
||||
def set_subdomain(user: UserRecord, subdomain: str) -> tuple[HTTPStatus, str or None]:
|
||||
def set_subdomain(user: UserRecord, subdomain: str) -> tuple[HTTPStatus, Union[str, None]]:
|
||||
user_id: str = user.uid
|
||||
store = firebase_manager.firestore_database
|
||||
_users = store.collection("users")
|
||||
@ -42,7 +43,7 @@ def set_subdomain(user: UserRecord, subdomain: str) -> tuple[HTTPStatus, str or
|
||||
return HTTPStatus.FORBIDDEN, str(e)
|
||||
|
||||
|
||||
def fetch_servers(user: UserRecord) -> tuple[HTTPStatus, str or list]:
|
||||
def fetch_servers(user: UserRecord) -> tuple[HTTPStatus, Union[str, list]]:
|
||||
user_id: str = user.uid
|
||||
server_ref = firebase_manager.firestore_database.collection('users').document(user_id).collection('servers')
|
||||
try:
|
||||
@ -65,7 +66,7 @@ def fetch_servers(user: UserRecord) -> tuple[HTTPStatus, str or list]:
|
||||
return account_create(user)
|
||||
|
||||
|
||||
def account_create(user: UserRecord) -> tuple[HTTPStatus, str or None]:
|
||||
def account_create(user: UserRecord) -> tuple[HTTPStatus, Union[str, None]]:
|
||||
if firebase_manager.user_field_exists(user.uid):
|
||||
return HTTPStatus.FORBIDDEN, "User already exists."
|
||||
try:
|
||||
@ -87,7 +88,7 @@ allowed_frameworks: list[str] = ["paper", "spigot", "bukkit"]
|
||||
|
||||
|
||||
def server_create(user: UserRecord, name: str, version: str, framework: str = "paper") -> (
|
||||
tuple)[HTTPStatus, str or None]:
|
||||
tuple)[HTTPStatus, Union[str, None]]:
|
||||
if framework not in allowed_frameworks:
|
||||
return HTTPStatus.METHOD_NOT_ALLOWED, f"Framework {framework} not recognized."
|
||||
user_id = user.uid
|
||||
@ -117,7 +118,7 @@ def server_create(user: UserRecord, name: str, version: str, framework: str = "p
|
||||
return HTTPStatus.INTERNAL_SERVER_ERROR, f"Server creation failed | {e}"
|
||||
|
||||
|
||||
def server_delete(name: str, user: UserRecord) -> tuple[HTTPStatus, str or None]:
|
||||
def server_delete(name: str, user: UserRecord) -> tuple[HTTPStatus, Union[str, None]]:
|
||||
user_id = user.uid
|
||||
try:
|
||||
port: int = firebase_manager.get_server_port(user_id)
|
||||
@ -134,7 +135,7 @@ def server_delete(name: str, user: UserRecord) -> tuple[HTTPStatus, str or None]
|
||||
return HTTPStatus.INTERNAL_SERVER_ERROR, None
|
||||
|
||||
|
||||
def account_delete(user: UserRecord) -> tuple[HTTPStatus, str or None]:
|
||||
def account_delete(user: UserRecord) -> tuple[HTTPStatus, Union[str, None]]:
|
||||
user_id = user.uid
|
||||
try:
|
||||
dns_record_id = firebase_manager.get_user_field(user_id, "cloudflare_entry")
|
||||
@ -155,7 +156,7 @@ def account_delete(user: UserRecord) -> tuple[HTTPStatus, str or None]:
|
||||
return HTTPStatus.INTERNAL_SERVER_ERROR, f"Error deleting user '{user_id}' on the server."
|
||||
|
||||
|
||||
def server_run(user: UserRecord, name: str) -> tuple[HTTPStatus, str or None]:
|
||||
def server_run(user: UserRecord, name: str) -> tuple[HTTPStatus, Union[str, None]]:
|
||||
user_id = user.uid
|
||||
mc_manager.set_cooldown(user_id=user_id)
|
||||
try:
|
||||
@ -175,7 +176,7 @@ def server_run(user: UserRecord, name: str) -> tuple[HTTPStatus, str or None]:
|
||||
return HTTPStatus.INTERNAL_SERVER_ERROR, f"Error when running server: {e}"
|
||||
|
||||
|
||||
def server_stop(user: UserRecord, name: str) -> tuple[HTTPStatus, str or None]:
|
||||
def server_stop(user: UserRecord, name: str) -> tuple[HTTPStatus, Union[str, None]]:
|
||||
port = None
|
||||
user_id: str = user.uid
|
||||
if mc_manager.has_cooldown(user_id=user_id):
|
||||
@ -199,7 +200,7 @@ def server_stop(user: UserRecord, name: str) -> tuple[HTTPStatus, str or None]:
|
||||
return HTTPStatus.INTERNAL_SERVER_ERROR, f"Error occurred when stopping server '{name}'."
|
||||
|
||||
|
||||
def update_property(uid: str, name: str, prop: str, value: str) -> tuple[HTTPStatus, str or None]:
|
||||
def update_property(uid: str, name: str, prop: str, value: str) -> tuple[HTTPStatus, Union[str, None]]:
|
||||
property_file_path: str = f"users/{uid}/{name}/server.properties"
|
||||
try:
|
||||
file_manager.update_server_property(property_file_path, prop, value)
|
||||
@ -215,7 +216,7 @@ def update_property(uid: str, name: str, prop: str, value: str) -> tuple[HTTPSta
|
||||
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]:
|
||||
def update_properties(user: UserRecord, name: str, props: list[tuple[str, str]]) -> tuple[HTTPStatus, Union[str, None]]:
|
||||
errors: list[str] = []
|
||||
for prop, value in props:
|
||||
if prop not in MinecraftServerManager.allowed_properties:
|
||||
@ -228,7 +229,7 @@ def update_properties(user: UserRecord, name: str, props: list[tuple[str, str]])
|
||||
return HTTPStatus.OK, f"Successfully updated server '{name}'."
|
||||
|
||||
|
||||
def run_command(user: UserRecord, command: str, name: str) -> tuple[HTTPStatus, str or None]:
|
||||
def run_command(user: UserRecord, command: str, name: str) -> tuple[HTTPStatus, Union[str, None]]:
|
||||
try:
|
||||
port = firebase_manager.get_server_port(user.uid)
|
||||
if port is None:
|
||||
|
@ -3,6 +3,7 @@ import shlex
|
||||
import time
|
||||
import mcipc.query
|
||||
import mcipc.query.client
|
||||
from typing import Union
|
||||
|
||||
|
||||
class MinecraftServerManager:
|
||||
@ -15,7 +16,7 @@ class MinecraftServerManager:
|
||||
self.cooldowns = {}
|
||||
|
||||
def start_server(self, server_directory: str, port: int,
|
||||
java_executable='java', jar_file='server.jar', memory_size='2048M') -> int or None:
|
||||
java_executable='java', jar_file='server.jar', memory_size='2048M') -> Union[int, None]:
|
||||
|
||||
if port in self.servers:
|
||||
return None
|
||||
|
Loading…
Reference in New Issue
Block a user