https://github.com/railsware/go-global
Golang configuration reader for AWS Parameter Store and more
https://github.com/railsware/go-global
Last synced: 9 months ago
JSON representation
Golang configuration reader for AWS Parameter Store and more
- Host: GitHub
- URL: https://github.com/railsware/go-global
- Owner: railsware
- Created: 2020-05-25T05:34:18.000Z (over 5 years ago)
- Default Branch: v2
- Last Pushed: 2025-03-19T16:23:10.000Z (10 months ago)
- Last Synced: 2025-04-22T23:11:49.269Z (9 months ago)
- Language: Go
- Size: 54.7 KB
- Stars: 4
- Watchers: 14
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Go companion modules for Ruby Global gem
This repository contains modules to load configuration analogous to the [Global gem](https://github.com/railsware/global). Refere to Global's documentation for more details
## AWS Parameter Store usage example
See [main Global documentation](https://github.com/railsware/global#aws-parameter-store-1) for setup instructions.
```sh
go get github.com/railsware/go-global/v2/aws
```
```go
import (
"os"
awsConfig "github.com/aws/aws-sdk-go-v2/config"
globalAWS "github.com/railsware/go-global/v2/aws"
)
type Config struct {
Database struct {
PoolSize int `json:"pool_size"`
URLs []string `json:"urls"`
} `json:"database"`
}
var config Config
awsConfig, err := awsConfig.LoadDefaultConfig(context.TODO())
if err != nil {
log.Fatalf("cannot load aws config: %v", err)
}
awsParamPrefix := os.Getenv("AWS_PARAM_PREFIX")
err := globalAWS.LoadConfigFromParameterStore(
awsConfig,
globalAWS.LoadConfigOptions{ParamPrefix: awsParamPrefix},
&config
)
// config.Database.URLs[0] loaded from /param_prefix/database/urls/0
// config.Database.PoolSize loaded from /param_prefix/database/pool_size
```
- Supported value types: `string`, `int`, `bool` ("true"/"false").
- A `[]byte` can be loaded from Base64 encoded string.
- Complex type should be either a `struct`, a `map` or a `slice`. You can arbitrarily nest them.
- For structs, use `global` or `json` tag to set field name.
- For maps, the key name is the map key (maps must use strings as keys.)
- For slices, all subscripts in Parameter Store must be integers.
### Shorthand for running on AWS ECS or Lambda
If you use Global in AWS environments, you can DRY up the code by following a convention:
- Set up param prefix in the `AWS_PARAM_PREFIX` environment variable.
- Make sure the IAM role has permissions to the params
Then you can simply use:
```go
var config Config
// Will panic if anything goes wrong
globalAWS.MustLoadConfig(&config)
```
### Handling unmapped params
By default, Global assumes that all params from Parameter Store must be mapped to config fields, and returns a warning if some param does not correspond to a config field.
Sometimes, it is expected that the config only contains a subset of the params, and such a warning is unnecessary.
In this case, set `options.IgnoreUnmappedParams` to true. Note that other mapping issues, like a type mismatch, will still cause an error.