
appconfig
SpringBoot like profile based application configuration loader.
See confita for details.
Settings
ActiveProfileEnvName
Environment variable name to specify active profile.
Default value is app.profiles.active
appconfig.ActiveProfileEnvName = "app.profiles.active"
ConfigFilenamePrefix
Configuration filename prefix.
Default value is application
.
appconfig.ConfigFilenamePrefix = "application"
Load Precedence
- Environment variable.
- Environment variable is read from
config
tag.
- If field tag is
nested.first-name
, field value will be read from nested.first-name
, NESTED_FIRST_NAME
environment variable if set.
- List items are separated by comma(
,
).
- Specified configFilename file (passed as
LoadConfig()
argument)
config/application-{profile}.yaml
application-{profile}.yaml
config/application.yaml
application.yaml
config/application-{profile}.yaml
Example
Suppose we have set environment variables as:
FOO_CONFIG_FILE
=myconfig.yml
foo.profiles.active
=dev
The following code will load files in the following order:
myconfig.yml
config/foo-dev.yaml
foo-dev.yaml
config/foo.yaml
foo.yaml
package appconfig
import (
"fmt"
"github.com/lechuckroh/appconfig"
"os"
)
type Config struct {
Name string `config:"name"`
Age int `config:"age"`
Tags []string `config:"tags"`
Nested struct {
Name string `config:"nested.name"`
Age int `config:"nested.age"`
}
}
func main() {
appconfig.ActiveProfileEnvName = "foo.profiles.active"
appconfig.ConfigFilenamePrefix = "foo"
configFilename := os.Getenv("FOO_CONFIG_FILE")
config := Config{}
loadedFilenames, err := appconfig.LoadConfig(configFilename, &config)
if err != nil {
fmt.Printf("Failed to load config: %s\n", err.Error())
return
}
for idx, filename := range loadedFilenames {
fmt.Printf("Configuration loaded: [%d] %s\n", idx+1, filename)
}
if len(loadedFilenames) == 0 {
fmt.Println("No config file loaded")
}
fmt.Printf("Config: %+v\n", config)
}