Add service categories and also unbreak things
This commit is contained in:
+6
-5
@@ -5,10 +5,11 @@ import time
|
|||||||
import httpx
|
import httpx
|
||||||
import uvicorn
|
import uvicorn
|
||||||
from fastapi import FastAPI
|
from fastapi import FastAPI
|
||||||
|
from fastapi.responses import HTMLResponse
|
||||||
from fastapi.staticfiles import StaticFiles
|
from fastapi.staticfiles import StaticFiles
|
||||||
from jinja2 import Environment, PackageLoader
|
from jinja2 import Environment, PackageLoader
|
||||||
|
|
||||||
from velping.config import Config, ServiceConfig, load_cfg
|
from velping.config import HTTPServiceConfig, TCPServiceConfig, load_cfg
|
||||||
|
|
||||||
http = httpx.Client(
|
http = httpx.Client(
|
||||||
headers={"User-Agent": "velping"},
|
headers={"User-Agent": "velping"},
|
||||||
@@ -46,7 +47,7 @@ def handle_service_status(
|
|||||||
print(f"[I] Pinging {service_name} succeeded!")
|
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."""
|
"""Continuously ping a service via TCP."""
|
||||||
|
|
||||||
socket_type = socket.AF_INET if service.ipv == 4 else socket.AF_INET6
|
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)
|
time.sleep(cfg.pinging.rate)
|
||||||
|
|
||||||
|
|
||||||
def http_ping(service: ServiceConfig) -> None:
|
def http_ping(service: HTTPServiceConfig) -> None:
|
||||||
"""Continuously ping a service via HTTP."""
|
"""Continuously ping a service via HTTP."""
|
||||||
while True:
|
while True:
|
||||||
print(f"[I] Pinging {service.name}")
|
print(f"[I] Pinging {service.name}")
|
||||||
@@ -97,8 +98,8 @@ def http_ping(service: ServiceConfig) -> None:
|
|||||||
time.sleep(cfg.pinging.rate)
|
time.sleep(cfg.pinging.rate)
|
||||||
|
|
||||||
|
|
||||||
@app.get("/")
|
@app.get("/", response_class=HTMLResponse)
|
||||||
async def root(cfg: Config) -> str:
|
async def root() -> str:
|
||||||
template = templates.get_template("index.xht")
|
template = templates.get_template("index.xht")
|
||||||
return template.render(
|
return template.render(
|
||||||
cfg=cfg,
|
cfg=cfg,
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ class ServiceConfig(BaseModel):
|
|||||||
name: str
|
name: str
|
||||||
type: Literal["http", "tcp"]
|
type: Literal["http", "tcp"]
|
||||||
timeout: int = 5
|
timeout: int = 5
|
||||||
|
category: str | None = None
|
||||||
|
|
||||||
|
|
||||||
class HTTPServiceConfig(ServiceConfig):
|
class HTTPServiceConfig(ServiceConfig):
|
||||||
@@ -58,6 +59,8 @@ def load_cfg() -> Config:
|
|||||||
if isinstance(cfg.services, dict):
|
if isinstance(cfg.services, dict):
|
||||||
print("Your services config is using a dict, which is deprecated.")
|
print("Your services config is using a dict, which is deprecated.")
|
||||||
cfg.services = list(cfg.services.values())
|
cfg.services = list(cfg.services.values())
|
||||||
|
|
||||||
|
cfg.services = sorted(cfg.services, key=lambda s: s.category or "")
|
||||||
except ValidationError as e:
|
except ValidationError as e:
|
||||||
raise SystemExit(str(e))
|
raise SystemExit(str(e))
|
||||||
|
|
||||||
|
|||||||
@@ -12,7 +12,18 @@
|
|||||||
<body>
|
<body>
|
||||||
<h2>{{ cfg.web.name }}</h2>
|
<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">
|
<div class="service">
|
||||||
<p><b>{{ service.name }}</b></p>
|
<p><b>{{ service.name }}</b></p>
|
||||||
<div class="status">
|
<div class="status">
|
||||||
@@ -28,6 +39,7 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
|
{% endfor %}
|
||||||
</body>
|
</body>
|
||||||
|
|
||||||
</html>
|
</html>
|
||||||
|
|||||||
Reference in New Issue
Block a user