Files
uptimesebi/internal/web/public.templ
T
Sebastian UnterschützandClaude Opus 5.5 cca4e80e54
ci / lint (push) Skipped
ci / test (push) Skipped
ci / build (push) Skipped
ci / image (push) Skipped
ci / test (pull_request) Successful in 9m5s
ci / lint (pull_request) Successful in 10m24s
ci / build (pull_request) Successful in 18m10s
ci / image (pull_request) Successful in 5m16s
feat(statuspages): email subscriptions, RSS feed and a JSON API
Visitors can subscribe to a page's incidents by email where the
organization allows it. They confirm their address first; confirmation
and unsubscribe links only show a button, so mail scanners do not act on
them, and mails carry RFC 8058 one-click unsubscribe headers. Subscribers
get a mail when an incident opens or resolves and for every public
update.

Every page also has an RSS feed of incidents and planned maintenance and
a read-only JSON API (status, summary, components, incidents), both
served from the cached snapshot.

Abuse protection: status page servers limit feed and API reads,
subscription requests and link actions per visitor; the API limits
subscription requests per visitor, mailbox and page, and a new limit caps
the subscriptions an organization's pages gain in seven days (platform
default and per-organization override, 1000 in the cloud). Unconfirmed
subscriptions count towards it and are deleted after a week.

The fixed cap of 200 monitors per status page is gone; the configurable
limit alone decides.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
2026-11-26 18:18:08 +01:00

