Documentation
¶
Overview ¶
Package config provides configuration loading built on top of Viper.
It supports yaml, json, toml, and any other format Viper understands. Environment variables are expanded automatically inside configuration files, and an optional validator can reject invalid configurations at load time.
Basic loading ¶
loader := config.NewViper[AppConfig](&config.LoaderOptions[AppConfig]{
ConfigName: "config",
ConfigType: "yaml",
ConfigPaths: []string{".", "./config"},
})
cfg, err := loader.Load()
Loading with validation ¶
cfg, err := loader.LoadWithValidation(func(c *AppConfig) error {
if c.Port == 0 {
return errors.New("port is required")
}
return nil
})
Watching for changes ¶
Set WatchConfig: true to start a file watcher. Use Subscribe to receive typed change events, or OnConfigChange for a simpler callback.
ch := loader.Subscribe()
defer loader.Unsubscribe(ch)
for event := range ch {
if event.Error != nil {
log.Printf("reload failed: %v", event.Error)
continue
}
log.Printf("config reloaded: port %d", event.NewConfig.Port)
}
Index ¶
- Variables
- type ConfigChangeEvent
- type ConfigWatcher
- type Loader
- func (cl *Loader[T]) GetCurrent() *T
- func (cl *Loader[T]) GetViper() *viper.Viper
- func (cl *Loader[T]) Load() (*T, error)
- func (cl *Loader[T]) LoadWithValidation(validator func(*T) error) (*T, error)
- func (cl *Loader[T]) Reload() error
- func (cl *Loader[T]) Stop()
- func (cl *Loader[T]) Subscribe() <-chan ConfigChangeEvent[T]
- func (cl *Loader[T]) Unsubscribe(ch <-chan ConfigChangeEvent[T])
- type LoaderOptions
Constants ¶
This section is empty.
Variables ¶
var ( ErrConfigFileReadFailed = errors.New("error reading configuration file") ErrConfigUnmarshalFailed = errors.New("error parsing configuration") ErrConfigValidationFailed = errors.New("invalid configuration") ErrConfigReloadFailed = errors.New("failed reloading the configuration") ErrValidationReloadFailed = errors.Join(ErrConfigReloadFailed, ErrConfigValidationFailed) )
Functions ¶
This section is empty.
Types ¶
type ConfigChangeEvent ¶
type ConfigChangeEvent[T any] struct { OldConfig *T // Previous configuration NewConfig *T // New configuration Error error // Error if reload failed Timestamp time.Time // Time of the event }
ConfigChangeEvent represents a configuration change event
type ConfigWatcher ¶
type ConfigWatcher[T any] interface { Subscribe() <-chan ConfigChangeEvent[T] Unsubscribe(<-chan ConfigChangeEvent[T]) }
ConfigWatcher defines subscription behavior for config changes
type Loader ¶
type Loader[T any] struct { // contains filtered or unexported fields }
Loader handles configuration loading and watching
func NewViper ¶
func NewViper[T any](options *LoaderOptions[T]) *Loader[T]
NewViper creates a new configuration loader
func (*Loader[T]) GetCurrent ¶
func (cl *Loader[T]) GetCurrent() *T
GetCurrent returns the current configuration safely
func (*Loader[T]) LoadWithValidation ¶
LoadWithValidation loads configuration and applies validation
func (*Loader[T]) Subscribe ¶
func (cl *Loader[T]) Subscribe() <-chan ConfigChangeEvent[T]
Subscribe registers a new subscriber for config changes
func (*Loader[T]) Unsubscribe ¶
func (cl *Loader[T]) Unsubscribe(ch <-chan ConfigChangeEvent[T])
Unsubscribe removes a subscriber
type LoaderOptions ¶
type LoaderOptions[T any] struct { ConfigName string // Configuration file name without extension ConfigType string // Configuration file type (yaml, json, toml, etc.) ConfigPaths []string // Paths where the configuration file is searched WatchConfig bool // Enables file watching for reload ReloadDebounce time.Duration // Debounce duration for reload events OnConfigChange func(*T) // Callback executed on successful reload OnConfigChangeError func(error) // Callback executed on reload error }
LoaderOptions defines configuration loader options
func DefaultLoaderOptions ¶
func DefaultLoaderOptions[T any]() *LoaderOptions[T]
DefaultLoaderOptions returns default configuration loader options