Documentation
¶
Overview ¶
Package config provides a single way to manage the configuration of your application. The configuration can be a yaml file and/or a list of environment variable. To set the config using the environment, this package is using the package github.com/nexucis/lamenv, which is able to determinate what is the environment variable that matched the different attribute tof the struct. By default it is based on the yaml tag provided.
The main entry point of this package is the struct Resolver. This struct will allow you to set the path to your config file if you have one and to give the prefix of all of your environment variable. Note:
- A good practice is to prefix your environment variable by the name of your application.
- The config file is not mandatory, you can manage all you configuration using the environment variable.
- The config by environment is always overriding the config by file.
The Resolver at the end returns an object that implements the interface Validator. Each config/struct can implement this interface in order to provide a single way to verify the configuration and to set the default value. The object returned by the Resolver will loop other different structs that are parts of the config and execute the method Verify if implemented.
Example:
import (
"fmt"
"github.com/perses/common/config"
)
type Config struct {
Etcd *EtcdConfig `yaml:"etcd"`
}
func (c *Config) Verify() error {
if c.EtcdConfig == nil {
return fmt.Errorf("etcd config cannot be empty")
}
}
func Resolve(configFile string) (Config, error) {
c := Config{}
return c, config.NewResolver().
SetConfigFile(configFile).
SetEnvPrefix("PERSES").
Resolve(&c).
Verify()
}
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Resolver ¶
type Resolver[T any] interface { SetEnvPrefix(prefix string) Resolver[T] SetConfigFile(filename string) Resolver[T] SetConfigData(data []byte) Resolver[T] AddChangeCallback(func(*T)) Resolver[T] Resolve(config *T) Validator }
Resolver is the interface to set the config file path and the environment variable prefix