v1.4.0: модули с веб-UI, правое меню, расширенные тесты

Реестр MODULE_MOUNTS: API, ui_router, фрагменты главной, EventBus.
Главная и страницы модулей с правой навигацией из реестра; wrap_module_html_page.
Ingress: публикация alert.received после сохранения в БД.
Документация MODULES.md; pytest покрывает API, UI и навигацию.

Made-with: Cursor
This commit is contained in:
Alexandr
2026-04-03 08:45:19 +03:00
parent 85eb61b576
commit 349cea85a3
17 changed files with 571 additions and 24 deletions

View File

@ -1,6 +1,10 @@
import html
import json
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
from onguard24.status_snapshot import build
@ -34,7 +38,42 @@ def _row(name: str, value: object) -> str:
return f"<tr><th>{label}</th><td>{badge}</td></tr>"
async def render_root_page(request) -> str:
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:
data = await build(request)
rows = ""
for key in ("database", "vault", "grafana", "forgejo"):
@ -42,6 +81,8 @@ async def render_root_page(request) -> str:
rows += _row(key, data[key])
payload = html.escape(json.dumps(data, ensure_ascii=False, indent=2))
modules_html = await _module_sections_html(request)
rail = nav_rail_html(None)
return f"""<!DOCTYPE html>
<html lang="ru">
@ -50,7 +91,7 @@ async def render_root_page(request) -> str:
<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; }}
{APP_SHELL_CSS}
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; }}
@ -64,9 +105,23 @@ async def render_root_page(request) -> str:
.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; }}
.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; }}
</style>
</head>
<body>
<div class="app-shell">
<main class="app-main">
<h1>onGuard24</h1>
<p class="links">
<a href="/docs">Swagger</a>
@ -80,9 +135,13 @@ async def render_root_page(request) -> str:
{rows}
</tbody>
</table>
{modules_html}
<div class="json">
<h3>Полный ответ <code>/api/v1/status</code></h3>
<pre>{payload}</pre>
</div>
</main>
{rail}
</div>
</body>
</html>"""