config

package
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Jul 10, 2026 License: Apache-2.0 Imports: 25 Imported by: 0

Documentation

Overview

Package config provides a configuration system for Lakta using koanf. It supports loading configuration from YAML, JSON, and TOML files, environment variables, and CLI flags with hot-reload support.

Index

Constants

View Source
const (
	CategoryOTel       = "otel"
	CategoryLogging    = "logging"
	CategoryHealth     = "health"
	CategoryHTTP       = "http"
	CategoryGRPC       = "grpc"
	CategoryDB         = "db"
	CategoryWorkflows  = "workflows"
	CategoryEvents     = "events"
	CategoryFeatures   = "features"
	CategoryWorkers    = "workers"
	CategoryResilience = "resilience"
	CategoryDebug      = "debug"
	CategoryCache      = "cache"
	CategoryAuth       = "auth"
	CategoryDev        = "dev"
)

Category constants for module organization.

View Source
const (
	OriginFile    = "file"
	OriginEnv     = "env"
	OriginFlag    = "flag"
	OriginDefault = "default"
)

Origin values for a resolved config key.

View Source
const DefaultInstanceName = "default"

DefaultInstanceName is the default instance name for modules.

Variables

This section is empty.

Functions

func Apply added in v0.0.5

func Apply[C any, O ~func(*C)](defaults C, opts ...O) C

Apply applies options to a copy of defaults and returns the result.

func Bind added in v0.0.4

func Bind[T any](pathSegments ...string) *bindModule[T]

Bind creates a module that binds a config struct to a koanf path and registers it in DI as *Binding[T]. Path segments are joined with "." (e.g. "app", "limits" → "app.limits").

func Get added in v0.0.4

func Get[T any](ctx context.Context) *T

Get returns the cached config value from DI. Zero-alloc hot path.

func ModulePath

func ModulePath(category, moduleType, instance string) string

ModulePath generates the config path for a module instance. Example: ModulePath("grpc", "server", "internal") -> "modules.grpc.server.internal"

func UnmarshalKoanf added in v0.0.5

func UnmarshalKoanf[C any](c *C, k *koanf.Koanf, path string) error

UnmarshalKoanf loads configuration from koanf at the given path into c.

Types

type Binding added in v0.0.4

type Binding[T any] struct {
	// contains filtered or unexported fields
}

Binding is a thread-safe, cached config accessor with hot-reload support.

func GetBinding added in v0.0.4

func GetBinding[T any](ctx context.Context) *Binding[T]

GetBinding returns the Binding for advanced use (OnChange callbacks).

func (*Binding[T]) Get added in v0.0.4

func (b *Binding[T]) Get() *T

Get returns the cached config value (zero-alloc atomic pointer load).

func (*Binding[T]) OnChange added in v0.0.4

func (b *Binding[T]) OnChange(fn func(*T))

OnChange registers a callback invoked with the new config value after each reload.

type Config

type Config struct {
	// EnvPrefix specifies the prefix for environment variables used to override configuration values.
	EnvPrefix string

	// ConfigDirs specifies the directories to search for configuration files in the given order.
	ConfigDirs []string

	// ConfigName specifies the base name of the configuration file without its file extension.
	ConfigName string

	// Args contains the command-line arguments to be parsed for configuration overrides.
	Args []string

	// DebounceDelay is how long to wait after a file change event before reloading config.
	// Defaults to 100ms. Set to a lower value in tests.
	DebounceDelay time.Duration

	// Profile selects an environment overlay file (lakta.<Profile>.<ext>) loaded
	// after the base config so its keys win. Empty disables the overlay.
	// Defaults from the LAKTA_PROFILE env var.
	Profile string
}

Config holds the configuration for the config module.

func NewConfig

func NewConfig(options ...Option) Config

NewConfig returns configuration with provided options based on defaults.

func NewDefaultConfig

func NewDefaultConfig() Config

NewDefaultConfig returns default configuration.

type Module

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

Module is the configuration module that loads and provides configuration.

func NewModule

func NewModule(options ...Option) *Module

NewModule creates a new config module.

func (*Module) Init

func (m *Module) Init(ctx context.Context) error

Init initializes the config module, loading configuration from files, env vars, and CLI flags.

func (*Module) IsConfigModule

func (m *Module) IsConfigModule()

IsConfigModule is a marker method to identify this module as the config module.

func (*Module) Koanf

func (m *Module) Koanf() *koanf.Koanf

Koanf returns the koanf instance (thread-safe for hot-reload).

func (*Module) OnReload added in v0.0.2

func (m *Module) OnReload(fn func(k *koanf.Koanf))

OnReload registers a callback that is invoked after config is successfully reloaded. Callbacks run under the module's write lock, so they must not call back into the config module.

func (*Module) OnValidate added in v0.2.0

