Documentation
¶
Index ¶
- Constants
- Variables
- func FromViper(v *viper.Viper) fx.Option
- func FromViperWithPath(v *viper.Viper, configPath string) fx.Option
- func NewAutoConfig() (hyperion.ConfigWatcher, error)
- func NewConfigFromOptions(opts Options) (hyperion.ConfigWatcher, error)
- func NewConfigFromViper(v *viper.Viper) hyperion.ConfigWatcher
- func NewProvider(configPath string) (hyperion.ConfigWatcher, error)
- func NewViperProvider() (hyperion.ConfigWatcher, error)deprecated
- func WithOptions(opts Options) fx.Option
- type Options
- type OptionsBuilder
- func (b *OptionsBuilder) AddConfigPath(path string) *OptionsBuilder
- func (b *OptionsBuilder) Build() (Options, error)
- func (b *OptionsBuilder) ForConsul(endpoint, path string) *OptionsBuilder
- func (b *OptionsBuilder) ForDevelopment() *OptionsBuilder
- func (b *OptionsBuilder) ForEtcd(endpoint, path string) *OptionsBuilder
- func (b *OptionsBuilder) ForProduction() *OptionsBuilder
- func (b *OptionsBuilder) ForTesting() *OptionsBuilder
- func (b *OptionsBuilder) WithAutomaticEnv(enable bool) *OptionsBuilder
- func (b *OptionsBuilder) WithConfigFile(path string) *OptionsBuilder
- func (b *OptionsBuilder) WithConfigName(name string) *OptionsBuilder
- func (b *OptionsBuilder) WithConfigPaths(paths ...string) *OptionsBuilder
- func (b *OptionsBuilder) WithConfigType(configType string) *OptionsBuilder
- func (b *OptionsBuilder) WithEnvPrefix(prefix string) *OptionsBuilder
- func (b *OptionsBuilder) WithJSON(name string) *OptionsBuilder
- func (b *OptionsBuilder) WithRemoteConfig(config map[string]string) *OptionsBuilder
- func (b *OptionsBuilder) WithRemoteProvider(provider, endpoint, path string) *OptionsBuilder
- func (b *OptionsBuilder) WithTOML(name string) *OptionsBuilder
- func (b *OptionsBuilder) WithYAML(name string) *OptionsBuilder
- type Provider
- func (p *Provider) AllKeys() []string
- func (p *Provider) Get(key string) any
- func (p *Provider) GetBool(key string) bool
- func (p *Provider) GetFloat64(key string) float64
- func (p *Provider) GetInt(key string) int
- func (p *Provider) GetInt64(key string) int64
- func (p *Provider) GetString(key string) string
- func (p *Provider) GetStringSlice(key string) []string
- func (p *Provider) IsSet(key string) bool
- func (p *Provider) Unmarshal(key string, rawVal any) error
- func (p *Provider) Watch(callback func(event hyperion.ChangeEvent)) (stop func(), err error)
Constants ¶
const (
// DefaultConfigPath is the default path to configuration file
DefaultConfigPath = "configs/config.yaml"
)
Configuration-related constants
Variables ¶
var ( // ErrNoViperProvided is returned when FromViper is called with nil *viper.Viper. ErrNoViperProvided = errors.New("viper: no *viper.Viper provided") // ErrNoConfigPathProvided is returned when FromViperWithPath is called with empty config path. ErrNoConfigPathProvided = errors.New("viper: no config path provided") )
Error variables for Viper adapter.
var AutoModule = hyperion.WrapAdapter( fx.Module("hyperion.adapter.viper.auto", fx.Provide( fx.Annotate( NewAutoConfig, fx.As(new(hyperion.Config)), fx.As(new(hyperion.ConfigWatcher)), ), ), ), "config", )
AutoModule provides Level 1 (Zero-Config) API with capability declaration. It auto-configures Viper Config from a predefined config file path.
Usage:
hyperion.NewApp(
viper.AutoModule, // Auto-configured from configs/config.yaml
zap.Module,
gorm.Module,
myapp.Module,
)
Default configuration file: configs/config.yaml
var Module = AutoModule
Module provides Viper-based Config implementation with capability declaration. This is the default module using Level 1 (Zero-Config) API.
Usage:
hyperion.NewApp(
viper.Module, // Provides Config - auto-detected capability
myapp.Module,
).Run()
Default configuration file: configs/config.yaml
For Level 2 (Configuration-Driven) API, use WithOptions() instead. For Level 3 (Custom Provider) API, use FromViper() or FromViperWithPath() instead.
Functions ¶
func FromViper ¶
FromViper provides Level 3 (Custom Provider) API. It accepts a user-provided *viper.Viper instance.
This allows applications to fully control Viper initialization and version, while still using Hyperion's Config and ConfigWatcher abstractions.
IMPORTANT: When using this function, the caller is responsible for managing the Viper instance lifecycle.
Usage:
import "github.com/spf13/viper"
v := viper.New()
v.SetConfigFile("my-config.yaml")
v.ReadInConfig()
// Add custom settings
v.Set("custom.key", "value")
v.SetDefault("app.name", "my-app")
hyperion.NewApp(
viper.FromViper(v),
myapp.Module,
)
func FromViperWithPath ¶
FromViperWithPath provides Level 3 API with explicit config file path. This variant enables hot reload support by tracking the config file.
Usage:
v := viper.New()
v.SetConfigFile("configs/app.yaml")
v.ReadInConfig()
hyperion.NewApp(
viper.FromViperWithPath(v, "configs/app.yaml"),
myapp.Module,
)
func NewAutoConfig ¶
func NewAutoConfig() (hyperion.ConfigWatcher, error)
NewAutoConfig creates a Config from auto-configuration. It uses the default config file path: configs/config.yaml
func NewConfigFromOptions ¶
func NewConfigFromOptions(opts Options) (hyperion.ConfigWatcher, error)
NewConfigFromOptions creates a Config from Options.
func NewConfigFromViper ¶
func NewConfigFromViper(v *viper.Viper) hyperion.ConfigWatcher
NewConfigFromViper creates a Config from an existing *viper.Viper. Note: This variant does NOT support hot reload (Watch) since we don't know the config file path. Use FromViperWithPath if you need hot reload.
func NewProvider ¶
func NewProvider(configPath string) (hyperion.ConfigWatcher, error)
NewProvider creates a new viper-based config provider from the given configuration file path. It automatically detects the file format based on the file extension.
Supported formats: .yaml, .yml, .json, .toml
Environment variables are automatically mapped to configuration keys. For example, APP_DATABASE_HOST maps to the "database.host" config key.
Returns an error if the configuration file cannot be read or parsed.
func NewViperProvider
deprecated
func NewViperProvider() (hyperion.ConfigWatcher, error)
Deprecated: Use AutoModule, WithOptions(), or FromViper() instead. This function is kept for backward compatibility.
func WithOptions ¶
WithOptions provides Level 2 (Configuration-Driven) API. It creates Config from programmatic Options.
Usage:
opts, err := viper.NewOptionsBuilder().
WithConfigFile("configs/app.yaml").
WithEnvPrefix("MYAPP").
ForProduction().
Build()
if err != nil {
panic(err)
}
hyperion.NewApp(
viper.WithOptions(opts),
myapp.Module,
)
Types ¶
type Options ¶
type Options struct {
// Config file configuration
ConfigFile string // Path to config file (e.g., "configs/config.yaml")
ConfigName string // Config file name without extension (e.g., "config")
ConfigType string // Config file type (yaml, json, toml, etc.)
ConfigPaths []string // Directories to search for config file
// Environment variable configuration
EnvPrefix string // Prefix for environment variables (e.g., "APP")
AutomaticEnv *bool // Enable automatic environment variable override (use pointer to allow explicit false)
// Advanced: Remote configuration
RemoteProvider string // Remote config provider (etcd, consul)
RemoteEndpoint string // Remote provider endpoint
RemotePath string // Path in remote provider
RemoteConfig map[string]string // Additional remote config options
}
Options provides programmatic configuration for the Viper adapter. This is used for Level 2 (Configuration-Driven) API with Builder pattern.
Example usage:
opts := viper.NewOptionsBuilder().
WithConfigFile("configs/config.yaml").
WithEnvPrefix("APP").
WithAutomaticEnv(true).
Build()
hyperion.NewApp(
viper.WithOptions(opts),
myapp.Module,
)
func DefaultOptions ¶
func DefaultOptions() Options
DefaultOptions returns sensible default options for local development.
func (Options) Defaults ¶
func (o Options) Defaults() hyperion.AdapterOptions
Defaults returns the default options for Viper adapter.
func (Options) Merge ¶
func (o Options) Merge(defaults hyperion.AdapterOptions) hyperion.AdapterOptions
Merge combines user-provided options with default options. User options take precedence over defaults.
type OptionsBuilder ¶
type OptionsBuilder struct {
// contains filtered or unexported fields
}
OptionsBuilder provides a fluent API for building Viper Options. It implements the builder pattern for better ergonomics.
Example usage:
opts, err := viper.NewOptionsBuilder().
WithConfigFile("configs/app.yaml").
WithEnvPrefix("MYAPP").
WithAutomaticEnv(true).
Build()
func NewOptionsBuilder ¶
func NewOptionsBuilder() *OptionsBuilder
NewOptionsBuilder creates a new OptionsBuilder with default options.
func (*OptionsBuilder) AddConfigPath ¶
func (b *OptionsBuilder) AddConfigPath(path string) *OptionsBuilder
AddConfigPath adds a directory to search for config files. This appends to the existing paths instead of replacing them.
func (*OptionsBuilder) Build ¶
func (b *OptionsBuilder) Build() (Options, error)
Build finalizes the builder and returns the Options instance. It validates the options before returning.
func (*OptionsBuilder) ForConsul ¶
func (b *OptionsBuilder) ForConsul(endpoint, path string) *OptionsBuilder
ForConsul configures Viper to use Consul as the remote config source.
func (*OptionsBuilder) ForDevelopment ¶
func (b *OptionsBuilder) ForDevelopment() *OptionsBuilder
ForDevelopment applies recommended settings for development environment.
func (*OptionsBuilder) ForEtcd ¶
func (b *OptionsBuilder) ForEtcd(endpoint, path string) *OptionsBuilder
ForEtcd configures Viper to use etcd as the remote config source.
func (*OptionsBuilder) ForProduction ¶
func (b *OptionsBuilder) ForProduction() *OptionsBuilder
ForProduction applies recommended settings for production environment.
func (*OptionsBuilder) ForTesting ¶
func (b *OptionsBuilder) ForTesting() *OptionsBuilder
ForTesting applies recommended settings for testing environment.
func (*OptionsBuilder) WithAutomaticEnv ¶
func (b *OptionsBuilder) WithAutomaticEnv(enable bool) *OptionsBuilder
WithAutomaticEnv enables or disables automatic environment variable override. When enabled, environment variables will automatically override config values.
func (*OptionsBuilder) WithConfigFile ¶
func (b *OptionsBuilder) WithConfigFile(path string) *OptionsBuilder
WithConfigFile sets the complete path to the config file. This is the most straightforward way to specify configuration. Example: "configs/config.yaml", "/etc/myapp/config.json"
func (*OptionsBuilder) WithConfigName ¶
func (b *OptionsBuilder) WithConfigName(name string) *OptionsBuilder
WithConfigName sets the config file name (without extension). Viper will search for this file in the configured paths. Requires ConfigType to be set.
func (*OptionsBuilder) WithConfigPaths ¶
func (b *OptionsBuilder) WithConfigPaths(paths ...string) *OptionsBuilder
WithConfigPaths sets the directories to search for config files. Only used when ConfigName is set (not ConfigFile).
func (*OptionsBuilder) WithConfigType ¶
func (b *OptionsBuilder) WithConfigType(configType string) *OptionsBuilder
WithConfigType sets the config file type/format. Valid values: yaml, json, toml, hcl, env, ini, properties.
func (*OptionsBuilder) WithEnvPrefix ¶
func (b *OptionsBuilder) WithEnvPrefix(prefix string) *OptionsBuilder
WithEnvPrefix sets the prefix for environment variables. For example, with prefix "APP", the env var "APP_DATABASE_HOST" will map to the config key "database.host".
func (*OptionsBuilder) WithJSON ¶
func (b *OptionsBuilder) WithJSON(name string) *OptionsBuilder
WithJSON is a shortcut for setting JSON configuration.
func (*OptionsBuilder) WithRemoteConfig ¶
func (b *OptionsBuilder) WithRemoteConfig(config map[string]string) *OptionsBuilder
WithRemoteConfig sets additional remote configuration options.
func (*OptionsBuilder) WithRemoteProvider ¶
func (b *OptionsBuilder) WithRemoteProvider(provider, endpoint, path string) *OptionsBuilder
WithRemoteProvider configures a remote configuration provider. Supported providers: etcd, etcd3, consul, firestore.
func (*OptionsBuilder) WithTOML ¶
func (b *OptionsBuilder) WithTOML(name string) *OptionsBuilder
WithTOML is a shortcut for setting TOML configuration.
func (*OptionsBuilder) WithYAML ¶
func (b *OptionsBuilder) WithYAML(name string) *OptionsBuilder
WithYAML is a shortcut for setting YAML configuration.
type Provider ¶
type Provider struct {
// contains filtered or unexported fields
}
Provider is a hyperion.ConfigWatcher implementation based on spf13/viper. It supports multiple configuration formats (YAML, JSON, TOML) and automatic environment variable override with hot reload capabilities.
func (*Provider) GetFloat64 ¶
GetFloat64 returns the value for the given key as a float64.
func (*Provider) GetStringSlice ¶
GetStringSlice returns the value for the given key as a string slice.
func (*Provider) Unmarshal ¶
Unmarshal unmarshals the configuration at the given key into the provided struct.
func (*Provider) Watch ¶
func (p *Provider) Watch(callback func(event hyperion.ChangeEvent)) (stop func(), err error)
Watch starts watching for configuration file changes and triggers callbacks. It uses fsnotify to watch the configuration file directly.
The callback is invoked whenever the configuration file is modified. Note: The ChangeEvent.Key will contain the filename and Value will be nil for file-based watches. Use the Provider methods to read the updated configuration values.
Multiple callbacks can be registered by calling Watch multiple times. Each callback is assigned a unique ID to ensure safe removal even in concurrent scenarios.
Returns a stop function to cancel watching and an error if watching cannot be started.