2026-04-03 08:30:56 +03:00
|
|
|
import html
|
|
|
|
|
import json
|
|
|
|
|
|
2026-04-03 08:45:19 +03:00
|
|
|
from starlette.requests import Request
|
|
|
|
|
|
|
|
|
|
from onguard24.modules.registry import MODULE_MOUNTS
|
|
|
|
|
from onguard24.modules.ui_support import APP_SHELL_CSS, nav_rail_html, safe_fragment
|
2026-04-03 08:30:56 +03:00
|
|
|
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>"
|
|
|
|
|
|
|
|
|
|
|
2026-04-03 08:45:19 +03:00
|
|
|
async def _module_sections_html(request: Request) -> str:
|
|
|
|
|
"""Блоки модулей на главной: фрагменты изолированы (ошибка одного не роняет страницу)."""
|
|
|
|
|
blocks: list[str] = []
|
|
|
|
|
for m in MODULE_MOUNTS:
|
|
|
|
|
title = html.escape(m.title)
|
|
|
|
|
slug_e = html.escape(m.slug)
|
|
|
|
|
full_url = f"/ui/modules/{m.slug}/"
|
|
|
|
|
if m.render_home_fragment:
|
|
|
|
|
inner = await safe_fragment(m.slug, m.render_home_fragment, request)
|
|
|
|
|
else:
|
|
|
|
|
inner = '<p class="module-note">Превью не задано — откройте полный интерфейс.</p>'
|
|
|
|
|
foot = ""
|
|
|
|
|
if m.ui_router:
|
|
|
|
|
foot = (
|
|
|
|
|
f'<footer class="module-card-foot">'
|
|
|
|
|
f'<a class="module-open" href="{html.escape(full_url)}">'
|
|
|
|
|
"Полный интерфейс модуля"
|
|
|
|
|
f"</a></footer>"
|
|
|
|
|
)
|
|
|
|
|
blocks.append(
|
|
|
|
|
f'<article class="module-card" data-module="{slug_e}">'
|
|
|
|
|
f'<header class="module-card-head"><h3>{title}</h3></header>'
|
|
|
|
|
f'<div class="module-card-body">{inner}</div>'
|
|
|
|
|
f"{foot}"
|
|
|
|
|
f"</article>"
|
|
|
|
|
)
|
|
|
|
|
grid = "".join(blocks)
|
|
|
|
|
return (
|
|
|
|
|
'<section class="modules" id="modules">'
|
|
|
|
|
"<h2>Модули</h2>"
|
|
|
|
|
f'<div class="modules-grid">{grid}</div>'
|
|
|
|
|
"</section>"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def render_root_page(request: Request) -> str:
|
2026-04-03 08:30:56 +03:00
|
|
|
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))
|
2026-04-03 08:45:19 +03:00
|
|
|
modules_html = await _module_sections_html(request)
|
|
|
|
|
rail = nav_rail_html(None)
|
2026-04-03 08:30:56 +03:00
|
|
|
|
|
|
|
|
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>
|
2026-04-03 08:45:19 +03:00
|
|
|
{APP_SHELL_CSS}
|
2026-04-03 08:30:56 +03:00
|
|
|
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; }}
|
2026-04-03 08:45:19 +03:00
|
|
|
.modules {{ margin-top: 2.5rem; max-width: 56rem; }}
|
|
|
|
|
.modules h2 {{ font-size: 1.25rem; }}
|
|
|
|
|
.modules-grid {{ display: grid; grid-template-columns: repeat(auto-fill, minmax(17rem, 1fr)); gap: 1rem; margin-top: 1rem; }}
|
|
|
|
|
.module-card {{ background: #fff; border-radius: 8px; box-shadow: 0 1px 3px #0001; overflow: hidden; display: flex; flex-direction: column; }}
|
|
|
|
|
.module-card-head {{ padding: 0.75rem 1rem; border-bottom: 1px solid #e4e4e7; }}
|
|
|
|
|
.module-card-head h3 {{ margin: 0; font-size: 1rem; }}
|
|
|
|
|
.module-card-body {{ padding: 0.75rem 1rem; flex: 1; font-size: 0.9rem; }}
|
|
|
|
|
.module-card-foot {{ padding: 0.5rem 1rem; border-top: 1px solid #f4f4f5; font-size: 0.85rem; }}
|
|
|
|
|
.module-open {{ font-weight: 600; }}
|
|
|
|
|
.module-fragment p {{ margin: 0.35rem 0 0; }}
|
|
|
|
|
.module-note {{ color: #71717a; margin: 0; }}
|
|
|
|
|
.module-err {{ color: #991b1b; background: #fef2f2; padding: 0.5rem 0.75rem; border-radius: 6px; font-size: 0.85rem; }}
|
2026-04-03 08:30:56 +03:00
|
|
|
</style>
|
|
|
|
|
</head>
|
|
|
|
|
<body>
|
2026-04-03 08:45:19 +03:00
|
|
|
<div class="app-shell">
|
|
|
|
|
<main class="app-main">
|
2026-04-03 08:30:56 +03:00
|
|
|
<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>
|
2026-04-03 08:45:19 +03:00
|
|
|
{modules_html}
|
2026-04-03 08:30:56 +03:00
|
|
|
<div class="json">
|
|
|
|
|
<h3>Полный ответ <code>/api/v1/status</code></h3>
|
|
|
|
|
<pre>{payload}</pre>
|
|
|
|
|
</div>
|
2026-04-03 08:45:19 +03:00
|
|
|
</main>
|
|
|
|
|
{rail}
|
|
|
|
|
</div>
|
2026-04-03 08:30:56 +03:00
|
|
|
</body>
|
|
|
|
|
</html>"""
|