Config lib is a library to handle configuration in your program.
It handles config file and environments variables.
It uses viper and koanf to handle, respectively, env variables and configuration.
This library handle json in a case sensitive mode.
Install
go get -u github.com/mia-platform/configlib
Example usage
Get env variables
type EnvVariables struct {
StringValue string
BoolValue bool
}
var envVariablesConfig = []configlib.EnvConfig{
{
Key: "SOME_STRING",
Variable: "StringValue",
DefaultValue: "",
},
{
Key: "BOOLEAN_KEY",
Variable: "BoolValue",
Required: true,
},
}
var env EnvVariables
if err := configlib.GetEnvVariables(envVariablesConfig, &env); err != nil {
panic(err.Error())
}
Load file service json configuration - with json schema validation
type Config struct {}
func loadServiceConfiguration(path, fileName string) (Config, error) {
jsonSchema, err := configlib.ReadFile(configSchemaPath)
if err != nil {
return nil, err
}
var config ServiceConfig
if err := configlib.GetConfigFromFile(fileName, path, jsonSchema, &config); err != nil {
return nil, err
}
return config, err
}
// Load service configuration
config, err := loadServiceConfiguration("my/path", "file")
if err != nil {
log.Fatal(err.Error())
}
Load file service json configuration - without json schema validation
type Config struct {}
func loadServiceConfiguration(path, fileName string) (Config, error) {
var config ServiceConfig
if err := configlib.GetConfigFromFile(fileName, path, nil, &config); err != nil {
return nil, err
}
return config, err
}
// Load service configuration
config, err := loadServiceConfiguration("my/path", "file")
if err != nil {
log.Fatal(err.Error())
}
Contributing
Please read CONTRIBUTING.md for details on our code of conduct,
and the process for submitting pull requests to us.
Versioning
We use [SemVer][semver] for versioning. For the versions available,
see the tags on this repository.
License
This project is licensed under the Apache License 2.0 - see the LICENSE.md
file for details