confunc
provides a functional approach to configure an application. With confunc
, each configuraion value can be defined as a function, providing a dynamic value rather than a static one that is assigned once the application starts up.
Example:
// declare your configuration value
maxNumberOfOpenConnections := confunc.From(confunc.Env()).Int("MAX_OPEN_CONNECTIONS")
...
// when you need to access the value
if connectionCount < maxNumberOfOpenConnections() {
// do your thing
}
Source is where the configuration is retrieved. It is defined by the Source
interface where a string value is returned for a specified key. Once it is passed to confunc.From()
function; a struct that can return configuration functions is created. Available sources are:
Env()
: retrieves Environment VariablesMap(map[string]string)
: retrieves configuration from a mapcfconsul.Source(*api.Config)
: retrieves configuration from consul. You need to importconfunc/cfconsul
package
confunc
provides a middleware mechanism via Interceptor
type. It enables handling of various cases like default value
maxNumberOfOpenConnections := confunc.From(confunc.Env()).Int("MAX_OPEN_CONNECTIONS", confunc.Default("10"))
...
// when you need to access the value
if connectionCount < maxNumberOfOpenConnections() {
// do your thing
}
Interceptor
is a simple function that accepts a Confunc
function and also returns Confunc
function.
type Confunc func() (string, error)
type Interceptor func(Confunc) Confunc
Interceptors process the configuration value before it is accessed, every time. Custom interceptors can be easily introduces by providing a function matching the above signature. Available interceptors are:
Default(defaultValue string)
: returns a default value if there is no configuration madeCacheOnce()
: caches the value retrieved once and prevents accessing the configuration source each timeCacheFor(cacheDuration time.Duration)
: caches the value for a duration provided.