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
- func Apply[C any, O ~func(*C)](defaults C, opts ...O) C
- func Bind[T any](pathSegments ...string) *bindModule[T]
- func Get[T any](ctx context.Context) *T
- func ModulePath(category, moduleType, instance string) string
- func UnmarshalKoanf[C any](c *C, k *koanf.Koanf, path string) error
- type Binding
- type Config
- type Module
- func (m *Module) Init(ctx context.Context) error
- func (m *Module) IsConfigModule()
- func (m *Module) Koanf() *koanf.Koanf
- func (m *Module) OnReload(fn func(k *koanf.Koanf))
- func (m *Module) OnValidate(fn func(k *koanf.Koanf) error)
- func (m *Module) ProvenanceSnapshot() []ProvenanceEntry
- func (m *Module) Provides() []reflect.Type
- func (m *Module) Shutdown(_ context.Context) error
- type Option
- type Passthrough
- type ProvenanceEntry
- type ReloadNotifier
- type TLS
- type Validatable
Constants ¶
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.
const ( OriginFile = "file" OriginEnv = "env" OriginFlag = "flag" OriginDefault = "default" )
Origin values for a resolved config key.
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
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 ModulePath ¶
ModulePath generates the config path for a module instance. Example: ModulePath("grpc", "server", "internal") -> "modules.grpc.server.internal"
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
GetBinding returns the Binding for advanced use (OnChange callbacks).
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 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 (*Module) Init ¶
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) OnReload ¶ added in v0.0.2
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
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.
type Option ¶
type Option func(cfg *Config)
Option manipulates Config.
func WithConfigDirs ¶
WithConfigDirs sets directories to search for config files.
func WithConfigName ¶
WithConfigName sets the base config file name without extension (default: "lakta").
func WithDebounceDelay ¶ added in v0.0.5
WithDebounceDelay sets how long to wait after a file change before reloading (default: 100ms).
func WithEnvPrefix ¶
WithEnvPrefix sets the environment variable prefix (default: "LAKTA_").
func WithProfile ¶ added in v0.3.0
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
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
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.
type Validatable ¶ added in v0.0.4
type Validatable interface {
Validate() error
}
Validatable is implemented by config structs that need validation after unmarshalling.