feat!: let checkers and status page servers reach data only through the api
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
This commit is contained in:
1 parent
56862dce84
commit
d9891bb050
23 files changed
+1361
-45
No files matched your search
+32
-11
@@ -24,14 +24,25 @@ const (
|
||||
|
||||
// Common holds settings shared by all components.
|
||||
type Common struct {
|
||||
Edition Edition `env:"UPTIME_EDITION" envDefault:"selfhosted"`
|
||||
LogLevel slog.Level `env:"UPTIME_LOG_LEVEL" envDefault:"info"`
|
||||
DatabaseURL string `env:"UPTIME_DATABASE_URL"`
|
||||
RedisURL string `env:"UPTIME_REDIS_URL"`
|
||||
RedisSentinel Sentinel
|
||||
Edition Edition `env:"UPTIME_EDITION" envDefault:"selfhosted"`
|
||||
LogLevel slog.Level `env:"UPTIME_LOG_LEVEL" envDefault:"info"`
|
||||
ShutdownTimeout time.Duration `env:"UPTIME_SHUTDOWN_TIMEOUT" envDefault:"20s"`
|
||||
}
|
||||
|
||||
// Storage locates Postgres and Redis. Only the API connects to them; every
|
||||
// other component goes through the API.
|
||||
type Storage struct {
|
||||
DatabaseURL string `env:"UPTIME_DATABASE_URL"`
|
||||
RedisURL string `env:"UPTIME_REDIS_URL"`
|
||||
RedisSentinel Sentinel
|
||||
}
|
||||
|
||||
// Agent configures how checkers and status page servers reach the API.
|
||||
type Agent struct {
|
||||
APIURL string `env:"UPTIME_API_URL,required,notEmpty"`
|
||||
Token string `env:"UPTIME_AGENT_TOKEN,required,notEmpty"`
|
||||
}
|
||||
|
||||
// Sentinel switches the redis client to failover mode. UPTIME_REDIS_URL then
|
||||
// only contributes credentials, db and TLS for the master; its host is ignored.
|
||||
type Sentinel struct {
|
||||
@@ -58,6 +69,7 @@ const (
|
||||
// API configures cmd/api.
|
||||
type API struct {
|
||||
Common
|
||||
Storage
|
||||
Listen string `env:"UPTIME_API_LISTEN" envDefault:":8080"`
|
||||
SecretKeys string `env:"UPTIME_SECRET_KEYS"`
|
||||
Signup SignupMode `env:"UPTIME_SIGNUP" envDefault:"first-user"`
|
||||
@@ -86,6 +98,9 @@ func (a API) Validate() error {
|
||||
if err := a.Common.Validate(); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := a.Storage.Validate(); err != nil {
|
||||
return err
|
||||
}
|
||||
if a.SecretKeys == "" {
|
||||
return fmt.Errorf("config: UPTIME_SECRET_KEYS is required (format id:base64-32-bytes)")
|
||||
}
|
||||
@@ -108,17 +123,19 @@ func (a API) Validate() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Checker configures cmd/checker. Region names the location this instance
|
||||
// checks from and must match the regions assigned to monitors.
|
||||
// Checker configures cmd/checker. Its region is bound to the agent token.
|
||||
type Checker struct {
|
||||
Common
|
||||
Region string `env:"UPTIME_CHECKER_REGION,required,notEmpty"`
|
||||
Agent
|
||||
HealthListen string `env:"UPTIME_CHECKER_HEALTH_LISTEN" envDefault:":8081"`
|
||||
// Concurrency caps checks running at the same time in one instance.
|
||||
Concurrency int `env:"UPTIME_CHECKER_CONCURRENCY" envDefault:"64"`
|
||||
}
|
||||
|
||||
// StatusPage configures cmd/statuspage.
|
||||
type StatusPage struct {
|
||||
Common
|
||||
Agent
|
||||
Listen string `env:"UPTIME_STATUSPAGE_LISTEN" envDefault:":8082"`
|
||||
}
|
||||
|
||||
@@ -131,13 +148,17 @@ func Load[T any]() (T, error) {
|
||||
return cfg, nil
|
||||
}
|
||||
|
||||
// Validate reports missing or contradictory settings.
|
||||
// Validate reports an unknown edition.
|
||||
func (c Common) Validate() error {
|
||||
switch c.Edition {
|
||||
case EditionCloud, EditionSelfHosted:
|
||||
default:
|
||||
return fmt.Errorf("config: unknown edition %q", c.Edition)
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("config: unknown edition %q", c.Edition)
|
||||
}
|
||||
|
||||
// Validate reports missing or contradictory database and Redis settings.
|
||||
func (c Storage) Validate() error {
|
||||
if c.DatabaseURL == "" {
|
||||
return fmt.Errorf("config: UPTIME_DATABASE_URL is required")
|
||||
}
|
||||
|
||||
@@ -2,20 +2,20 @@ package config
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestCommonValidateRedisModes(t *testing.T) {
|
||||
func TestStorageValidateRedisModes(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
modify func(*Common)
|
||||
modify func(*Storage)
|
||||
wantErr bool
|
||||
}{
|
||||
{"standalone", func(c *Common) { c.RedisURL = "redis://r" }, false},
|
||||
{"no redis", func(*Common) {}, true},
|
||||
{"sentinel", func(c *Common) { c.RedisSentinel = Sentinel{Master: "m", Addrs: []string{"s:26379"}} }, false},
|
||||
{"sentinel without addrs", func(c *Common) { c.RedisSentinel = Sentinel{Master: "m"} }, true},
|
||||
{"standalone", func(c *Storage) { c.RedisURL = "redis://r" }, false},
|
||||
{"no redis", func(*Storage) {}, true},
|
||||
{"sentinel", func(c *Storage) { c.RedisSentinel = Sentinel{Master: "m", Addrs: []string{"s:26379"}} }, false},
|
||||
{"sentinel without addrs", func(c *Storage) { c.RedisSentinel = Sentinel{Master: "m"} }, true},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
c := Common{Edition: EditionSelfHosted, DatabaseURL: "postgres://x"}
|
||||
c := Storage{DatabaseURL: "postgres://x"}
|
||||
tt.modify(&c)
|
||||
if err := c.Validate(); (err != nil) != tt.wantErr {
|
||||
t.Errorf("Validate() = %v, want error: %t", err, tt.wantErr)
|
||||
|
||||
Reference in new issue
Block a user