With UPTIME_STATUSPAGE_TLS the server serves HTTPS with ACME certificates for its hosts and for verified custom domains of pages it may serve, shared by all instances through the API. Plain HTTP answers ACME challenges and health checks and redirects everything else. Closes #22 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
191 lines
6.7 KiB
Go
191 lines
6.7 KiB
Go
// Package config reads component settings from UPTIME_* environment
|
|
// variables.
|
|
package config
|
|
|
|
import (
|
|
"fmt"
|
|
"log/slog"
|
|
"net/url"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/caarlos0/env/v11"
|
|
)
|
|
|
|
// Edition switches features that only make sense when we operate the
|
|
// service, such as billing.
|
|
type Edition string
|
|
|
|
// Editions.
|
|
const (
|
|
EditionCloud Edition = "cloud"
|
|
EditionSelfHosted Edition = "selfhosted"
|
|
)
|
|
|
|
// 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"`
|
|
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 {
|
|
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"`
|
|
}
|
|
|
|
// Enabled reports whether a sentinel master name is configured.
|
|
func (s Sentinel) Enabled() bool { return s.Master != "" }
|
|
|
|
// SignupMode controls who may create an account and organization.
|
|
type SignupMode string
|
|
|
|
// Signup modes. SignupFirstUser allows exactly one account, the operator of a
|
|
// fresh self-hosted installation.
|
|
const (
|
|
SignupOpen SignupMode = "open"
|
|
SignupFirstUser SignupMode = "first-user"
|
|
SignupClosed SignupMode = "closed"
|
|
)
|
|
|
|
// 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"`
|
|
SessionIdle time.Duration `env:"UPTIME_SESSION_IDLE" envDefault:"168h"`
|
|
SessionAbsolute time.Duration `env:"UPTIME_SESSION_ABSOLUTE" envDefault:"720h"`
|
|
// PublicURL is the externally reachable base URL, used for SSO callbacks.
|
|
PublicURL string `env:"UPTIME_PUBLIC_URL" envDefault:"http://localhost:8080"`
|
|
// SSOPrivateNetworks is "auto", "true" or "false". Auto allows
|
|
// organizations' IdPs on private networks only in the self-hosted edition.
|
|
SSOPrivateNetworks string `env:"UPTIME_SSO_ALLOW_PRIVATE_NETWORKS" envDefault:"auto"`
|
|
// SMTPURL configures outgoing mail for email notification channels,
|
|
// e.g. smtp://user:pass@host:587?from=alerts@example.com. Empty disables
|
|
// email channels.
|
|
SMTPURL string `env:"UPTIME_SMTP_URL"`
|
|
// StatusPageURL is where the platform's status page servers are
|
|
// reachable; the console links pages as <StatusPageURL>/<slug>.
|
|
StatusPageURL string `env:"UPTIME_STATUSPAGE_URL" envDefault:"http://localhost:8082"`
|
|
}
|
|
|
|
// SSOAllowPrivate resolves SSOPrivateNetworks.
|
|
func (a API) SSOAllowPrivate() bool {
|
|
switch a.SSOPrivateNetworks {
|
|
case "true":
|
|
return true
|
|
case "false":
|
|
return false
|
|
}
|
|
return a.Edition == EditionSelfHosted
|
|
}
|
|
|
|
// Validate checks API-specific settings in addition to Common.Validate.
|
|
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)")
|
|
}
|
|
switch a.Signup {
|
|
case SignupOpen, SignupFirstUser, SignupClosed:
|
|
default:
|
|
return fmt.Errorf("config: unknown UPTIME_SIGNUP %q", a.Signup)
|
|
}
|
|
switch a.SSOPrivateNetworks {
|
|
case "auto", "true", "false":
|
|
default:
|
|
return fmt.Errorf("config: UPTIME_SSO_ALLOW_PRIVATE_NETWORKS must be auto, true or false")
|
|
}
|
|
if u, err := url.Parse(a.PublicURL); err != nil || u.Host == "" || (u.Scheme != "https" && u.Scheme != "http") || strings.HasSuffix(a.PublicURL, "/") {
|
|
return fmt.Errorf("config: UPTIME_PUBLIC_URL must be an absolute http(s) URL without trailing slash")
|
|
}
|
|
if a.SessionIdle <= 0 || a.SessionAbsolute < a.SessionIdle {
|
|
return fmt.Errorf("config: UPTIME_SESSION_ABSOLUTE must be >= UPTIME_SESSION_IDLE > 0")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Checker configures cmd/checker. Its region is bound to the agent token.
|
|
type Checker struct {
|
|
Common
|
|
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"`
|
|
// Hosts are the host names that serve pages by slug, e.g.
|
|
// status.example.com. Any other host is treated as a custom domain.
|
|
Hosts []string `env:"UPTIME_STATUSPAGE_HOSTS" envSeparator:"," envDefault:"localhost"`
|
|
// TLS obtains certificates via ACME for the hosts and verified custom
|
|
// domains and serves HTTPS on TLSListen; Listen then answers ACME HTTP
|
|
// challenges and redirects to HTTPS.
|
|
TLS bool `env:"UPTIME_STATUSPAGE_TLS"`
|
|
TLSListen string `env:"UPTIME_STATUSPAGE_TLS_LISTEN" envDefault:":443"`
|
|
ACMEEmail string `env:"UPTIME_ACME_EMAIL"`
|
|
// ACMECA defaults to Let's Encrypt; use its staging URL for testing.
|
|
ACMECA string `env:"UPTIME_ACME_CA" envDefault:"https://acme-v02.api.letsencrypt.org/directory"`
|
|
}
|
|
|
|
// Load parses the environment into a T, applying envDefault tags.
|
|
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
|
|
}
|
|
|
|
// Validate reports an unknown edition.
|
|
func (c Common) Validate() error {
|
|
switch c.Edition {
|
|
case EditionCloud, EditionSelfHosted:
|
|
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")
|
|
}
|
|
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
|
|
}
|