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:
1 parent
20664dd5dd
commit
6e2f8d0626
12 files changed
+660
-214
No files matched your search
+46
-9
@@ -11,6 +11,7 @@ import (
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"github.com/caddyserver/certmagic"
|
||||
@@ -49,31 +50,36 @@ func run(log *slog.Logger, level *slog.LevelVar) error {
|
||||
defer stop()
|
||||
|
||||
client := agentapi.NewClient(cfg.APIURL, cfg.Token, uuid.New(), buildinfo.Version)
|
||||
hello, err := platform.Hello(ctx, client, log)
|
||||
if err != nil {
|
||||
if err := checkToken(ctx, client, log); err != nil {
|
||||
return err
|
||||
}
|
||||
if hello.Kind != "statuspage" {
|
||||
return fmt.Errorf("token belongs to a %s agent, not a status page server", hello.Kind)
|
||||
cacheDir, err := resolveCacheDir(cfg.CacheDir)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
catalog, err := i18n.Load()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
pages, err := statuspage.New(client, catalog, log, statuspage.Options{Hosts: cfg.Hosts, CacheDir: filepath.Join(cacheDir, "pages")})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
apiProbe := func(ctx context.Context) error { _, err := client.Hello(ctx); return err }
|
||||
health := platform.NewHealth(map[string]platform.Probe{
|
||||
"api": func(ctx context.Context) error { _, err := client.Hello(ctx); return err },
|
||||
"pages": func(ctx context.Context) error { return pages.Ready(ctx, apiProbe) },
|
||||
})
|
||||
r := platform.NewRouter(log)
|
||||
health.Register(r)
|
||||
r.StaticFS("/static", web.Static())
|
||||
pages := statuspage.New(client, catalog, log, cfg.Hosts)
|
||||
pages.Mount(r)
|
||||
go pages.Run(ctx)
|
||||
|
||||
if !cfg.TLS {
|
||||
return platform.Serve(ctx, log, platform.NewHTTPServer(cfg.Listen, r), health, 5*time.Second, cfg.ShutdownTimeout)
|
||||
}
|
||||
tlsConfig, challenges, err := certificates(ctx, cfg, client, pages, log)
|
||||
tlsConfig, challenges, err := certificates(ctx, cfg, client, pages, filepath.Join(cacheDir, "certs"), log)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -91,13 +97,13 @@ func run(log *slog.Logger, level *slog.LevelVar) error {
|
||||
|
||||
// certificates sets up on-demand ACME certificates stored through the API,
|
||||
// shared by every instance of this status page server.
|
||||
func certificates(ctx context.Context, cfg config.StatusPage, client *agentapi.Client, pages *statuspage.Server, log *slog.Logger) (*tls.Config, func(http.Handler) http.Handler, error) {
|
||||
func certificates(ctx context.Context, cfg config.StatusPage, client *agentapi.Client, pages *statuspage.Server, certDir string, log *slog.Logger) (*tls.Config, func(http.Handler) http.Handler, error) {
|
||||
var magic *certmagic.Config
|
||||
cache := certmagic.NewCache(certmagic.CacheOptions{
|
||||
GetConfigForCert: func(certmagic.Certificate) (*certmagic.Config, error) { return magic, nil },
|
||||
})
|
||||
magic = certmagic.New(cache, certmagic.Config{
|
||||
Storage: statuspage.NewCertStorage(client),
|
||||
Storage: statuspage.NewCertStorage(client, certDir),
|
||||
OnDemand: &certmagic.OnDemandConfig{DecisionFunc: pages.AllowCertificate},
|
||||
Logger: zap.NewNop(),
|
||||
})
|
||||
@@ -128,3 +134,34 @@ func redirectToHTTPS(next http.Handler) http.Handler {
|
||||
http.Redirect(w, r, "https://"+host+r.URL.RequestURI(), http.StatusMovedPermanently)
|
||||
})
|
||||
}
|
||||
|
||||
// checkToken rejects tokens of the wrong kind or revoked tokens, but starts
|
||||
// anyway when the API is unreachable: cached pages must be served exactly
|
||||
// when the rest of the system has trouble.
|
||||
func checkToken(ctx context.Context, client *agentapi.Client, log *slog.Logger) error {
|
||||
hctx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
||||
defer cancel()
|
||||
hello, err := client.Hello(hctx)
|
||||
switch {
|
||||
case errors.Is(err, agentapi.ErrUnauthorized):
|
||||
return err
|
||||
case err != nil:
|
||||
log.Warn("api unreachable at start; serving cached pages", "error", err)
|
||||
return nil
|
||||
case hello.Kind != "statuspage":
|
||||
return fmt.Errorf("token belongs to a %s agent, not a status page server", hello.Kind)
|
||||
}
|
||||
log.Info("connected to api", "agent", hello.Name)
|
||||
return nil
|
||||
}
|
||||
|
||||
func resolveCacheDir(dir string) (string, error) {
|
||||
if dir != "" {
|
||||
return dir, nil
|
||||
}
|
||||
base, err := os.UserCacheDir()
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("no cache directory; set UPTIME_STATUSPAGE_CACHE_DIR: %w", err)
|
||||
}
|
||||
return filepath.Join(base, "uptimesebi-statuspage"), nil
|
||||
}
|
||||
Reference in new issue
Block a user