config

package
v2.1.0 Latest Latest
Warning

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

Go to latest
Published: May 2, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package config provides fail-fast configuration loading helpers.

Missing required values and invalid present values are reported as errors by LoadFromEnv and trigger a panic through MustLoadFromEnv, making startup configuration failures explicit instead of silently falling back to defaults.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsProduction

func IsProduction(env string) bool

IsProduction returns true when env is production-like.

func SplitCSV

func SplitCSV(input string) []string

SplitCSV splits a comma-separated string into a list of trimmed values.

Types

type Config

type Config struct {
	Addr           string `env:"API_ADDR"`         // host:port|:port
	DatabaseURL    string `env:"DATABASE_URL"`     // required
	LogLevel       string `env:"LOG_LEVEL"`        // debug|info|warn|error
	MigrateOnStart bool   `env:"MIGRATE_ON_START"` // true|false
	MigrationsDir  string `env:"MIGRATIONS_DIR"`   // plain - means use embedded
	Env            string `env:"ENV"`              // development|staging|production

	// Optional rate-limit bypass for test/dev environments.
	RateLimitSkipEnabled               bool   `env:"RATE_LIMIT_SKIP_ENABLED"`                 // true|false
	RateLimitSkipHeader                string `env:"RATE_LIMIT_SKIP_HEADER"`                  // header name
	RateLimitAllowDangerousDevBypasses bool   `env:"RATE_LIMIT_ALLOW_DANGEROUS_DEV_BYPASSES"` // true|false
}

Config captures application configuration loaded from environment variables.

func LoadFromEnv

func LoadFromEnv(loader *Loader) (Config, error)

LoadFromEnv loads config from environment using an optional loader and returns an error when required values are missing or present values are invalid.

func MustLoadFromEnv

func MustLoadFromEnv() Config

MustLoadFromEnv loads config or panics if required values are missing or present values are invalid. Reusable library code should prefer LoadFromEnv.

type Loader

type Loader struct {
	// contains filtered or unexported fields
}

Loader reads env vars with defaults and aggregates errors.

func NewLoader

func NewLoader() *Loader

NewLoader creates a loader backed by the default env adapter.

func (*Loader) Bool

func (l *Loader) Bool(key string, def bool) bool

Bool reads a bool env var with a default.

func (*Loader) CSV

func (l *Loader) CSV(key string) []string

CSV reads a comma-separated env var into a slice.

func (*Loader) Duration

func (l *Loader) Duration(key string, def time.Duration) time.Duration

Duration reads a duration env var with a default.

func (*Loader) Err

func (l *Loader) Err() error

Err returns aggregated errors, if any.

func (*Loader) Int

func (l *Loader) Int(key string, def int) int

Int reads an int env var with a default.

func (*Loader) OneOf added in v2.1.0

func (l *Loader) OneOf(key, value string, allowed ...string) string

OneOf validates an enum-like string value and records an error when the normalized value is not in the allowed set.

func (*Loader) Require

func (l *Loader) Require(key string) string

Require returns the env var or records an error if missing.

func (*Loader) String

func (l *Loader) String(key, def string) string

String reads a string env var with a default.

Jump to

Keyboard shortcuts

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