Env Vars
All environment variables reside in pkg/config/features
. They're categorised within their respective features such as database.go
or service.go
. Each env var must have a mapstructure:
decoration that spells it in caps when parsing an ENV. You can add your own, it's as simple as adding a new line in any of these files, or create your own.
Below is a sample of pkg/config/features/service.go
:
type ServiceConfig struct {
Host string `mapstructure:"HOST"`
ProtectedApiPort string `mapstructure:"PROTECTED_API_PORT"`
PublicApiPort string `mapstructure:"PUBLIC_API_PORT"`
HiddenApiPort string `mapstructure:"HIDDEN_API_PORT"`
LogLevel string `mapstructure:"LOG_LEVEL"`
RequestTimeoutDuration string `mapstructure:"REQUEST_TIMEOUT_DURATION"`
WatcherSleepInterval string `mapstructure:"WATCHER_SLEEP_INTERVAL"`
}
var service = &Feature{
Name: constants.FEATURE_SERVICE,
Config: &ServiceConfig{},
enabled: true,
configured: false,
ready: false,
requirements: []string{
"Host",
"ProtectedApiPort",
"PublicApiPort",
"HiddenApiPort",
"LogLevel",
"RequestTimeoutDuration",
"WatcherSleepInterval",
},
}
func init() {
Features.Add(service)
}
From the example above, you can find a type ServiceConfig
that states what env vars are to be expected. These are automatically read from the environment. Env vars must belong to a feature which can be toggled on or off. A feature can also define which env vars are required for it to start.
If you wish to disable a feature, you can mention it in the list of DISABLE_FEATURES
var in run-time.
Reading the env vars is the job of pkg/config/envVars.go
. Each config struct must be registered in envVars.go
. The config struct is then automatically injected to its respective feature after initialisation.
It is possible to set a default value for each variable, this can be done in pkg/config/envVars.go
under setDefaults()
.
By the time the CMD calls the Proc, all env vars should have already been read and injected into their features, making them available for the rest of the package.