Documentation
¶
Overview ¶
Package config provides configuration loading and validation for gokit applications.
It uses Viper to load configuration from files and environment variables, supporting multiple formats (YAML, JSON, TOML) and environment-specific overrides.
Usage ¶
cfg, err := config.Load[MyConfig]("config.yaml")
Environment variables override file values using the APP_ prefix with underscore-separated paths (e.g., APP_DATABASE_HOST).
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func LoadConfig ¶
func LoadConfig(serviceName string, cfg interface{}, opts ...LoaderOption) error
LoadConfig loads configuration for a service into the provided cfg struct. It searches for config.yml and .env files in standard locations, binds environment variables, and unmarshals the result into cfg.
Types ¶
type FileSystem ¶
type FileSystem interface {
Exists(path string) bool
LoadEnv(path string) error
Getwd() (string, error)
}
FileSystem interface for file operations (useful for testing).
type LoaderConfig ¶
type LoaderConfig struct {
FileSystem FileSystem
ConfigFile string // Direct config file path (optional)
EnvFile string // Direct env file path (optional)
}
LoaderConfig holds dependencies and optional file overrides.
type LoaderOption ¶
type LoaderOption func(*LoaderConfig)
LoaderOption is a functional option for LoadConfig.
func WithConfigFile ¶
func WithConfigFile(path string) LoaderOption
WithConfigFile sets an explicit config file path.
func WithEnvFile ¶
func WithEnvFile(path string) LoaderOption
WithEnvFile sets an explicit .env file path.
func WithFileSystem ¶
func WithFileSystem(fs FileSystem) LoaderOption
WithFileSystem sets a custom filesystem for the loader.
type RealFileSystem ¶
type RealFileSystem struct{}
RealFileSystem implements FileSystem using actual file operations.
func (*RealFileSystem) Exists ¶
func (rfs *RealFileSystem) Exists(path string) bool
func (*RealFileSystem) Getwd ¶
func (rfs *RealFileSystem) Getwd() (string, error)
func (*RealFileSystem) LoadEnv ¶
func (rfs *RealFileSystem) LoadEnv(path string) error
type ResolvedFiles ¶
ResolvedFiles contains the resolved config and env file paths.
type Resolver ¶
type Resolver struct {
FileSystem FileSystem
}
Resolver handles finding and resolving config and env files.
func (*Resolver) ResolveFiles ¶
func (cr *Resolver) ResolveFiles(serviceName string, opts LoaderConfig) ResolvedFiles
ResolveFiles finds config and env files for a service. Returns explicit paths if provided, otherwise searches for them.
type ServiceConfig ¶
type ServiceConfig struct {
Name string `yaml:"name" mapstructure:"name"`
Environment string `yaml:"environment" mapstructure:"environment"`
Version string `yaml:"version" mapstructure:"version"`
Debug bool `yaml:"debug" mapstructure:"debug"`
Logging logger.Config `yaml:"logging" mapstructure:"logging"`
}
ServiceConfig contains the essential configuration fields every service needs. Projects extend this by embedding it in their own config structs.
Example:
type MyConfig struct {
gkconfig.ServiceConfig `yaml:",inline" mapstructure:",squash"`
Database database.Config `yaml:"database" mapstructure:"database"`
}
func (*ServiceConfig) ApplyDefaults ¶
func (c *ServiceConfig) ApplyDefaults()
ApplyDefaults applies default values to the base configuration. Override this in embedding structs and call c.ServiceConfig.ApplyDefaults() first.
func (*ServiceConfig) GetServiceConfig ¶
func (c *ServiceConfig) GetServiceConfig() *ServiceConfig
GetServiceConfig returns the base ServiceConfig. When embedded in a larger config struct, this method is promoted so the embedding struct automatically satisfies the Config interface.
func (*ServiceConfig) Validate ¶
func (c *ServiceConfig) Validate() error
Validate validates the base configuration fields. Override this in embedding structs and call c.ServiceConfig.Validate() first.