Files
uptimesebi/internal/config/config.go
T
Sebastian UnterschützandClaude Opus 5.5 e9c7394c8b feat(platform)!: log exclusively as structured json
Request access logs, panics, migration output and fatal errors are now
emitted through the JSON slog handler.

BREAKING CHANGE: UPTIME_LOG_FORMAT is removed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
2026-11-21 23:41:57 +01:00

79 lines
2.1 KiB
Go

package config
import (
"fmt"
"log/slog"
"time"
"github.com/caarlos0/env/v11"
)
type Edition string
const (
EditionCloud Edition = "cloud"
EditionSelfHosted Edition = "selfhosted"
)
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
ShutdownTimeout time.Duration `env:"UPTIME_SHUTDOWN_TIMEOUT" envDefault:"20s"`
}
// 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 {
Master string `env:"UPTIME_REDIS_SENTINEL_MASTER"`
Addrs []string `env:"UPTIME_REDIS_SENTINEL_ADDRS" envSeparator:","`
Username string `env:"UPTIME_REDIS_SENTINEL_USERNAME"`
Password string `env:"UPTIME_REDIS_SENTINEL_PASSWORD"`
}
func (s Sentinel) Enabled() bool { return s.Master != "" }
type API struct {
Common
Listen string `env:"UPTIME_API_LISTEN" envDefault:":8080"`
}
type Checker struct {
Common
Region string `env:"UPTIME_CHECKER_REGION,required"`
HealthListen string `env:"UPTIME_CHECKER_HEALTH_LISTEN" envDefault:":8081"`
}
type StatusPage struct {
Common
Listen string `env:"UPTIME_STATUSPAGE_LISTEN" envDefault:":8082"`
}
func Load[T any]() (T, error) {
var cfg T
if err := env.Parse(&cfg); err != nil {
return cfg, fmt.Errorf("config: %w", err)
}
return cfg, nil
}
func (c Common) Validate() error {
switch c.Edition {
case EditionCloud, EditionSelfHosted:
default:
return fmt.Errorf("config: unknown edition %q", c.Edition)
}
if c.DatabaseURL == "" {
return fmt.Errorf("config: UPTIME_DATABASE_URL is required")
}
switch {
case c.RedisSentinel.Enabled() && len(c.RedisSentinel.Addrs) == 0:
return fmt.Errorf("config: UPTIME_REDIS_SENTINEL_ADDRS is required with UPTIME_REDIS_SENTINEL_MASTER")
case !c.RedisSentinel.Enabled() && c.RedisURL == "":
return fmt.Errorf("config: UPTIME_REDIS_URL or UPTIME_REDIS_SENTINEL_MASTER is required")
}
return nil
}