Only cmd/api connects to Postgres and Redis. Agents authenticate with tokens created by 'api agent create' and use /agent/v1: checkers long poll for jobs, which doubles as heartbeat, and report results in batches. Organizations manage monitors through /api/v1/org/monitors. BREAKING CHANGE: cmd/checker and cmd/statuspage no longer read UPTIME_DATABASE_URL or UPTIME_REDIS_*; they need UPTIME_API_URL and UPTIME_AGENT_TOKEN. UPTIME_CHECKER_REGION is replaced by the region bound to the agent token. Closes #10 Closes #11 Closes #12 Closes #13 Closes #49 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
115 lines
3.1 KiB
Go
115 lines
3.1 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"
|
|
|
|
"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/i18n"
|
|
"git.zb-server.de/ZB-Server/uptimesebi/internal/migrations"
|
|
"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.DatabaseURL, args[1:])
|
|
case "agent", "region":
|
|
return admin(ctx, cfg, args)
|
|
case "serve":
|
|
default:
|
|
return errors.New(usage)
|
|
}
|
|
}
|
|
|
|
if err := cfg.Validate(); err != nil {
|
|
return err
|
|
}
|
|
keys, err := sealed.Parse(cfg.SecretKeys)
|
|
if 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)
|
|
health.Register(r)
|
|
r.StaticFS("/static", web.Static())
|
|
srv.Mount(r)
|
|
go srv.RunScheduler(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, dsn string, args []string) error {
|
|
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 && len(res) == 0 {
|
|
log.Info("schema up to date")
|
|
}
|
|
return err
|
|
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)
|
|
}
|