Configuration library to load from multiple sources.
go get github.com/rakunlabs/chuDefine a struct to hold the configuration.
type Config struct {
Name string `cfg:"name"`
Age int `cfg:"age"`
Secret string `cfg:"secret" log:"-"` // skip this field in chu.MarshalMap
}And load the configuration.
cfg := Config{}
if err := chu.Load(ctx, "test", &cfg); err != nil {
return fmt.Errorf("failed to load config: %w", err)
}
slog.Info("loaded configuration", "config", chu.MarshalMap(ctx, cfg))The configuration will be loaded from the following sources in order:
- Default
- File
- Http
- Environment
chu.MarshalMap or chu.MarshalJSON print the configuration, skipping the fields log:"false" tag and value unless 1, t, T, TRUE, true, True makes false.
String func use fmt.Stringer interface checks to print the configuration.
CONFIG_NAME_PREFIX env value can be used to set the prefix for the configuration.
Check example folder to see how to use loaders with different kind of configuration.
Default loader is used to set default values from tag default.
type Config struct {
Name string `cfg:"name" default:"John"`
Age int `cfg:"age" default:"30"`
}Default supports numbers, string, bool, time.Duration and pointer of that types.
HTTP loader is used to load configuration from HTTP server.
| Env Value | Description | Default |
|---|---|---|
CONFIG_HTTP_ADDR |
HTTP server address, not exist than skips loader | - |
CONFIG_HTTP_PREFIX |
Prefix for the configuration | - |
It send GET request to the server with CONFIG_HTTP_ADDR env value with appending the name as path.
204 or 404 response code will skip the loader, only accept 200 response code.
File loader is used to load configuration from file.
First checking CONFIG_FILE env value and try current location to find in order of .toml, .yaml, .yml, .json extension with using given name.
Environment loader is used to load configuration from environment variables.
env or cfg tag can usable for environment loader.
If you have .env, .env.local file in the current directory, it will be loaded automatically.
Add extra env file using CONFIG_ENV_FILE env value.
export NAME=John
export AGE=30type Config struct {
Name string `cfg:"name"`
Age int `cfg:"age"`
}When loading configuration, usable to change env loader's options.
err := chu.Load(ctx, "my-app", &cfg,
// now you need to set prefix "MY_APP_" for env variables
chu.WithLoaderOption(loaderenv.New(
loaderenv.WithPrefix("MY_APP_"),
)),
)Use the noprefix tag option on fields that should also check unprefixed env vars as a fallback.
The prefixed value takes priority when both exist.
type Config struct {
AppName string `cfg:"app_name"` // only MY_APP_APP_NAME
LogLevel string `cfg:"log_level,noprefix"` // MY_APP_LOG_LEVEL, falls back to LOG_LEVEL
}This loaders not enabled by default. Import the package to enable it.
Use chu.WithLoaderOption to set the loader options.
Or use chu.WithLoader to set the loaders manually.
#### Vault
Vault loader is used to load configuration from HashiCorp Vault.
This is not enabled by default.
Enable Vault loader importing the package.
import (
_ "github.com/rakunlabs/chu/loader/external/loadervault"
)| Env Value | Description | Default |
|---|---|---|
VAULT_SECRET_BASE_PATH |
Prefix for the configuration, must given base | - |
VAULT_ADDR VAULT_AGENT_ADDR |
Vault server address, not exist than skips loader | - |
VAULT_ROLE_ID |
Role ID for AppRole authentication, for role login | - |
VAULT_SECRET_ID |
Secret ID for AppRole authentication, for role login | - |
VAULT_APPROLE_BASE_PATH |
Base path for AppRole authentication, for role login | auth/approle/login |
#### Consul
Consul loader is used to load configuration from HashiCorp Consul.
This is not enabled by default.
Enable Consul loader importing the package.
import (
_ "github.com/rakunlabs/chu/loader/external/loaderconsul"
)| Env Value | Description | Default |
|---|---|---|
CONSUL_CONFIG_PATH_PREFIX |
Prefix for the configuration | - |
CONSUL_HTTP_ADDR |
Consul server address, not exist than skips loader | - |