Added libs
This commit is contained in:
65
app/ui/static/main.js
Normal file
65
app/ui/static/main.js
Normal file
@@ -0,0 +1,65 @@
|
||||
const socket = io();
|
||||
// const socket = io("http://127.0.0.1:5000");
|
||||
|
||||
socket.on("connect", () => {
|
||||
console.log("Connected...");
|
||||
socket.emit("frontend_ready");
|
||||
});
|
||||
|
||||
const logContainer = document.getElementById("log-container");
|
||||
|
||||
function appendLog(data) {
|
||||
const line = document.createElement("div");
|
||||
line.textContent = data.levelname + " : " + data.message;
|
||||
logContainer.appendChild(line);
|
||||
logContainer.scrollTop = logContainer.scrollHeight;
|
||||
}
|
||||
|
||||
const statusTree = document.getElementById("updater-tree");
|
||||
let statusTreeData = {};
|
||||
|
||||
function renderStatusTable() {
|
||||
let html = `
|
||||
<table border="1" cellpadding="6">
|
||||
<tr>
|
||||
<th>Módulo</th>
|
||||
<th>Status</th>
|
||||
</tr>
|
||||
`;
|
||||
for (const [module, status] of Object.entries(statusTreeData)) {
|
||||
html += `
|
||||
<tr>
|
||||
<td>${module}</td>
|
||||
<td>${status}</td>
|
||||
</tr>
|
||||
`;
|
||||
}
|
||||
html += `</table>`;
|
||||
statusTree.innerHTML = html;
|
||||
}
|
||||
|
||||
socket.on("status_lib", (data) => {
|
||||
console.log("lib status", data);
|
||||
|
||||
statusTreeData[data.lib] = data.status;
|
||||
|
||||
renderStatusTable();
|
||||
});
|
||||
|
||||
socket.on("redirect", (data) => {
|
||||
console.log("redirect", data);
|
||||
location.replace(data.url);
|
||||
});
|
||||
|
||||
socket.on("restarting", (data) => {
|
||||
console.log("restarting", data);
|
||||
location.reload()
|
||||
});
|
||||
|
||||
socket.on("log", (data) => {
|
||||
appendLog(data);
|
||||
});
|
||||
|
||||
socket.on("error", (data) => {
|
||||
appendLog(data);
|
||||
});
|
||||
22
app/ui/templates/index.html
Normal file
22
app/ui/templates/index.html
Normal file
@@ -0,0 +1,22 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>NoSys Startup</title>
|
||||
<style>
|
||||
body { font-family: sans-serif; margin: 20px; }
|
||||
ul { list-style-type: none; padding-left: 20px; }
|
||||
li { margin: 5px 0; }
|
||||
.status { font-weight: bold; }
|
||||
</style>
|
||||
</head>
|
||||
<body style="background-color:black; color: white;">
|
||||
<h1>NoSys Startup Status</h1>
|
||||
<ul id="updater-tree"></ul>
|
||||
<div id="log-container"></div>
|
||||
|
||||
<script src="https://cdn.socket.io/4.7.2/socket.io.min.js"></script>
|
||||
<!-- <script src="/static/main.js"></script> -->
|
||||
<script src="static/main.js"></script>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
76
app/ui/ui_server.py
Normal file
76
app/ui/ui_server.py
Normal file
@@ -0,0 +1,76 @@
|
||||
from libs.app.common.logging import get_logger
|
||||
from flask import Flask, render_template, request
|
||||
from flask_socketio import SocketIO
|
||||
import multiprocessing
|
||||
import threading
|
||||
import time
|
||||
|
||||
logger = get_logger()
|
||||
|
||||
class UIServer:
|
||||
def __init__(self, host="127.0.0.1", port=5000):
|
||||
self.host = host
|
||||
self.port = port
|
||||
self.process = None
|
||||
self.queue = multiprocessing.Queue()
|
||||
self.client_ready = multiprocessing.Value("b", False)
|
||||
|
||||
@staticmethod
|
||||
def run_server(host, port, queue, client_ready):
|
||||
app = Flask(__name__)
|
||||
socketio = SocketIO(app, cors_allowed_origins="*")
|
||||
|
||||
@app.route("/")
|
||||
def index():
|
||||
return render_template("index.html")
|
||||
|
||||
@socketio.on("frontend_ready")
|
||||
def frontend_ready():
|
||||
client_ready.value = True
|
||||
|
||||
def queue_listener():
|
||||
while True:
|
||||
event, data = queue.get()
|
||||
while not client_ready.value:
|
||||
time.sleep(1)
|
||||
|
||||
socketio.emit(event, data)
|
||||
|
||||
threading.Thread(target=queue_listener, daemon=True).start()
|
||||
|
||||
socketio.run(app, host=host, port=port, debug=False, use_reloader=False)
|
||||
|
||||
def start(self):
|
||||
if self.process and self.process.is_alive():
|
||||
logger.debug("Server is already running")
|
||||
return
|
||||
self.process = multiprocessing.Process(
|
||||
target=UIServer.run_server,
|
||||
args=(self.host, self.port, self.queue, self.client_ready),
|
||||
)
|
||||
self.process.start()
|
||||
logger.debug(f"Server started on http://{self.host}:{self.port}")
|
||||
|
||||
def stop(self, wait_queue=True, time_limit=5):
|
||||
while wait_queue and not self.queue.empty():
|
||||
logger.debug(f"Waiting server. {self.queue.qsize()} items in queue")
|
||||
time.sleep(1)
|
||||
time_limit-=1
|
||||
if time_limit <= 0:
|
||||
break
|
||||
|
||||
if self.process and self.process.is_alive():
|
||||
self.process.terminate()
|
||||
self.process.join()
|
||||
logger.debug("Server stopped")
|
||||
self.process = None
|
||||
|
||||
def restart(self):
|
||||
logger.debug("Restarting server...")
|
||||
self.stop()
|
||||
self.start()
|
||||
|
||||
def emit_event(self, event, data={}):
|
||||
self.queue.put((event, data))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user