func (m *Module) OnValidate(fn func(k *koanf.Koanf) error)

OnValidate registers a validator invoked on the candidate koanf before a reload is committed. A non-nil error aborts the reload. Validators run under the module's write lock, so they must not call back into the config module.

func (*Module) ProvenanceSnapshot added in v0.3.0

func (m *Module) ProvenanceSnapshot() []ProvenanceEntry

ProvenanceSnapshot reconstructs per-key origin by replaying the module's layers (files -> env -> flag) into throwaway koanf instances and attributing each key to the highest layer containing it; keys present in none are "default". koanf has no native per-key origin tracking. Read under RLock.

func (*Module) Provides added in v0.0.5

func (m *Module) Provides() []reflect.Type

Provides returns the types this module registers in DI.

func (*Module) Shutdown

func (m *Module) Shutdown(_ context.Context) error

Shutdown gracefully shuts down the config module.

type Option

type Option func(cfg *Config)

Option manipulates Config.

func WithArgs

func WithArgs(args []string) Option

WithArgs sets CLI arguments to parse for config overrides.

func WithConfigDirs

func WithConfigDirs(dirs ...string) Option

WithConfigDirs sets directories to search for config files.

func WithConfigName

func WithConfigName(name string) Option

WithConfigName sets the base config file name without extension (default: "lakta").

func WithDebounceDelay added in v0.0.5

func WithDebounceDelay(d time.Duration) Option

WithDebounceDelay sets how long to wait after a file change before reloading (default: 100ms).

func WithEnvPrefix

func WithEnvPrefix(prefix string) Option

WithEnvPrefix sets the environment variable prefix (default: "LAKTA_").

func WithProfile added in v0.3.0

func WithProfile(name string) Option

WithProfile sets the environment profile overlay (e.g. "prod" loads lakta.prod.<ext> after the base file). Overrides the LAKTA_PROFILE default.

type Passthrough added in v0.0.2

type Passthrough[T any] map[string]any

Passthrough captures arbitrary config keys (via koanf's ",remain") and carries the target struct type T for documentation generators to discover via reflect.

type ProvenanceEntry added in v0.3.0

type ProvenanceEntry struct {
	Key    string `json:"key"`
	Origin string `json:"origin"` // file|env|flag|default
	Value  any    `json:"value"`  // pre-redaction; caller redacts before display
}

ProvenanceEntry attributes a config key to the highest layer that set it.

type ReloadNotifier added in v0.0.2

type ReloadNotifier = lakta.ReloadNotifier

ReloadNotifier is an alias for lakta.ReloadNotifier.

type TLS added in v0.3.0

type TLS struct {
	// CertFile is the path to the PEM-encoded certificate (server identity, or
	// client identity for mutual TLS).
	CertFile string `koanf:"cert_file"`

	// KeyFile is the path to the PEM-encoded private key for CertFile.
	KeyFile string `koanf:"key_file"`

	// ClientCAFile is the path to a PEM bundle of CAs used by a server to verify
	// client certificates. Setting it enables mutual TLS.
	ClientCAFile string `koanf:"client_ca_file"`

	// CAFile is the path to a PEM bundle of CAs used by a client to verify the
	// server certificate. Leave empty to use the system trust store.
	CAFile string `koanf:"ca_file"`

	// ClientAuth overrides the server's client-certificate policy: "none",
	// "request", "require", "verify", or "require_and_verify". Defaults to
	// "require_and_verify" when ClientCAFile is set, otherwise "none".
	ClientAuth string `koanf:"client_auth"`
}

TLS holds file-path based TLS settings shared across transport modules. All fields are optional; lakta never imports a certificate provider, so any source that writes PEM files to disk (cert-manager, Vault Agent, SPIFFE helper, static secrets) works through these paths. For dynamic sources that cannot be expressed as files (e.g. an in-process SPIFFE X509Source), pass a *tls.Config or credentials.TransportCredentials via a module's code-only option instead.

func (TLS) ClientConfig added in v0.3.0

func (t TLS) ClientConfig() (*tls.Config, error)

ClientConfig builds a *tls.Config for a TLS client, presenting a client certificate when configured and verifying the server against CAFile. Returns nil when no client TLS material is configured.

func (TLS) Enabled added in v0.3.0

func (t TLS) Enabled() bool

Enabled reports whether server-side TLS material (cert + key) is configured.

func (TLS) ServerConfig added in v0.3.0

func (t TLS) ServerConfig() (*tls.Config, error)

ServerConfig builds a *tls.Config for a TLS server from the configured file paths, or nil when TLS is disabled (no cert/key).

type Validatable added in v0.0.4

type Validatable interface {
	Validate() error
}

Validatable is implemented by config structs that need validation after unmarshalling.

Jump to

Keyboard shortcuts

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