settings

package
v1.16.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 12, 2026 License: MIT Imports: 9 Imported by: 0

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

func Load(v any) error

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.

func LoadFrom

func LoadFrom(v any, lookup func(string) (string, bool)) error

LoadFrom is Load with another source of values, such as a map in tests.

Types

type Error

type Error struct {
	Problems []string
}

Error lists every setting that's missing or invalid.

func (*Error) Error

func (e *Error) Error() string

type Secret

type Secret string

Secret is a setting that must not be shown: a password, an API key, a signing secret.

func (Secret) GoString

func (s Secret) GoString() string

GoString implements fmt.GoStringer, without the value.

func (Secret) LogValue

func (s Secret) LogValue() slog.Value

LogValue implements slog.LogValuer, without the value.

func (Secret) MarshalJSON

func (s Secret) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler, without the value.

func (Secret) MarshalText

func (s Secret) MarshalText() ([]byte, error)

MarshalText implements encoding.TextMarshaler, without the value.

func (Secret) Reveal

func (s Secret) Reveal() string

Reveal returns the secret's value.

func (Secret) String

func (s Secret) String() string

String implements fmt.Stringer, without the value.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL