Bläddra i källkod

HTML page that lists variables

boyska 9 månader sedan
förälder
incheckning
0442056953
2 ändrade filer med 37 tillägg och 1 borttagningar
  1. 27 0
      templates/index.html
  2. 10 1
      webserver.py

+ 27 - 0
templates/index.html

@@ -0,0 +1,27 @@
+<!doctype html>
+<html>
+<head>
+    <meta http-equiv="refresh" content="60">
+    <title>Squeow</title>
+    <style>
+    table td { text-align: right; }
+    table th { text-align: left; }
+    table { border: 1px solid black; border-collapse: collapse; }
+    td, th { border: 1px solid black; padding: 0.3em; font-family: Monospace; font-size: 2em; }
+    </style>
+</head>
+<body>
+    <section>
+        <header> <h2>Variables</h2> </header>
+        <table border><tbody>
+            {% for key, value in variables.items() %}
+            <tr>
+                <th>{{key}}</th>
+                <td>{{value}}</td>
+            </tr>
+            {% endfor %}
+            </tbody></table>
+    </section>
+</body>
+</html>
+

+ 10 - 1
webserver.py

@@ -6,9 +6,11 @@ from typing import Optional, Callable, Any
 import datetime
 
 from pydantic import BaseModel, BaseSettings, Field
-from fastapi import FastAPI, Depends, HTTPException, status, Response
+from fastapi import FastAPI, Depends, HTTPException, status, Response, Request
+from fastapi.responses import HTMLResponse
 from fastapi.security import HTTPBasic, HTTPBasicCredentials
 from fastapi.middleware.cors import CORSMiddleware
+from fastapi.templating import Jinja2Templates
 
 
 class VariableModel(BaseModel):
@@ -50,6 +52,7 @@ class Settings(BaseSettings):
 app = FastAPI()
 settings = Settings()
 security = HTTPBasic()
+templates = Jinja2Templates(directory="templates")
 
 
 app.add_middleware(
@@ -144,3 +147,9 @@ async def export_prometheus() -> str:
     variables.append(("squeow_time_since_last_seen", int(time_since_last_seen)))
     text = "".join(f"{k} {v}\n" for k, v in variables)
     return Response(text, media_type="text/plain")
+
+
+@app.get("/", response_class=HTMLResponse)
+async def get_page_index(request: Request):
+    return templates.TemplateResponse("index.html",
+                                      dict(request=request, variables=settings.variables))