Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/antelman107/json-env-go
GO package to deal with complex configs provided through environment variables.
https://github.com/antelman107/json-env-go
configuration environment-variables golang json viper
Last synced: about 1 month ago
JSON representation
GO package to deal with complex configs provided through environment variables.
- Host: GitHub
- URL: https://github.com/antelman107/json-env-go
- Owner: antelman107
- Created: 2020-07-01T12:38:25.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2020-07-01T13:19:13.000Z (over 4 years ago)
- Last Synced: 2024-06-20T17:46:55.275Z (8 months ago)
- Topics: configuration, environment-variables, golang, json, viper
- Language: Go
- Homepage:
- Size: 5.86 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# What is it?
Tiny (1 line) GO package to deal with complex configs provided through environment variables.# Why we need it?
In dockerized applications and by https://12factor.net/ recommendation
we have to pass configuration for our GO program through environment variables.It is easy if configuration consists of simple values (strings, numbers, boolean, etc), something like this:
```json
{
"login":"user",
"password":"somepass",
"mfa_enabled": true
}
```In this case we could use several environment variables like `APP_LOGIN="user" APP_PASSWORD="somepass"`.
But how would we work with complex configuration that, for example, consists of arrays of objects? For example:
```json
{
"nodes":[
{
"url":"http://1.localhost/",
"priority":1,
"enabled":true
},
{
"url":"http://2.localhost/",
"priority":2,
"enabled":false
}
]
}
```This package is aimed to deal with such configs.
Just put the config into single environment variable.
It could be multiline, but work with single line is simplier from `bash`.Running program `bash` example:
```bash
CONFIG='{"nodes":[{"url":"http:\/\/1.localhost\/","priority":1,"enabled":true},{"url":"http:\/\/2.localhost\/","priority":2,"enabled":false}]}' ./app
```GO program code example:
```GO
import jsonEnvGo "github.com/antelman107/json-env-go"type Config struct {
Nodes []struct {
URL string `json:"url"`
Priority int `json:"priority"`
Enabled bool `json:"enabled"`
} `json:"nodes"`
}func main() {
var cfg Config
if err := jsonEnvGo.DecodeConfigFromEnv("CONFIG", &cfg); err != nil {
logger.Error(err)
return
}
// cfg now filled, do something with it
// ...
}
```This package is very tiny, it can be used instead of viper (https://github.com/spf13/viper) when working especially with json configs coming from environment variables.