Documentation
¶
Overview ¶
Package settings reads an app's settings from the environment into a struct, declared once and checked when the app starts:
type Settings struct {
DatabaseURL string `env:"DATABASE_URL,required"`
SessionSecret settings.Secret `env:"SESSION_SECRET,required,min=32"`
Workers int `env:"WORKERS" default:"4"`
Timeout time.Duration `env:"TIMEOUT" default:"10s"`
Admins []string `env:"ADMINS"`
}
var s Settings
if err := settings.Load(&s); err != nil {
log.Fatal(err) // every missing or invalid setting, at once
}
A Secret never shows its value: printed, logged with slog or encoded as JSON it's "[secret]", so a log line or an error page can't leak it. Reveal returns it, where it's used.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Load ¶
Load fills the struct v points to from the environment, by the fields' env tags: `env:"NAME"`, with ",required" for a setting the app can't run without and ",min=N" for a shortest length, and an optional `default:"…"`. Fields may be strings, Secrets, bools, integers, floats, time.Durations, slices of those (comma-separated), or types that implement encoding.TextUnmarshaler. Fields without an env tag are left alone; nested structs are read too.
Types ¶
type Error ¶
type Error struct {
Problems []string
}
Error lists every setting that's missing or invalid.
type Secret ¶
type Secret string
Secret is a setting that must not be shown: a password, an API key, a signing secret.
func (Secret) MarshalJSON ¶
MarshalJSON implements json.Marshaler, without the value.
func (Secret) MarshalText ¶
MarshalText implements encoding.TextMarshaler, without the value.