From 190c3ed616e66938624e89ac8a5a7eab7f7523c4 Mon Sep 17 00:00:00 2001 From: oitsjustjose Date: Sat, 14 Jun 2025 15:43:45 -0500 Subject: [PATCH] Remove old implementation -- Docker won't work with this :( --- Dockerfile | 15 ----------- src/__main__.bak.py | 24 ----------------- src/runtimes/rest.py | 42 ------------------------------ src/runtimes/websocket.py | 55 --------------------------------------- 4 files changed, 136 deletions(-) delete mode 100644 Dockerfile delete mode 100644 src/__main__.bak.py delete mode 100644 src/runtimes/rest.py delete mode 100644 src/runtimes/websocket.py diff --git a/Dockerfile b/Dockerfile deleted file mode 100644 index e74e3e7..0000000 --- a/Dockerfile +++ /dev/null @@ -1,15 +0,0 @@ -FROM python:3.10.16-slim - -RUN apt-get update && apt-get upgrade -y -# Required for the Docker Client to work within our app inside of another Docker Container -RUN apt-get -y install apt-transport-https ca-certificates curl gnupg2 software-properties-common -RUN curl -fsSL https://download.docker.com/linux/debian/gpg | apt-key add - -RUN add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/debian $(lsb_release -cs) stable" - -COPY . . - -RUN pip install -r requirements.txt - -WORKDIR /src - -CMD ["python", "__main__.py"] \ No newline at end of file diff --git a/src/__main__.bak.py b/src/__main__.bak.py deleted file mode 100644 index 90bc2ea..0000000 --- a/src/__main__.bak.py +++ /dev/null @@ -1,24 +0,0 @@ -""" -Author: Jose Stovall -Description: A Flask API service for the running Minecraft server statuses -""" - -from os import environ as env - - -def main() -> None: - host_method = env["METHOD"].lower() - if host_method == "rest": - from runtimes import rest - - rest.run() - elif host_method == "websocket": - from runtimes import websocket - - websocket.run() - else: - raise Exception(f"Unsupported 'METHOD' environment variable Currently set to {host_method}. Must be either 'rest' or 'websocket', and CANNOT be empty!") - - -if __name__ == "__main__": - main() diff --git a/src/runtimes/rest.py b/src/runtimes/rest.py deleted file mode 100644 index df06774..0000000 --- a/src/runtimes/rest.py +++ /dev/null @@ -1,42 +0,0 @@ -""" -@author: Jose Stovall | oitsjustjose -A dedicated REST API server for manually fetching the current status - of the running servers -""" - -from os import environ as env - -import docker -from flask import Flask, jsonify, request -from flask_cors import CORS - -from common import list_servers - -client = docker.from_env() -app = Flask(__name__) -CORS(app) - - -@app.route("/") -def index(): - try: - data = list_servers(client, all=bool(request.args.get("all"))) - - 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(): - host = "0.0.0.0" - port = int(env["PORT"]) if "PORT" in env else 9090 - - print(f"RestAPI Server is now running on {host}:{port}") - app.run(host=host, port=port, threaded=True) diff --git a/src/runtimes/websocket.py b/src/runtimes/websocket.py deleted file mode 100644 index 8799d15..0000000 --- a/src/runtimes/websocket.py +++ /dev/null @@ -1,55 +0,0 @@ -""" -@author: Jose Stovall | oitsjustjose -A dedicated websocket server for updating connected clients with - the latest set of changes to the server list -""" - -import asyncio -import json -from os import environ as env - -import docker -from docker import DockerClient -from websockets.asyncio.server import ServerConnection, serve - -from common import list_servers - - -def __fetch(client: DockerClient): - return json.dumps(list_servers(client, all=True)) - - -CLIENT = docker.from_env() -CACHE = __fetch(CLIENT) - - -async def __loop_infinitely(conn: ServerConnection): - while True: - # raises ConnectionClosed ex which breaks out of the loop. - # https://websockets.readthedocs.io/en/stable/howto/patterns.html - await __wait_for_change(conn) - - -async def __setup(host: str, port: int = 8008): - async with serve(__loop_infinitely, host, port): - await asyncio.Future() - - -async def __wait_for_change(conn: ServerConnection): - global CACHE - - new = json.dumps(list_servers(CLIENT, all=True)) - - while new == CACHE: - await asyncio.sleep(1) - new = json.dumps(list_servers(CLIENT, all=True)) - CACHE = new - await conn.send(new) - - -def run(): - host = "0.0.0.0" - port = int(env["PORT"]) if "PORT" in env else 8008 - - print(f"WebSocket Server is now running on {host}:{port}") - asyncio.run(__setup(host, port))