Pages render without JavaScript, are matched by verified custom domain or by slug, and stay available from a stale snapshot for ten minutes if the API is unreachable. Snapshots contain no targets or error details. Refs #21, #22 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
144 lines
3.9 KiB
Go
144 lines
3.9 KiB
Go
// Package statuspage serves public status pages from snapshots fetched
|
|
// through the agent API. It keeps a short in-memory cache and keeps serving
|
|
// the last snapshot for a while if the API is unreachable, so a page stays
|
|
// up during the incidents it is meant to report.
|
|
package statuspage
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"log/slog"
|
|
"net"
|
|
"net/http"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"git.zb-server.de/ZB-Server/uptimesebi/internal/agentapi"
|
|
"git.zb-server.de/ZB-Server/uptimesebi/internal/i18n"
|
|
"git.zb-server.de/ZB-Server/uptimesebi/internal/web"
|
|
)
|
|
|
|
const (
|
|
freshFor = 30 * time.Second
|
|
staleFor = 10 * time.Minute
|
|
)
|
|
|
|
// Source fetches snapshots; *agentapi.Client implements it.
|
|
type Source interface {
|
|
Page(ctx context.Context, host, slug string) (agentapi.PageSnapshot, error)
|
|
}
|
|
|
|
// Server renders pages for one status page agent.
|
|
type Server struct {
|
|
src Source
|
|
catalog *i18n.Catalog
|
|
log *slog.Logger
|
|
// ownHosts are the hosts that serve pages by slug; any other host is
|
|
// looked up as a custom domain.
|
|
ownHosts map[string]bool
|
|
now func() time.Time
|
|
|
|
mu sync.Mutex
|
|
cache map[string]entry
|
|
}
|
|
|
|
type entry struct {
|
|
snap agentapi.PageSnapshot
|
|
missing bool
|
|
fetched time.Time
|
|
}
|
|
|
|
// New returns a Server. ownHosts lists the host names of the default
|
|
// domain, e.g. "status.example.com"; requests to other hosts are matched
|
|
// against verified custom domains.
|
|
func New(src Source, catalog *i18n.Catalog, log *slog.Logger, ownHosts []string) *Server {
|
|
s := &Server{src: src, catalog: catalog, log: log, ownHosts: map[string]bool{}, now: time.Now, cache: map[string]entry{}}
|
|
for _, h := range ownHosts {
|
|
s.ownHosts[strings.ToLower(h)] = true
|
|
}
|
|
return s
|
|
}
|
|
|
|
// Mount registers the page routes.
|
|
func (s *Server) Mount(r *gin.Engine) {
|
|
r.NoRoute(s.serve)
|
|
}
|
|
|
|
// IsCustomDomain reports whether host is served as a custom domain.
|
|
func (s *Server) IsCustomDomain(host string) bool {
|
|
return !s.ownHosts[hostOnly(host)]
|
|
}
|
|
|
|
func hostOnly(h string) string {
|
|
if host, _, err := net.SplitHostPort(h); err == nil {
|
|
h = host
|
|
}
|
|
return strings.ToLower(strings.TrimSuffix(h, "."))
|
|
}
|
|
|
|
func (s *Server) serve(c *gin.Context) {
|
|
if c.Request.Method != http.MethodGet && c.Request.Method != http.MethodHead {
|
|
c.Status(http.StatusMethodNotAllowed)
|
|
return
|
|
}
|
|
host := hostOnly(c.Request.Host)
|
|
var key, slug string
|
|
switch {
|
|
case s.IsCustomDomain(host):
|
|
if c.Request.URL.Path != "/" {
|
|
c.Status(http.StatusNotFound)
|
|
return
|
|
}
|
|
key = "host:" + host
|
|
default:
|
|
slug = strings.Trim(c.Request.URL.Path, "/")
|
|
if slug == "" || strings.Contains(slug, "/") {
|
|
c.Status(http.StatusNotFound)
|
|
return
|
|
}
|
|
key = "slug:" + slug
|
|
host = ""
|
|
}
|
|
snap, ok := s.snapshot(c.Request.Context(), key, host, slug)
|
|
if !ok {
|
|
c.Status(http.StatusNotFound)
|
|
return
|
|
}
|
|
l := s.catalog.Match(c.GetHeader("Accept-Language"))
|
|
c.Request = c.Request.WithContext(i18n.NewContext(c.Request.Context(), l))
|
|
c.Header("Cache-Control", "public, max-age=30")
|
|
c.Header("Content-Language", l.Lang())
|
|
web.Render(c, http.StatusOK, web.PublicStatusPage(snap, web.PublicAssets{CSSHref: "/static/dist/app.css"}))
|
|
}
|
|
|
|
func (s *Server) snapshot(ctx context.Context, key, host, slug string) (agentapi.PageSnapshot, bool) {
|
|
now := s.now()
|
|
s.mu.Lock()
|
|
e, cached := s.cache[key]
|
|
s.mu.Unlock()
|
|
if cached && now.Sub(e.fetched) < freshFor {
|
|
return e.snap, !e.missing
|
|
}
|
|
snap, err := s.src.Page(ctx, host, slug)
|
|
switch {
|
|
case err == nil:
|
|
e = entry{snap: snap, fetched: now}
|
|
case errors.Is(err, agentapi.ErrNotFound):
|
|
e = entry{missing: true, fetched: now}
|
|
default:
|
|
if cached && now.Sub(e.fetched) < staleFor {
|
|
s.log.WarnContext(ctx, "serving stale status page", "page", key, "error", err)
|
|
return e.snap, !e.missing
|
|
}
|
|
s.log.ErrorContext(ctx, "fetching status page failed", "page", key, "error", err)
|
|
return agentapi.PageSnapshot{}, false
|
|
}
|
|
s.mu.Lock()
|
|
s.cache[key] = e
|
|
s.mu.Unlock()
|
|
return e.snap, !e.missing
|
|
}
|