Massive refactor, WSS and Rest on same eec

This commit is contained in:
oitsjustjose 2025-06-13 23:12:18 -05:00
commit 9863b0ead9
6 changed files with 276 additions and 242 deletions

View file

@ -3,30 +3,95 @@ Author: Jose Stovall
Description: A Flask API service for the running Minecraft server statuses
"""
from flask import jsonify, request, Flask
import json
from os import environ as env
from flask import Flask, jsonify, request
import asyncio
from flask_cors import CORS
from websockets.asyncio.server import serve, ServerConnection
from common import get_server_info
import docker
from docker import DockerClient
app = Flask(__name__)
CORS(app)
from threading import Thread
@app.route("/")
def index():
try:
data = get_server_info(all=request.args.get("all"))
class RestServer:
def __init__(self, docker_client: DockerClient):
self._app = Flask("RestAPI")
self._app.add_url_rule("/", view_func=self.index)
self._client = docker_client
CORS(self._app)
if request.args.get("sort"):
data = sorted(data, key=lambda x: x[request.args.get("sort")])
def index(self):
try:
data = get_server_info(self._client, all=bool(request.args.get("all")))
resp = jsonify(data)
resp.status_code = 200
return resp
except KeyError as e:
resp = jsonify({"error": f"Failed to find key {str(e)}"})
resp.status_code = 422
return resp
if request.args.get("sort"):
data = sorted(data, key=lambda x: x[request.args.get("sort")])
resp = jsonify(data)
resp.status_code = 200
return resp
except KeyError as e:
resp = jsonify({"error": f"Failed to find key {str(e)}"})
resp.status_code = 422
return resp
def run(self, **kwargs):
print(f"Rest Server is now running on {kwargs['host']}:{kwargs['port']}")
self._app.run(**kwargs)
class WebSocketServer:
def __init__(self, docker_client: DockerClient):
self._client = docker_client
self.cache = json.dumps(get_server_info(self._client, all=True))
async def __loop_infinitely(self, conn: ServerConnection):
while True:
# raises ConnectionClosed ex which breaks out of the loop.
# https://websockets.readthedocs.io/en/stable/howto/patterns.html
await self.__wait_for_change(conn)
async def __setup(self, host: str, port: int = 8008):
async with serve(self.__loop_infinitely, host, port):
await asyncio.Future()
async def __wait_for_change(self, conn: ServerConnection):
new = json.dumps(get_server_info(self._client, all=True))
while new == self.cache:
await asyncio.sleep(1)
new = json.dumps(get_server_info(self._client, all=True))
print(new)
self.cache = new
await conn.send(new)
def run(self, host: str, port: int):
print(f"WebSocket Server is now running on {host}:{port}")
asyncio.run(self.__setup(host, port))
def main() -> None:
client = docker.from_env()
wss = WebSocketServer(client)
wss_thread = Thread(
daemon=True,
target=wss.run,
kwargs={
"host": "0.0.0.0",
"port": int(env["WS_PORT"]) if "WS_PORT" in env else 8008,
},
)
wss_thread.start()
RestServer(client).run(host="0.0.0.0", port=int(env["REST_PORT"]) if "REST_PORT" in env else 9090)
wss_thread.join()
if __name__ == "__main__":
app.run(host="0.0.0.0")
main()