feat(statuspage): keep serving status pages while the api is down

Snapshots are persisted to UPTIME_STATUSPAGE_CACHE_DIR and loaded at
start, so pages survive restarts during an outage. Cached pages are
answered without waiting for the API, refreshed in the background and
served for as long as the API stays unreachable, with a notice once they
are older than five minutes. Only an explicit not found from the API
removes a page. Readiness no longer depends on the API when pages are
cached, the server starts without a reachable API, and certificates are
mirrored locally so HTTPS keeps working after a restart.

Closes #56

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
This commit is contained in:
Sebastian UnterschützandClaude Opus 5.5 committed 2026-11-22 09:05:20 +01:00
1 parent 20664dd5dd
commit 6e2f8d0626
12 files changed
+660 -214

No files matched your search

+4 -1
View File
@@ -104,7 +104,10 @@ func TestStatusPages(t *testing.T) {
t.Run("rendered by the status page server", func(t *testing.T) {
cat, _ := i18n.Load()
srv := statuspage.New(platform, cat, slog.New(slog.DiscardHandler), []string{"status.test"})
srv, err := statuspage.New(platform, cat, slog.New(slog.DiscardHandler), statuspage.Options{Hosts: []string{"status.test"}})
if err != nil {
t.Fatal(err)
}
gin.SetMode(gin.TestMode)
router := gin.New()
srv.Mount(router)
+40 -3
View File
@@ -12,14 +12,18 @@ import (
"github.com/google/uuid"
"git.zb-server.de/ZB-Server/uptimesebi/internal/agentapi"
"git.zb-server.de/ZB-Server/uptimesebi/internal/api"
"git.zb-server.de/ZB-Server/uptimesebi/internal/i18n"
"git.zb-server.de/ZB-Server/uptimesebi/internal/statuspage"
"git.zb-server.de/ZB-Server/uptimesebi/internal/store"
"git.zb-server.de/ZB-Server/uptimesebi/internal/tenant"
)
func TestCertStorage(t *testing.T) {
e := newEnv(t)
ctx := context.Background()
platform := statuspage.NewCertStorage(e.statuspageClient(t, nil))
platform := statuspage.NewCertStorage(e.statuspageClient(t, nil), "")
base := "certificates/" + uuid.NewString()[:8]
key := base + "/example.com/example.com.key"
@@ -60,7 +64,7 @@ func TestCertStorage(t *testing.T) {
other := e.browser(t)
other.signup("x@"+uniq("cs")+".test", uniq("cs-"))
org := uuid.MustParse(other.orgID())
orgStore := statuspage.NewCertStorage(e.statuspageClient(t, &org))
orgStore := statuspage.NewCertStorage(e.statuspageClient(t, &org), "")
if _, err := orgStore.Load(ctx, key); !errors.Is(err, fs.ErrNotExist) {
t.Errorf("org server loaded a platform key: %v", err)
}
@@ -100,7 +104,10 @@ func TestAllowCertificate(t *testing.T) {
record := body["domain_record"].(map[string]any)
cat, _ := i18n.Load()
srv := statuspage.New(e.statuspageClient(t, nil), cat, slog.New(slog.DiscardHandler), []string{"status.test"})
srv, err := statuspage.New(e.statuspageClient(t, nil), cat, slog.New(slog.DiscardHandler), statuspage.Options{Hosts: []string{"status.test"}})
if err != nil {
t.Fatal(err)
}
ctx := context.Background()
if err := srv.AllowCertificate(ctx, "status.test"); err != nil {
t.Errorf("AllowCertificate(default host) = %v", err)
@@ -116,3 +123,33 @@ func TestAllowCertificate(t *testing.T) {
t.Errorf("AllowCertificate(verified domain) = %v", err)
}
}
// TestCertStorageMirror loads certificates from the local mirror when the
// API is unreachable, as after a restart during an outage.
func TestCertStorageMirror(t *testing.T) {
e := newEnv(t)
ctx := context.Background()
dir := t.TempDir()
tok, params := api.NewAgentToken(nil, "statuspage", "mirror", nil)
if err := tenant.New(e.dbPool).Tx(ctx, tenant.System(), func(q *store.Queries) error {
_, err := q.CreateAgent(ctx, params)
return err
}); err != nil {
t.Fatal(err)
}
online := statuspage.NewCertStorage(agentapi.NewClient(e.srv.URL, tok, uuid.New(), "t").WithHTTPClient(e.srv.Client()), dir)
key := "certificates/acme/example.com/example.com.crt"
if err := online.Store(ctx, key, []byte("cert")); err != nil {
t.Fatal(err)
}
offline := statuspage.NewCertStorage(agentapi.NewClient("https://127.0.0.1:1", tok, uuid.New(), "t"), dir)
if got, err := offline.Load(ctx, key); err != nil || string(got) != "cert" {
t.Errorf("Load() with API down = %q, %v; want mirrored value", got, err)
}
if !offline.Exists(ctx, key) {
t.Error("Exists() with API down = false, want true from mirror")
}
if _, err := offline.Load(ctx, "../../etc/passwd"); err == nil {
t.Error("Load() escaped the mirror directory")
}
}