config

package
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Feb 17, 2026 License: Apache-2.0 Imports: 21 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"
)

Category constants for module organization.

View Source
const DefaultInstanceName = "default"

DefaultInstanceName is the default instance name for modules.

Variables

This section is empty.

Functions

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"

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
}

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) 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 WithEnvPrefix

func WithEnvPrefix(prefix string) Option

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

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 ReloadNotifier added in v0.0.2

type ReloadNotifier interface {
	OnReload(fn func(k *koanf.Koanf))
}

ReloadNotifier can register callbacks for config reload events.

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