// Command statuspage serves public status pages, including on customer // domains. Like the checker it reads data only through the API. package main import ( "context" "crypto/tls" "errors" "fmt" "log/slog" "net" "net/http" "os" "time" "github.com/caddyserver/certmagic" "github.com/google/uuid" "go.uber.org/zap" "git.zb-server.de/ZB-Server/uptimesebi/internal/agentapi" "git.zb-server.de/ZB-Server/uptimesebi/internal/buildinfo" "git.zb-server.de/ZB-Server/uptimesebi/internal/config" "git.zb-server.de/ZB-Server/uptimesebi/internal/i18n" "git.zb-server.de/ZB-Server/uptimesebi/internal/platform" "git.zb-server.de/ZB-Server/uptimesebi/internal/statuspage" "git.zb-server.de/ZB-Server/uptimesebi/internal/web" ) func main() { log, level := platform.NewLogger("statuspage") // Route the standard log package and dependencies through the same handler. slog.SetDefault(log) if err := run(log, level); err != nil { log.Error("exit", "error", err) os.Exit(1) } } func run(log *slog.Logger, level *slog.LevelVar) error { cfg, err := config.Load[config.StatusPage]() if err != nil { return err } level.Set(cfg.LogLevel) if err := cfg.Common.Validate(); err != nil { return err } ctx, stop := platform.SignalContext() defer stop() client := agentapi.NewClient(cfg.APIURL, cfg.Token, uuid.New(), buildinfo.Version) hello, err := platform.Hello(ctx, client, log) if err != nil { return err } if hello.Kind != "statuspage" { return fmt.Errorf("token belongs to a %s agent, not a status page server", hello.Kind) } catalog, err := i18n.Load() if err != nil { return err } health := platform.NewHealth(map[string]platform.Probe{ "api": func(ctx context.Context) error { _, err := client.Hello(ctx); return err }, }) r := platform.NewRouter(log) health.Register(r) r.StaticFS("/static", web.Static()) pages := statuspage.New(client, catalog, log, cfg.Hosts) pages.Mount(r) 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) if err != nil { return err } https := platform.NewHTTPServer(cfg.TLSListen, r) https.TLSConfig = tlsConfig plain := platform.NewHTTPServer(cfg.Listen, challenges(redirectToHTTPS(r))) errs := make(chan error, 2) go func() { errs <- platform.Serve(ctx, log, plain, health, 5*time.Second, cfg.ShutdownTimeout) }() go func() { errs <- platform.ServeTLS(ctx, log, https, health, 5*time.Second, cfg.ShutdownTimeout) }() err = <-errs stop() return errors.Join(err, <-errs) } // 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) { 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), OnDemand: &certmagic.OnDemandConfig{DecisionFunc: pages.AllowCertificate}, Logger: zap.NewNop(), }) issuer := certmagic.NewACMEIssuer(magic, certmagic.ACMEIssuer{CA: cfg.ACMECA, Email: cfg.ACMEEmail, Agreed: true}) magic.Issuers = []certmagic.Issuer{issuer} go func() { <-ctx.Done() cache.Stop() }() tc := magic.TLSConfig() tc.NextProtos = append([]string{"h2", "http/1.1"}, tc.NextProtos...) log.Info("tls enabled", "ca", cfg.ACMECA, "listen", cfg.TLSListen) return tc, issuer.HTTPChallengeHandler, nil } // redirectToHTTPS sends page requests to HTTPS but keeps health checks on // plain HTTP for load balancers and orchestrators. func redirectToHTTPS(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.URL.Path == "/healthz" || r.URL.Path == "/readyz" { next.ServeHTTP(w, r) return } host, _, err := net.SplitHostPort(r.Host) if err != nil { host = r.Host } http.Redirect(w, r, "https://"+host+r.URL.RequestURI(), http.StatusMovedPermanently) }) }