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.
- By default, an unknown field in the config is an error. Use Resolver.Strict to ignore the unknown fields.
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 (including the elements of a slice or an array, and the values of a map) and execute the method Verify if implemented.
See ExampleNewResolver for a complete example: Verify checks a value and sets a default one on a nested struct, and an environment variable like PERSES_DATABASE_URL overrides the config file.
Index ¶
Examples ¶
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] Strict(isStrict bool) 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
func NewResolver ¶
NewResolver creates a new resolver
Example ¶
package main
import (
"errors"
"fmt"
"os"
"path/filepath"
"github.com/perses/common/config"
)
type DatabaseConfig struct {
URL string `yaml:"url"`
MaxConns int `yaml:"max_conns"`
}
// Verify checks the config and sets the default values. It is called even if the field database is not in the config file.
func (c *DatabaseConfig) Verify() error {
if len(c.URL) == 0 {
return errors.New("database.url cannot be empty")
}
if c.MaxConns == 0 {
c.MaxConns = 10
}
return nil
}
type Config struct {
Database DatabaseConfig `yaml:"database"`
}
// Resolve reads the config file, then overrides it with the environment variables, like PERSES_DATABASE_URL.
func Resolve(configFile string) (Config, error) {
var c Config
err := config.NewResolver[Config]().
SetConfigFile(configFile).
SetEnvPrefix("PERSES").
Resolve(&c).
Verify()
return c, err
}
func main() {
dir, err := os.MkdirTemp("", "config")
if err != nil {
fmt.Println(err)
return
}
defer func() { _ = os.RemoveAll(dir) }()
configFile := filepath.Join(dir, "config.yaml")
if err = os.WriteFile(configFile, []byte("database:\n url: postgres://file\n"), 0o600); err != nil {
fmt.Println(err)
return
}
c, err := Resolve(configFile)
fmt.Println(c.Database.URL, c.Database.MaxConns, err)
// the environment variables override the config file
_ = os.Setenv("PERSES_DATABASE_URL", "postgres://env")
defer func() { _ = os.Unsetenv("PERSES_DATABASE_URL") }()
c, err = Resolve(configFile)
fmt.Println(c.Database.URL, c.Database.MaxConns, err)
}
Output: postgres://file 10 <nil> postgres://env 10 <nil>