Add service categories and also unbreak things
This commit is contained in:
+6
-5
@@ -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,
|
||||
|
||||
@@ -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))
|
||||
|
||||
|
||||
@@ -12,7 +12,18 @@
|
||||
<body>
|
||||
<h2>{{ cfg.web.name }}</h2>
|
||||
|
||||
{% for service in services %}
|
||||
{% 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__" %}
|
||||
<h3>{{ cat }}</h3>
|
||||
{% endif %}
|
||||
|
||||
{% for service in cat_services %}
|
||||
<div class="service">
|
||||
<p><b>{{ service.name }}</b></p>
|
||||
<div class="status">
|
||||
@@ -28,6 +39,7 @@
|
||||
</div>
|
||||
</div>
|
||||
{% endfor %}
|
||||
{% endfor %}
|
||||
</body>
|
||||
|
||||
</html>
|
||||
|
||||
Reference in New Issue
Block a user