433 lines
16 KiB
Templ
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package web
import (
"strconv"
"strings"
"time"
"git.zb-server.de/ZB-Server/uptimesebi/internal/agentapi"
"git.zb-server.de/ZB-Server/uptimesebi/internal/i18n"
)
templ publicShell(s agentapi.PageSnapshot, a PublicAssets, docTitle string) {
<!DOCTYPE html>
<html lang={ i18n.Lang(ctx) }>
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
if !a.NoRefresh {
<meta http-equiv="refresh" content="60"/>
}
if a.ComponentBase != "" {
<link rel="alternate" type="application/rss+xml" title={ s.Title } href={ a.ComponentBase + "feed.rss" }/>
}
<title>{ docTitle }</title>
if a.InlineCSS == "" {
@faviconLinks()
}
if a.InlineCSS != "" {
@templ.Raw("<style>" + a.InlineCSS + "</style>")
} else {
<link rel="stylesheet" href={ a.CSSHref }/>
}
if s.Theme.Accent != "" {
@templ.Raw("<style>:root{--page-accent:" + s.Theme.Accent + "}</style>")
}
if s.Theme.CSS != "" {
@templ.Raw("<style>" + s.Theme.CSS + "</style>")
}
</head>
<body class="min-h-screen">
<main class="mx-auto max-w-3xl px-4 py-8 sm:px-6 sm:py-14">
{ children... }
<footer class="flex flex-wrap justify-between gap-x-4 gap-y-2 border-t border-rule pt-4 font-mono text-xs text-muted">
<span>{ i18n.T(ctx, "page.updated", "Time", s.GeneratedAt.UTC().Format(time.DateTime)+" UTC") }</span>
<span class="flex flex-wrap justify-end gap-x-4 gap-y-1">
if a.ComponentBase != "" {
<a href={ templ.SafeURL(a.ComponentBase + "feed.rss") } class="hover:text-ink">{ i18n.T(ctx, "page.feed") }</a>
<a href={ templ.SafeURL(a.ComponentBase + "api/v1/summary") } class="hover:text-ink">{ i18n.T(ctx, "page.api") }</a>
}
for _, l := range PageLegalLinks(s, a.LegalBase) {
@legalLink(l)
}
<span class="flex items-center gap-1">
@brandMark("h-3.5 w-3.5")
UptimeSebi
</span>
</span>
</footer>
</main>
</body>
</html>
}
templ publicStaleNotice(a PublicAssets) {
if !a.StaleSince.IsZero() {
<p class="mb-6 border border-degraded/50 bg-degraded/10 px-4 py-2 text-sm">
{ i18n.T(ctx, "page.stale", "Time", a.StaleSince.UTC().Format("2006-01-02 15:04")+" UTC") }
</p>
}
}
templ PublicStatusPage(s agentapi.PageSnapshot, a PublicAssets) {
@publicShell(s, a, s.Title) {
<header class="mb-10">
if s.Theme.LogoURL != "" {
<img src={ s.Theme.LogoURL } alt={ s.Title } class="mb-6 h-10"/>
}
<h1 class="text-2xl font-semibold" style="color:var(--page-accent,inherit)">{ s.Title }</h1>
if s.Description != "" {
<div class="legal-text mt-2 text-muted">
@templ.Raw(Markdown(s.Description))
</div>
}
</header>
@publicStaleNotice(a)
<section
class={ "mb-12 border-l-4 py-3 pl-4",
templ.KV("border-up", s.Status == agentapi.PageOperational),
templ.KV("border-degraded", s.Status == agentapi.PageDegraded),
templ.KV("border-down", s.Status == agentapi.PageOutage),
templ.KV("border-unknown", s.Status == agentapi.PageUnknown),
templ.KV("border-maintenance", s.Status == agentapi.PageUnderMaintenance) }
>
<p class="text-lg font-medium">{ i18n.T(ctx, "page.status."+s.Status) }</p>
</section>
@pageMaintenance(i18n.T(ctx, "page.maintenance_ongoing"), s.Ongoing(a.Now))
if len(s.Sections) > 0 {
<section class="mb-12 border-t border-rule">
for _, sec := range s.Sections {
@publicSection(sec, s, a)
}
</section>
}
@publicIncidents(s.Incidents)
@pageMaintenance(i18n.T(ctx, "page.maintenance_upcoming"), s.Upcoming(a.Now))
if s.Subscribe && a.ComponentBase != "" {
@publicSubscribe(s, a)
}
}
}
// publicSubscribe offers email subscriptions to the page's incidents.
templ publicSubscribe(s agentapi.PageSnapshot, a PublicAssets) {
<section id="subscribe" class="mb-12 border-t border-rule pt-6">
<h2 class="mb-1 text-xs font-medium uppercase tracking-wider text-muted">{ i18n.T(ctx, "page.subscribe.title") }</h2>
<p class="mb-3 text-sm">{ i18n.T(ctx, "page.subscribe.intro") }</p>
<form method="post" action={ templ.SafeURL(a.ComponentBase + "subscribe") } class="flex max-w-md flex-wrap gap-2">
<label class="sr-only" for="subscribe-email">{ i18n.T(ctx, "page.subscribe.email") }</label>
<input id="subscribe-email" type="email" name="email" required maxlength="254" autocomplete="email" placeholder={ i18n.T(ctx, "page.subscribe.email") } class="min-w-0 flex-1 border border-rule bg-transparent px-2 py-1.5 text-sm"/>
<button type="submit" class="border border-ink px-3 py-1.5 text-sm hover:bg-ink hover:text-paper">{ i18n.T(ctx, "page.subscribe.submit") }</button>
</form>
<p class="mt-2 text-xs text-muted">{ i18n.T(ctx, "page.subscribe.privacy") }</p>
</section>
}
// PublicNoticePage tells a visitor of the page the outcome of a
// subscription request, or asks to confirm one with form.
templ PublicNoticePage(s agentapi.PageSnapshot, a PublicAssets, title, message string, form *NoticeForm) {
@publicShell(s, a, title+" · "+s.Title) {
<header class="mb-10">
<p class="mb-2 font-mono text-xs text-muted">{ s.Title }</p>
<h1 class="text-2xl font-semibold" style="color:var(--page-accent,inherit)">{ title }</h1>
</header>
<p class="mb-8 max-w-xl">{ message }</p>
if form != nil {
<form method="post" action={ templ.SafeURL(form.Action) } class="mb-8">
<input type="hidden" name="token" value={ form.Token }/>
<button type="submit" class="border border-ink px-3 py-1.5 text-sm hover:bg-ink hover:text-paper">{ form.Button }</button>
</form>
}
<p class="mb-12 text-sm"><a href={ templ.SafeURL(a.ComponentBase) } class="underline underline-offset-2">{ i18n.T(ctx, "page.back") }</a></p>
}
}
// publicSection renders an unnamed root component list or a collapsible nested
// group matching the monitor dashboard hierarchy.
templ publicSection(sec agentapi.Section, s agentapi.PageSnapshot, a PublicAssets) {
if sec.Name == "" {
@publicComponentList(sec.Components, s, a)
} else {
<details open class="border-b border-rule">
<summary class="flex cursor-pointer items-center gap-2.5 py-3 text-sm font-medium select-none hover:bg-rule/30">
@statusDot(SectionStatus(sec))
<span>{ sec.Name }</span>
<span class="font-mono text-xs font-normal text-muted">{ strconv.Itoa(SectionTotal(sec)) }</span>
<span class="ml-auto text-xs font-normal text-muted">{ i18n.T(ctx, "page.component."+string(secStatusName(sec))) }</span>
</summary>
<div class="ml-4 border-l border-rule pl-4">
@publicComponentList(sec.Components, s, a)
for _, child := range sec.Children {
@publicSection(child, s, a)
}
</div>
</details>
}
}
func secStatusName(sec agentapi.Section) string {
if sec.Status != "" {
return sec.Status
}
states := make([]string, len(sec.Components))
for i, c := range sec.Components {
states[i] = c.Status
}
return agentapi.Summarize(states)
}
templ publicComponentList(comps []agentapi.Component, s agentapi.PageSnapshot, a PublicAssets) {
if len(comps) > 0 {
<ul class="divide-y divide-rule border-b border-rule last:border-b-0">
for _, comp := range comps {
<li class="py-3">
if href := ComponentHref(a.ComponentBase, comp, s.ShowDetails); href != "" {
<a href={ templ.SafeURL(href) } class="block rounded-sm transition hover:bg-rule/30">
@publicComponentBody(comp, s, true)
</a>
} else {
@publicComponentBody(comp, s, false)
if s.ShowDetails && comp.Details != nil {
<details class="mt-2 border-t border-rule pt-2 text-xs">
<summary class="cursor-pointer text-muted hover:text-ink">{ i18n.T(ctx, "page.details_expand") }</summary>
<div class="mt-3">
@publicSLAGrid(comp.Details)
if len(comp.Details.Regions) > 0 {
@publicRegionsList(comp.Details.Regions)
}
</div>
</details>
}
}
</li>
}
</ul>
}
}
templ publicComponentBody(comp agentapi.Component, s agentapi.PageSnapshot, clickable bool) {
<div class="mb-2 flex items-center gap-2.5">
@statusDot(PageStatus(comp.Status))
<span class={ templ.KV("underline-offset-2 hover:underline", clickable) }>{ comp.Name }</span>
<span class="ml-auto flex items-center gap-2 text-sm text-muted">
if inc, ok := s.OpenIncidentStatus(comp.Name); ok {
<span class="text-xs">
@incidentStatusLabel(IncidentStatusInfo{Key: inc.Status, Name: inc.StatusName, Color: inc.StatusColor}, true)
</span>
<span aria-hidden="true">·</span>
}
<span>{ i18n.T(ctx, "page.component."+comp.Status) }</span>
if clickable {
<span aria-hidden="true" class="text-xs">→</span>
}
</span>
</div>
@bars(DayBars(comp.Days), 22)
<div class="mt-1 flex justify-between font-mono text-xs text-muted">
<span>{ i18n.T(ctx, "page.days_ago", "Count", len(comp.Days)) }</span>
<span>{ UptimeText(comp.Uptime) }</span>
<span>{ i18n.T(ctx, "page.today") }</span>
</div>
}
templ PublicComponentPage(s agentapi.PageSnapshot, comp agentapi.Component, a PublicAssets) {
@publicShell(s, a, comp.Name+" · "+s.Title) {
<p class="mb-6 text-sm">
<a href={ templ.SafeURL(BackHref(a.ComponentBase)) } class="text-muted hover:text-ink">← { s.Title }</a>
</p>
<header class="mb-8">
<h1 class="flex items-center gap-2.5 text-2xl font-semibold" style="color:var(--page-accent,inherit)">
@statusDot(PageStatus(comp.Status))
<span>{ comp.Name }</span>
</h1>
</header>
@publicStaleNotice(a)
<section
class={ "mb-10 border-l-4 py-3 pl-4",
templ.KV("border-up", comp.Status == agentapi.PageOperational),
templ.KV("border-degraded", comp.Status == agentapi.PageDegraded),
templ.KV("border-down", comp.Status == agentapi.PageOutage),
templ.KV("border-unknown", comp.Status == agentapi.PageUnknown),
templ.KV("border-maintenance", comp.Status == agentapi.PageUnderMaintenance) }
>
<p class="text-base font-medium">{ i18n.T(ctx, "page.component."+comp.Status) }</p>
</section>
{{ ongoing, upcoming := s.MaintenanceForComponent(comp.Name, a.Now) }}
@pageMaintenance(i18n.T(ctx, "page.maintenance_ongoing"), ongoing)
if comp.Details != nil {
<section class="mb-10">
<div class="mb-2 flex items-baseline justify-between">
<h2 class="text-xs font-medium uppercase tracking-wider text-muted">{ i18n.T(ctx, "page.details_last_24h") }</h2>
<span class="font-mono text-sm">{ UptimeText(comp.Details.SLA24h) }</span>
</div>
@bars(HourBars(comp.Details.Hours), 24)
{{ lat := HourLatency(comp.Details.Hours, 600, 96) }}
if lat.Points != "" {
<div class="mt-4 mb-1 flex justify-between font-mono text-xs text-muted">
<span>{ i18n.T(ctx, "monitors.latency") }</span>
<span>{ i18n.T(ctx, "page.details_latency_summary", "Avg", AvgLatencyText(comp.Details.Hours), "Max", strconv.Itoa(lat.MaxMS)+" ms") }</span>
</div>
<svg viewBox={ "0 0 " + strconv.Itoa(lat.Width) + " " + strconv.Itoa(lat.Height) } preserveAspectRatio="none" class="block h-24 w-full border-b border-rule">
<polyline points={ lat.Points } fill="none" class="stroke-ink" stroke-width="1.5" vector-effect="non-scaling-stroke"></polyline>
</svg>
}
</section>
<section class="mb-10">
<h2 class="mb-3 text-xs font-medium uppercase tracking-wider text-muted">{ i18n.T(ctx, "monitors.sla_table") }</h2>
@publicSLAGrid(comp.Details)
</section>
if len(comp.Details.Regions) > 0 {
<section class="mb-10">
<h2 class="mb-3 text-xs font-medium uppercase tracking-wider text-muted">{ i18n.T(ctx, "monitors.regions") }</h2>
@publicRegionsList(comp.Details.Regions)
</section>
}
}
<section class="mb-10">
<div class="mb-2 flex items-baseline justify-between">
<h2 class="text-xs font-medium uppercase tracking-wider text-muted">{ i18n.T(ctx, "monitors.last_90d") }</h2>
<span class="font-mono text-sm">{ UptimeText(comp.Uptime) }</span>
</div>
@bars(DayBars(comp.Days), 24)
<div class="mt-1 flex justify-between font-mono text-xs text-muted">
<span>{ i18n.T(ctx, "page.days_ago", "Count", len(comp.Days)) }</span>
<span>{ UptimeText(comp.Uptime) }</span>
<span>{ i18n.T(ctx, "page.today") }</span>
</div>
</section>
@publicIncidents(s.IncidentsForComponent(comp.Name))
@pageMaintenance(i18n.T(ctx, "page.maintenance_upcoming"), upcoming)
}
}
templ publicSLAGrid(d *agentapi.ComponentDetails) {
<dl class="grid grid-cols-2 gap-px border border-rule bg-rule sm:grid-cols-4">
for _, item := range []struct {
label string
value float64
}{
{"24h", d.SLA24h},
{"7d", d.SLA7d},
{"30d", d.SLA30d},
{"90d", d.SLA90d},
} {
<div class="bg-paper px-3 py-2">
<dt class="font-mono text-xs text-muted">{ item.label }</dt>
<dd class="font-mono text-sm">{ UptimeText(item.value) }</dd>
</div>
}
</dl>
}
templ publicRegionsList(regions []agentapi.ComponentRegion) {
<ul class="divide-y divide-rule border-y border-rule text-sm">
for _, r := range regions {
<li class="flex items-center gap-2.5 py-2">
@statusDot(PageStatus(r.Status))
<span>{ r.Name }</span>
<span class="ml-auto font-mono text-xs text-muted">
{ i18n.T(ctx, "page.component."+r.Status) }
if r.LatencyMS > 0 {
{ " · " + strconv.Itoa(r.LatencyMS) + " ms" }
}
</span>
</li>
}
</ul>
}
templ publicIncidents(incidents []agentapi.PageIncident) {
<section class="mb-12">
<h2 class="mb-3 text-xs font-medium uppercase tracking-wider text-muted">{ i18n.T(ctx, "page.incidents") }</h2>
if len(incidents) == 0 {
<p class="text-sm text-muted">{ i18n.T(ctx, "page.no_incidents") }</p>
} else {
<ul class="divide-y divide-rule border-y border-rule text-sm">
for _, inc := range incidents {
<li class="py-3">
<div class="flex flex-wrap items-baseline gap-x-4 gap-y-1">
@incidentStatusLabel(IncidentStatusInfo{Key: inc.Status, Name: inc.StatusName, Color: inc.StatusColor}, inc.ResolvedAt == nil)
if inc.Title != "" {
<span class="font-medium">{ inc.Title }</span>
<span class="text-xs text-muted">{ inc.Component }</span>
} else {
<span>{ inc.Component }</span>
}
<span class="ml-auto font-mono text-xs text-muted">
{ inc.StartedAt.UTC().Format("2006-01-02 15:04") }
if inc.ResolvedAt != nil {
– { inc.ResolvedAt.UTC().Format("15:04") }
}
UTC
</span>
</div>
if len(inc.Updates) > 0 {
<ol class="ml-1 mt-3 space-y-3 border-l border-rule pl-4">
for _, u := range inc.Updates {
<li>
<p class="text-xs">
@incidentStatusLabel(IncidentStatusInfo{Key: u.Status, Name: u.StatusName, Color: u.StatusColor}, true)
<span class="font-mono text-muted">{ " · " + u.At.UTC().Format("2006-01-02 15:04") + " UTC" }</span>
</p>
<div class="legal-text mt-1">
@templ.Raw(Markdown(u.Message))
</div>
</li>
}
</ol>
}
</li>
}
</ul>
}
</section>
}
// incidentStatusLabel names an incident's status: what the organization
// said, in its own color for its own statuses, or ongoing or resolved when
// it said nothing.
templ incidentStatusLabel(st IncidentStatusInfo, open bool) {
switch {
case st.Key == "custom":
<span class="inline-flex items-center gap-1.5 font-medium">
<span class="inline-block size-2 rounded-full" style={ "background-color:" + st.Color }></span>
{ st.Name }
</span>
case st.Key != "":
<span class={ "font-medium", incidentStatusClass(st.Key) }>{ i18n.T(ctx, "incident.status."+st.Key) }</span>
case open:
<span class="font-medium text-down">{ i18n.T(ctx, "incidents.ongoing") }</span>
default:
<span class="text-up">{ i18n.T(ctx, "incidents.resolved") }</span>
}
}
templ pageMaintenance(heading string, items []agentapi.PageMaintenance) {
if len(items) > 0 {
<section class="mb-12">
<h2 class="mb-3 text-xs font-medium uppercase tracking-wider text-muted">{ heading }</h2>
<ul class="divide-y divide-rule border-y border-rule text-sm">
for _, m := range items {
<li class="py-3">
<div class="flex flex-wrap items-baseline gap-x-4 gap-y-1">
<span class="flex items-center gap-2 font-medium">
@statusDot(StatusMaintenance)
{ m.Title }
</span>
<span class="ml-auto font-mono text-xs text-muted">
{ m.Start.UTC().Format("2006-01-02 15:04") + " – " + m.End.UTC().Format("15:04") + " UTC" }
</span>
</div>
<p class="ml-4 mt-1 text-xs text-muted">{ strings.Join(m.Components, ", ") }</p>
if m.Description != "" {
<div class="legal-text ml-4 mt-2 text-muted">
@templ.Raw(Markdown(m.Description))
</div>
}
</li>
}
</ul>
</section>
}
}