Tweaks & changes for deployment
This commit is contained in:
parent
4a586dcb4e
commit
284c016e21
7 changed files with 156 additions and 4 deletions
24
src/__main__.bak.py
Normal file
24
src/__main__.bak.py
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
"""
|
||||
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()
|
||||
|
|
@ -89,7 +89,7 @@ def main() -> None:
|
|||
|
||||
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()
|
||||
# wss_thread.join()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
|
|
|||
42
src/runtimes/rest.py
Normal file
42
src/runtimes/rest.py
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
"""
|
||||
@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 get_server_info
|
||||
|
||||
client = docker.from_env()
|
||||
app = Flask(__name__)
|
||||
CORS(app)
|
||||
|
||||
|
||||
@app.route("/")
|
||||
def index():
|
||||
try:
|
||||
data = get_server_info(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)
|
||||
55
src/runtimes/websocket.py
Normal file
55
src/runtimes/websocket.py
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
"""
|
||||
@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 get_server_info
|
||||
|
||||
|
||||
def __fetch(client: DockerClient):
|
||||
return json.dumps(get_server_info(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(get_server_info(CLIENT, all=True))
|
||||
|
||||
while new == CACHE:
|
||||
await asyncio.sleep(1)
|
||||
new = json.dumps(get_server_info(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