ci / lint (push) Skipped
ci / test (push) Skipped
ci / build (push) Skipped
ci / image (push) Skipped
ci / lint (pull_request) Successful in 7m28s
ci / test (pull_request) Successful in 12m52s
ci / build (pull_request) Successful in 11m21s
ci / image (pull_request) Successful in 4m38s
A custom domain now needs a single record: a CNAME to <label>.<status page host>, where the label is derived from the page's domain token. The wildcard record of the status page host routes it, and since the target belongs to one page alone it also proves who set it up; a CNAME to the shared host would not, as any organization could then claim a domain pointing at the platform. The TXT record stays for domains whose CNAME cannot be seen, behind a proxy or without a subdomain. - The domain watch looks for the records of a new domain every minute for a quarter of an hour, then every ten minutes for two days, and verifies it on its own; "Check now" still works. - Verified domains are checked daily. When the record is gone the owners are told, and a week later the domain is switched off, so a record left behind cannot be taken over. Domains verified before keep working without checks. - The editor shows the CNAME record first, the TXT way for proxies and apex domains, how the domain was verified and when a missing record will switch it off. The API returns cname_record and domain_missing_since. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
155 lines
4.3 KiB
Go
155 lines
4.3 KiB
Go
// Command api serves the management API and console. It also owns schema
|
|
// migrations: run "api migrate up" before rolling out a new release.
|
|
package main
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"log/slog"
|
|
"os"
|
|
"time"
|
|
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
|
|
"git.zb-server.de/ZB-Server/uptimesebi/internal/api"
|
|
"git.zb-server.de/ZB-Server/uptimesebi/internal/config"
|
|
"git.zb-server.de/ZB-Server/uptimesebi/internal/datapolicy"
|
|
"git.zb-server.de/ZB-Server/uptimesebi/internal/i18n"
|
|
"git.zb-server.de/ZB-Server/uptimesebi/internal/migrations"
|
|
"git.zb-server.de/ZB-Server/uptimesebi/internal/notify"
|
|
"git.zb-server.de/ZB-Server/uptimesebi/internal/platform"
|
|
"git.zb-server.de/ZB-Server/uptimesebi/internal/sealed"
|
|
"git.zb-server.de/ZB-Server/uptimesebi/internal/tenant"
|
|
"git.zb-server.de/ZB-Server/uptimesebi/internal/web"
|
|
)
|
|
|
|
func main() {
|
|
log, level := platform.NewLogger("api")
|
|
// Route the standard log package and dependencies through the same handler.
|
|
slog.SetDefault(log)
|
|
if err := run(log, level, os.Args[1:]); err != nil {
|
|
log.Error("exit", "error", err)
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
|
|
func run(log *slog.Logger, level *slog.LevelVar, args []string) error {
|
|
cfg, err := config.Load[config.API]()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
level.Set(cfg.LogLevel)
|
|
ctx, stop := platform.SignalContext()
|
|
defer stop()
|
|
|
|
if len(args) > 0 {
|
|
switch args[0] {
|
|
case "migrate":
|
|
return migrate(ctx, log, cfg, args[1:])
|
|
case "admin", "agent", "region":
|
|
return admin(ctx, cfg, args)
|
|
case "health":
|
|
// The image has no shell; Docker's HEALTHCHECK runs this.
|
|
return platform.ProbeLocal(ctx, cfg.Listen, "/healthz")
|
|
case "serve":
|
|
default:
|
|
return usageError{}
|
|
}
|
|
}
|
|
|
|
if err := cfg.Validate(); err != nil {
|
|
return err
|
|
}
|
|
if err := retentionOf(cfg).Validate(); err != nil {
|
|
return err
|
|
}
|
|
keys, err := sealed.Parse(cfg.SecretKeys)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if _, err := notify.ParseSMTP(cfg.SMTPURL); err != nil {
|
|
return err
|
|
}
|
|
deps, err := platform.Connect(ctx, cfg.Storage, log)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer deps.Close()
|
|
|
|
catalog, err := i18n.Load()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
srv := api.New(cfg, api.Deps{Log: log, DB: tenant.New(deps.DB), Redis: deps.Redis, Keys: keys, Catalog: catalog})
|
|
health := platform.NewHealth(deps.Probes())
|
|
r := platform.NewRouter(log)
|
|
if err := platform.TrustProxies(r, cfg.TrustedProxies); err != nil {
|
|
return err
|
|
}
|
|
health.Register(r)
|
|
r.StaticFS("/static", web.Static())
|
|
srv.Mount(r)
|
|
go srv.RunScheduler(ctx)
|
|
go srv.RunNotifier(ctx)
|
|
go srv.RunHousekeeping(ctx)
|
|
go srv.RunRegionWatch(ctx)
|
|
go srv.RunDomainWatch(ctx)
|
|
go srv.RunHeartbeats(ctx)
|
|
|
|
return platform.Serve(ctx, log, platform.NewHTTPServer(cfg.Listen, r), health, 5*time.Second, cfg.ShutdownTimeout)
|
|
}
|
|
|
|
func migrate(ctx context.Context, log *slog.Logger, cfg config.API, args []string) error {
|
|
dsn := cfg.DatabaseURL
|
|
if dsn == "" {
|
|
return errors.New("UPTIME_DATABASE_URL is required")
|
|
}
|
|
log = log.With("task", "migrate")
|
|
cmd := "up"
|
|
if len(args) > 0 {
|
|
cmd = args[0]
|
|
}
|
|
switch cmd {
|
|
case "up":
|
|
res, err := migrations.Up(ctx, dsn)
|
|
for _, r := range res {
|
|
log.Info("migration applied", "migration", r.Source.Version, "file", r.Source.Path, "duration_ms", r.Duration.Milliseconds())
|
|
}
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if len(res) == 0 {
|
|
log.Info("schema up to date")
|
|
}
|
|
return applyRetention(ctx, log, cfg)
|
|
case "down":
|
|
r, err := migrations.Down(ctx, dsn)
|
|
if r != nil {
|
|
log.Info("migration rolled back", "migration", r.Source.Version, "file", r.Source.Path, "duration_ms", r.Duration.Milliseconds())
|
|
}
|
|
return err
|
|
case "status":
|
|
st, err := migrations.Status(ctx, dsn)
|
|
for _, s := range st {
|
|
log.Info("migration status", "migration", s.Source.Version, "file", s.Source.Path, "state", string(s.State))
|
|
}
|
|
return err
|
|
}
|
|
return errors.New("unknown migrate command " + cmd)
|
|
}
|
|
|
|
func retentionOf(cfg config.API) datapolicy.Retention {
|
|
return datapolicy.Retention{Raw: cfg.Retention.Raw, Hourly: cfg.Retention.Hourly, Daily: cfg.Retention.Daily}
|
|
}
|
|
|
|
// applyRetention runs with the migration's owner rights, which changing
|
|
// TimescaleDB policies needs.
|
|
func applyRetention(ctx context.Context, log *slog.Logger, cfg config.API) error {
|
|
pool, err := pgxpool.New(ctx, cfg.DatabaseURL)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer pool.Close()
|
|
return datapolicy.Apply(ctx, pool, retentionOf(cfg), log)
|
|
}
|