Remove old implementation -- Docker won't work with this :(
This commit is contained in:
parent
c58b095248
commit
190c3ed616
4 changed files with 0 additions and 136 deletions
|
|
@ -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()
|
||||
|
|
@ -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)
|
||||
|
|
@ -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))
|
||||
Loading…
Reference in a new issue