89 lines
3.3 KiB
Python
89 lines
3.3 KiB
Python
import html
|
|
import json
|
|
|
|
from onguard24.status_snapshot import build
|
|
|
|
|
|
def _row(name: str, value: object) -> str:
|
|
label = html.escape(name)
|
|
if value == "disabled":
|
|
badge = '<span class="badge muted">не настроено</span>'
|
|
return f"<tr><th>{label}</th><td>{badge}</td></tr>"
|
|
|
|
if isinstance(value, dict):
|
|
st = value.get("status", "?")
|
|
if st == "ok":
|
|
badge = '<span class="badge ok">OK</span>'
|
|
elif st == "reachable":
|
|
badge = '<span class="badge warn">доступен</span>'
|
|
elif st == "error":
|
|
badge = '<span class="badge err">ошибка</span>'
|
|
else:
|
|
badge = f'<span class="badge muted">{html.escape(str(st))}</span>'
|
|
extra = {k: v for k, v in value.items() if k != "status"}
|
|
detail_html = ""
|
|
if extra:
|
|
detail_html = (
|
|
f'<tr class="sub"><td colspan="2"><pre class="detail">'
|
|
f"{html.escape(json.dumps(extra, ensure_ascii=False, indent=2))}"
|
|
f"</pre></td></tr>"
|
|
)
|
|
return f"<tr><th>{label}</th><td>{badge}</td></tr>{detail_html}"
|
|
|
|
badge = html.escape(str(value))
|
|
return f"<tr><th>{label}</th><td>{badge}</td></tr>"
|
|
|
|
|
|
async def render_root_page(request) -> str:
|
|
data = await build(request)
|
|
rows = ""
|
|
for key in ("database", "vault", "grafana", "forgejo"):
|
|
if key in data:
|
|
rows += _row(key, data[key])
|
|
|
|
payload = html.escape(json.dumps(data, ensure_ascii=False, indent=2))
|
|
|
|
return f"""<!DOCTYPE html>
|
|
<html lang="ru">
|
|
<head>
|
|
<meta charset="utf-8"/>
|
|
<meta name="viewport" content="width=device-width, initial-scale=1"/>
|
|
<title>onGuard24</title>
|
|
<style>
|
|
body {{ font-family: system-ui, sans-serif; margin: 2rem; background: #fafafa; color: #18181b; }}
|
|
h1 {{ margin-top: 0; }}
|
|
.badge {{ display: inline-block; padding: 0.15rem 0.5rem; border-radius: 6px; font-size: 0.85rem; font-weight: 600; }}
|
|
.ok {{ background: #dcfce7; color: #166534; }}
|
|
.warn {{ background: #fef9c3; color: #854d0e; }}
|
|
.err {{ background: #fee2e2; color: #991b1b; }}
|
|
.muted {{ background: #e4e4e7; color: #52525b; }}
|
|
table {{ border-collapse: collapse; width: 100%; max-width: 56rem; background: #fff; border-radius: 8px; overflow: hidden; box-shadow: 0 1px 3px #0001; }}
|
|
th {{ text-align: left; padding: 0.75rem 1rem; width: 10rem; vertical-align: top; border-bottom: 1px solid #e4e4e7; }}
|
|
td {{ padding: 0.75rem 1rem; border-bottom: 1px solid #e4e4e7; }}
|
|
tr.sub .detail {{ margin: 0; font-size: 0.8rem; max-height: 10rem; overflow: auto; }}
|
|
.links a {{ margin-right: 1rem; }}
|
|
.json {{ margin-top: 2rem; max-width: 56rem; }}
|
|
.json pre {{ background: #18181b; color: #e4e4e7; padding: 1rem; border-radius: 8px; overflow: auto; font-size: 0.8rem; }}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<h1>onGuard24</h1>
|
|
<p class="links">
|
|
<a href="/docs">Swagger</a>
|
|
<a href="/openapi.json">OpenAPI</a>
|
|
<a href="/health">/health</a>
|
|
<a href="/api/v1/status">JSON статус</a>
|
|
</p>
|
|
<h2>Проверки доступа</h2>
|
|
<table>
|
|
<tbody>
|
|
{rows}
|
|
</tbody>
|
|
</table>
|
|
<div class="json">
|
|
<h3>Полный ответ <code>/api/v1/status</code></h3>
|
|
<pre>{payload}</pre>
|
|
</div>
|
|
</body>
|
|
</html>"""
|