diff --git a/velping/__main__.py b/velping/__main__.py index d8e8d22..9046e58 100644 --- a/velping/__main__.py +++ b/velping/__main__.py @@ -5,10 +5,11 @@ import time import httpx import uvicorn from fastapi import FastAPI +from fastapi.responses import HTMLResponse from fastapi.staticfiles import StaticFiles from jinja2 import Environment, PackageLoader -from velping.config import Config, ServiceConfig, load_cfg +from velping.config import HTTPServiceConfig, TCPServiceConfig, load_cfg http = httpx.Client( headers={"User-Agent": "velping"}, @@ -46,7 +47,7 @@ def handle_service_status( print(f"[I] Pinging {service_name} succeeded!") -def tcp_ping(service: ServiceConfig) -> None: +def tcp_ping(service: TCPServiceConfig) -> None: """Continuously ping a service via TCP.""" socket_type = socket.AF_INET if service.ipv == 4 else socket.AF_INET6 @@ -77,7 +78,7 @@ def tcp_ping(service: ServiceConfig) -> None: time.sleep(cfg.pinging.rate) -def http_ping(service: ServiceConfig) -> None: +def http_ping(service: HTTPServiceConfig) -> None: """Continuously ping a service via HTTP.""" while True: print(f"[I] Pinging {service.name}") @@ -97,8 +98,8 @@ def http_ping(service: ServiceConfig) -> None: time.sleep(cfg.pinging.rate) -@app.get("/") -async def root(cfg: Config) -> str: +@app.get("/", response_class=HTMLResponse) +async def root() -> str: template = templates.get_template("index.xht") return template.render( cfg=cfg, diff --git a/velping/config.py b/velping/config.py index ee18968..8343013 100644 --- a/velping/config.py +++ b/velping/config.py @@ -10,6 +10,7 @@ class ServiceConfig(BaseModel): name: str type: Literal["http", "tcp"] timeout: int = 5 + category: str | None = None class HTTPServiceConfig(ServiceConfig): @@ -58,6 +59,8 @@ def load_cfg() -> Config: if isinstance(cfg.services, dict): print("Your services config is using a dict, which is deprecated.") cfg.services = list(cfg.services.values()) + + cfg.services = sorted(cfg.services, key=lambda s: s.category or "") except ValidationError as e: raise SystemExit(str(e)) diff --git a/velping/templates/index.xht b/velping/templates/index.xht index cb2d95b..9813927 100644 --- a/velping/templates/index.xht +++ b/velping/templates/index.xht @@ -12,21 +12,33 @@
{{ service.name }}
-█
- {% elif ping == "O" %} -█
- {% else %} -█
- {% endif %} - {% endfor %} + {% set services_by_cat = {} %} + {% for s in services %} + {% set cat = s.category if s.category else "__none__" %} + {% set _ = services_by_cat.setdefault(cat, []).append(s) %} + {% endfor %} + + {% for cat, cat_services in services_by_cat.items() %} + {% if cat != "__none__" %} +{{ service.name }}
+█
+ {% elif ping == "O" %} +█
+ {% else %} +█
+ {% endif %} + {% endfor %} +