An opinionated configuration loading framework for Containerized and Cloud-Native applications.
MIT License
Configuro is an opinionated configuration loading and validation framework with not very much to configure. It defines a method for loading configurations without so many options for a straight-forward and simple code.
The method defined by Configuro allow you to implement 12-Factor's Config and it mimics how many mature applications do configurations (e.g Elastic, Neo4J, etc); and is also fit for containerized applications.
With Only with two lines of code, and zero setting up you get loading configuration from Config File, Overwrite values /or rely exclusively on Environment Variables, Expanding values using ${ENV} expressions, and validation tags.
Load()
the read configuration into.database.password
can be set by setting CONFIG_DATABASE_PASSWORD
. (CONFIG_
default prefix can be changed)_
then replace with __
in the Environment Variable.CONFIG: {"a":123, "b": "abc"}
).env
file to load environment variables that are not set by the OS.config.yml
; name and extension can be configured..yml
, .yaml
, .json
, and .toml
.host: www.example.com:%{PORT|3306}
with 3306
being the default value if env ${PORT} is not set).Validatable
Interface Validate() error
.OS EnvVar
> .env EnvVars
> Config File
> Value set in Struct before loading.
go get github.com/sherifabdlnaby/configuro
import "github.com/sherifabdlnaby/configuro"
This is the struct you're going to use to retrieve your config values in-code.
type Config struct {
Database struct {
Host string
Port int
}
Logging struct {
Level string
LineFormat string `config:"line_format"`
}
}
.
(e.g Database.Host
)config
tag to change field name if you want it to be different from the Struct field name.config
tag for unmarshalling.Configuro.Config
object. // Create Configuro Object with supplied options (explained below)
config, err := configuro.NewConfig( opts ...configuro.ConfigOption )
// Create our Config Struct
configStruct := &Config{ /*put defaults config here*/ }
// Load values in our struct
err = config.Load(configStruct)
opts ...configuro.ConfigOption
which is explained in the below sections.database.host
can be expressed in environment variables as CONFIG_DATABASE_HOST
._
then replace them with __
in the Environment Variable.CONFIG_
prefix can be configured.CONFIG: {"a":123, "b": "abc"}
).env
file to load environment variables that are not set by the OS. (notice that .env is loaded globally in the application scope)The above settings can be changed upon constructing the configuro object via passing these options.
configuro.WithLoadFromEnvVars(EnvPrefix string) // Enable Env loading and set Prefix.
configuro.WithoutLoadFromEnvVars() // Disable Env Loading Entirely
configuro.WithLoadDotEnv(envDotFilePath string) // Enable loading .env into Environment Variables
configuro.WithoutLoadDotEnv() // Disable loading .env
filepath
.
filename
=> "config.yml"Yaml
, Json
, and Toml
.CONFIG_DIR
.The above settings can be changed upon constructing the configuro object via passing these options.
configuro.WithLoadFromConfigFile(Filepath string, ErrIfFileNotFound bool) // Enable Config File Load
configuro.WithoutLoadFromConfigFile() // Disable Config File Load
configuro.WithEnvConfigPathOverload(configFilepathENV string) // Enable Overloading Path with ENV var.
configuro.WithoutEnvConfigPathOverload() // Disable Overloading Path with ENV var.
${ENV}
and ${ENV|default}
expressions are evaluated and expanded if the Environment Variable is set or with the default value if defined, otherwise it leaves it as it is.config:
database:
host: xyz:${PORT:3306}
username: admin
password: ${PASSWORD}
The above settings can be changed upon constructing the configuro object via passing these options.
configuro.WithExpandEnvVars() // Enable Expanding
configuro.WithoutExpandEnvVars() // Disable Expanding
err := config.Validate(configStruct)
if err != nil {
return err
}
Validatable
Interface that will be called on any type that implements it recursively, also on each element of a Map or a Slice.Validatable
Interface. (default: recursively) configuro.WithValidateByTags()
configuro.WithoutValidateByTags()
configuro.WithValidateByFunc(stopOnFirstErr bool, recursive bool)
configuro.WithoutValidateByFunc()
config
and validate
tag can be renamed using configuro.Tag(structTag, validateTag)
construction option.MIT License Copyright (c) 2020 Sherif Abdel-Naby
PR(s) are Open and Welcomed.