feat(statuspages): error page linking a home page set by admins
ci / lint (push) Skipped
ci / test (push) Skipped
ci / build (push) Skipped
ci / image (push) Skipped
ci / lint (pull_request) Successful in 6m52s
ci / test (pull_request) Successful in 15m30s
ci / build (pull_request) Successful in 16m56s
ci / image (pull_request) Successful in 6m1s
ci / lint (push) Skipped
ci / test (push) Skipped
ci / build (push) Skipped
ci / image (push) Skipped
ci / lint (pull_request) Successful in 6m52s
ci / test (pull_request) Successful in 15m30s
ci / build (pull_request) Successful in 16m56s
ci / image (pull_request) Successful in 6m1s
The bare status page domain and pages that do not exist answered with a plain "404 page not found". They now show an error page with a button to the platform's home page, which platform admins set under Admin. The status page servers receive it with the page index and keep it across restarts. 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
af992409ef
commit
1f90fe8470
17 files changed
+902
-395
No files matched your search
@@ -63,7 +63,7 @@ type Source interface {
|
||||
// PageIfChanged answers agentapi.ErrNotModified when the page still
|
||||
// has checksum.
|
||||
PageIfChanged(ctx context.Context, host, slug, checksum string) (agentapi.PageSnapshot, error)
|
||||
Pages(ctx context.Context) ([]agentapi.PageRef, error)
|
||||
Pages(ctx context.Context) (agentapi.PageIndex, error)
|
||||
}
|
||||
|
||||
// Server renders pages for one status page agent.
|
||||
@@ -78,7 +78,9 @@ type Server struct {
|
||||
dir string
|
||||
now func() time.Time
|
||||
|
||||
mu sync.Mutex
|
||||
mu sync.Mutex
|
||||
// home is the platform's home page, linked from the error page.
|
||||
home string
|
||||
cache map[string]entry
|
||||
refreshing map[string]bool
|
||||
// again holds pages that changed while being refreshed, which are
|
||||
@@ -161,9 +163,39 @@ func (s *Server) loadDisk() error {
|
||||
if len(s.cache) > 0 {
|
||||
s.log.Info("loaded cached status pages", "count", len(s.cache))
|
||||
}
|
||||
if raw, err := os.ReadFile(filepath.Join(s.dir, homeFile)); err == nil {
|
||||
s.home = string(raw)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// homeFile keeps the home page across restarts; not .json, which are pages.
|
||||
const homeFile = "home.url"
|
||||
|
||||
// setHome records the home page the error page links.
|
||||
func (s *Server) setHome(url string) {
|
||||
s.mu.Lock()
|
||||
changed := s.home != url
|
||||
s.home = url
|
||||
s.mu.Unlock()
|
||||
if changed && s.dir != "" {
|
||||
if err := os.WriteFile(filepath.Join(s.dir, homeFile), []byte(url), 0o600); err != nil {
|
||||
s.log.Warn("saving the home page failed", "error", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// notFound answers visitors of a page that does not exist with a page
|
||||
// leading to the platform's home page.
|
||||
func (s *Server) notFound(c *gin.Context) {
|
||||
s.localizeRequest(c)
|
||||
s.mu.Lock()
|
||||
home := s.home
|
||||
s.mu.Unlock()
|
||||
c.Header("Cache-Control", "no-store")
|
||||
web.Render(c, http.StatusNotFound, web.PublicNotFoundPage("/static/dist/app.css", home))
|
||||
}
|
||||
|
||||
func (s *Server) fileFor(key string) string {
|
||||
sum := sha256.Sum256([]byte(key))
|
||||
return filepath.Join(s.dir, hex.EncodeToString(sum[:16])+".json")
|
||||
@@ -237,7 +269,7 @@ func (s *Server) serve(c *gin.Context) {
|
||||
key, base = "host:"+host, "/"
|
||||
} else {
|
||||
if len(parts) == 0 {
|
||||
c.Status(http.StatusNotFound)
|
||||
s.notFound(c)
|
||||
return
|
||||
}
|
||||
slug, parts = parts[0], parts[1:]
|
||||
@@ -261,7 +293,11 @@ func (s *Server) serve(c *gin.Context) {
|
||||
}
|
||||
e, ok := s.lookup(c.Request.Context(), key, host, slug)
|
||||
if !ok {
|
||||
c.Status(http.StatusNotFound)
|
||||
if rt.kind == routeFeed || rt.kind == routeAPI {
|
||||
c.Status(http.StatusNotFound)
|
||||
} else {
|
||||
s.notFound(c)
|
||||
}
|
||||
return
|
||||
}
|
||||
if rt.kind == routeLegal {
|
||||
@@ -523,11 +559,13 @@ func (s *Server) Run(ctx context.Context) {
|
||||
func (s *Server) Sync(ctx context.Context) {
|
||||
ctx, cancel := context.WithTimeout(ctx, syncInterval)
|
||||
defer cancel()
|
||||
refs, err := s.src.Pages(ctx)
|
||||
index, err := s.src.Pages(ctx)
|
||||
if err != nil {
|
||||
s.log.WarnContext(ctx, "listing status pages failed", "error", err)
|
||||
return
|
||||
}
|
||||
s.setHome(index.HomeURL)
|
||||
refs := index.Pages
|
||||
todo := map[string]entry{}
|
||||
s.mu.Lock()
|
||||
for key, e := range s.cache {
|
||||
|
||||
@@ -25,6 +25,7 @@ type fakeSource struct {
|
||||
err error
|
||||
delay time.Duration
|
||||
pages map[string]agentapi.PageSnapshot
|
||||
home string
|
||||
}
|
||||
|
||||
// PageIfChanged counts calls and answers like the API, sending pages
|
||||
@@ -65,16 +66,16 @@ func (f *fakeSource) page(ctx context.Context, host, slug string) (agentapi.Page
|
||||
}
|
||||
|
||||
// Pages lists the fake's pages by their keys: "host|" or "|slug".
|
||||
func (f *fakeSource) Pages(context.Context) ([]agentapi.PageRef, error) {
|
||||
func (f *fakeSource) Pages(context.Context) (agentapi.PageIndex, error) {
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
if f.err != nil {
|
||||
return nil, f.err
|
||||
return agentapi.PageIndex{}, f.err
|
||||
}
|
||||
var out []agentapi.PageRef
|
||||
out := agentapi.PageIndex{HomeURL: f.home}
|
||||
for k := range f.pages {
|
||||
host, slug, _ := strings.Cut(k, "|")
|
||||
out = append(out, agentapi.PageRef{Host: host, Slug: slug})
|
||||
out.Pages = append(out.Pages, agentapi.PageRef{Host: host, Slug: slug})
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
@@ -128,6 +129,26 @@ func (h harness) get(host, path string) (int, string) {
|
||||
return w.Code, w.Body.String()
|
||||
}
|
||||
|
||||
// The error page links the platform's home page, also after a restart
|
||||
// while the API is down.
|
||||
func TestNotFoundLinksHome(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
now := time.Now()
|
||||
src := newSource()
|
||||
h := newHarness(t, src, dir, &now)
|
||||
if code, body := h.get("status.example.test", "/"); code != 404 || strings.Contains(body, "Zur Hauptseite") {
|
||||
t.Errorf("error page without a home page = %d, links one: %t", code, strings.Contains(body, "Zur Hauptseite"))
|
||||
}
|
||||
src.home = "https://example.test/"
|
||||
h.srv.Sync(context.Background())
|
||||
restarted := newHarness(t, &fakeSource{err: errors.New("connection refused")}, dir, &now)
|
||||
for _, hs := range []harness{h, restarted} {
|
||||
if code, body := hs.get("status.example.test", "/gone"); code != 404 || !strings.Contains(body, `href="https://example.test/"`) {
|
||||
t.Errorf("error page = %d, lacks the link to the home page", code)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRouting(t *testing.T) {
|
||||
now := time.Now()
|
||||
h := newHarness(t, newSource(), t.TempDir(), &now)
|
||||
@@ -138,8 +159,8 @@ func TestRouting(t *testing.T) {
|
||||
}{
|
||||
{"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.example.test", "/missing", 404, "Seite nicht gefunden"},
|
||||
{"status.example.test", "/", 404, "Seite nicht gefunden"},
|
||||
{"status.acme.test", "/", 200, "Acme on own domain"},
|
||||
{"status.acme.test", "/acme", 404, ""},
|
||||
{"other.test", "/", 404, ""},
|
||||
|
||||
Reference in new issue
Block a user