From 91f95d55a567d4854409065405d9d96a1deee48b Mon Sep 17 00:00:00 2001 From: buenosairesam Date: Mon, 26 Jan 2026 21:00:50 -0300 Subject: [PATCH] Move edge HTML to templates, add jinja2 --- ctrl/edge/Dockerfile | 3 +- ctrl/edge/edge.py | 365 +---------------------------- ctrl/edge/templates/dashboard.html | 349 +++++++++++++++++++++++++++ 3 files changed, 360 insertions(+), 357 deletions(-) create mode 100644 ctrl/edge/templates/dashboard.html diff --git a/ctrl/edge/Dockerfile b/ctrl/edge/Dockerfile index 9e92404..b098374 100644 --- a/ctrl/edge/Dockerfile +++ b/ctrl/edge/Dockerfile @@ -2,9 +2,10 @@ FROM python:3.11-slim WORKDIR /app -RUN pip install --no-cache-dir fastapi uvicorn[standard] websockets +RUN pip install --no-cache-dir fastapi uvicorn[standard] websockets jinja2 COPY edge.py . +COPY templates/ templates/ ENV API_KEY="" ENV LOG_LEVEL=INFO diff --git a/ctrl/edge/edge.py b/ctrl/edge/edge.py index 526b9e4..77aa94c 100644 --- a/ctrl/edge/edge.py +++ b/ctrl/edge/edge.py @@ -4,10 +4,12 @@ import asyncio import json import logging import os -from datetime import datetime +from pathlib import Path from fastapi import FastAPI, Query, WebSocket, WebSocketDisconnect +from fastapi.requests import Request from fastapi.responses import HTMLResponse +from fastapi.templating import Jinja2Templates # Configuration API_KEY = os.environ.get("API_KEY", "") @@ -23,367 +25,19 @@ log = logging.getLogger("gateway") app = FastAPI(title="sysmonstm") +# Templates +templates_path = Path(__file__).parent / "templates" +templates = Jinja2Templates(directory=str(templates_path)) + # Store connected websockets connections: list[WebSocket] = [] # Store latest metrics from collectors machines: dict = {} -HTML = """ - - - - - - System Monitor Dashboard - - - -
-

System Monitor

-
- - Connecting... -
-
- -
-
-
-

No machines connected

-

Waiting for collectors to send metrics...

-
-
-
- - - - -""" - @app.get("/", response_class=HTMLResponse) -async def index(): - return HTML +async def index(request: Request): + return templates.TemplateResponse("dashboard.html", {"request": request}) @app.get("/health") @@ -399,7 +53,6 @@ async def get_machines(): @app.websocket("/ws") async def websocket_endpoint(websocket: WebSocket, key: str = Query(default="")): # API key validation for collectors (browsers don't need key) - # Check if this looks like a collector (will send metrics) or browser (will receive) # We validate key only when metrics are received, allowing browsers to connect freely await websocket.accept() diff --git a/ctrl/edge/templates/dashboard.html b/ctrl/edge/templates/dashboard.html new file mode 100644 index 0000000..ba2a969 --- /dev/null +++ b/ctrl/edge/templates/dashboard.html @@ -0,0 +1,349 @@ + + + + + + System Monitor Dashboard + + + +
+

System Monitor

+
+ + Connecting... +
+
+ +
+
+
+

No machines connected

+

Waiting for collectors to send metrics...

+
+
+
+ + + +