feat(statuspage): serve public status pages from api snapshots
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
This commit is contained in:
1 parent
d820547e8c
commit
2b72715a9d
8 files changed
+1017
No files matched your search
@@ -0,0 +1,104 @@
|
||||
package statuspage
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"git.zb-server.de/ZB-Server/uptimesebi/internal/agentapi"
|
||||
"git.zb-server.de/ZB-Server/uptimesebi/internal/i18n"
|
||||
)
|
||||
|
||||
type fakeSource struct {
|
||||
calls int
|
||||
err error
|
||||
pages map[string]agentapi.PageSnapshot
|
||||
}
|
||||
|
||||
func (f *fakeSource) Page(_ context.Context, host, slug string) (agentapi.PageSnapshot, error) {
|
||||
f.calls++
|
||||
if f.err != nil {
|
||||
return agentapi.PageSnapshot{}, f.err
|
||||
}
|
||||
p, ok := f.pages[host+"|"+slug]
|
||||
if !ok {
|
||||
return p, agentapi.ErrNotFound
|
||||
}
|
||||
return p, nil
|
||||
}
|
||||
|
||||
func newTest(t *testing.T) (*Server, *fakeSource, *gin.Engine, *time.Time) {
|
||||
t.Helper()
|
||||
cat, err := i18n.Load()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
src := &fakeSource{pages: map[string]agentapi.PageSnapshot{
|
||||
"|acme": {Title: "Acme status", Status: agentapi.PageOperational},
|
||||
"status.acme.test|": {Title: "Acme on own domain", Status: agentapi.PageOutage},
|
||||
}}
|
||||
s := New(src, cat, slog.New(slog.DiscardHandler), []string{"status.example.test"})
|
||||
now := time.Now()
|
||||
s.now = func() time.Time { return now }
|
||||
gin.SetMode(gin.TestMode)
|
||||
r := gin.New()
|
||||
s.Mount(r)
|
||||
return s, src, r, &now
|
||||
}
|
||||
|
||||
func get(r http.Handler, host, path string) (int, string) {
|
||||
req := httptest.NewRequest(http.MethodGet, path, nil)
|
||||
req.Host = host
|
||||
req.Header.Set("Accept-Language", "de")
|
||||
w := httptest.NewRecorder()
|
||||
r.ServeHTTP(w, req)
|
||||
return w.Code, w.Body.String()
|
||||
}
|
||||
|
||||
func TestRouting(t *testing.T) {
|
||||
_, _, r, _ := newTest(t)
|
||||
tests := []struct {
|
||||
host, path string
|
||||
want int
|
||||
contains string
|
||||
}{
|
||||
{"status.example.test", "/acme", 200, "Acme status"},
|
||||
{"status.example.test:8082", "/acme", 200, "Alle Systeme funktionieren"},
|
||||
{"status.example.test", "/missing", 404, ""},
|
||||
{"status.example.test", "/", 404, ""},
|
||||
{"status.acme.test", "/", 200, "Acme on own domain"},
|
||||
{"status.acme.test", "/acme", 404, ""},
|
||||
{"other.test", "/", 404, ""},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
code, body := get(r, tt.host, tt.path)
|
||||
if code != tt.want || !strings.Contains(body, tt.contains) {
|
||||
t.Errorf("GET %s%s = %d (contains %q: %t), want %d", tt.host, tt.path, code, tt.contains, strings.Contains(body, tt.contains), tt.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCacheAndStaleFallback(t *testing.T) {
|
||||
_, src, r, now := newTest(t)
|
||||
get(r, "status.example.test", "/acme")
|
||||
get(r, "status.example.test", "/acme")
|
||||
if src.calls != 1 {
|
||||
t.Errorf("source calls within freshness window = %d, want 1", src.calls)
|
||||
}
|
||||
src.err = errors.New("api down")
|
||||
*now = now.Add(time.Minute)
|
||||
if code, body := get(r, "status.example.test", "/acme"); code != 200 || !strings.Contains(body, "Acme status") {
|
||||
t.Errorf("GET with API down = %d, want stale page", code)
|
||||
}
|
||||
*now = now.Add(time.Hour)
|
||||
if code, _ := get(r, "status.example.test", "/acme"); code != 404 {
|
||||
t.Errorf("GET with API down long after = %d, want 404", code)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user