Demo Go Client
Access configuration data with a go application
Introduction
Our go-client for accessing configuration data stored in ConfigSeeder is available at https://gitlab.com/configseeder/go-client.
All examples use the same configuration data set up in section Prepare ConfigSeeder. All examples use the same confiseeder.yaml created in the according section.
Please see https://gitlab.com/configseeder/go-demo for more examples.
Prepare ConfigSeeder
Create a configuration group go-client-demo with the following configuration values and an API Key with access to the created configuration group:
Prepare your go project
Add dependency to go-client
If you are using dep, add the following dependency:
[[constraint]]
branch = "staging"
name = "gitlab.com/configseeder/go-client"
Provide configseeder.yaml
configseeder:
client:
environmentKey: "TEST" (1)
#fullForNonFiltered: true
#dateTime: "2001-02-03T04:05:06"
connectionTimeout: 2000
readTimeout: 2000
configurationGroupKeys: "go-client-demo" (2)
initializationMode: "EAGER"
refreshMode: "LAZY"
refreshCycle: 15000
serverUrl: "https://staging-postgres-config-seeder.oneo.cloud"
apiKey: "<apikey>" (3)
- Set the Environment for which the Configuration Values should be loaded
- Select the Configuration Group
- Enter an API Key with access to the selected Environment and Configuration Group
Example 1: Print all loaded Configuration Values
func main() {
// Setup Flags for ConfigSeeder access configuration
client := configseeder.ClientImplementation{}
client.InitFlags()
flag.Parse()
// initialize go client
err := client.Init(nil)
if err != nil {
fmt.Printf("Error while initializing configseeder client", err)
}
// Get data from ConfigSeeder
configValueDtos, err := client.GetConfigValues()
if err != nil {
fmt.Printf("Error while reading config values", err)
}
// Print values
printConfigValues(configValueDtos)
}
func printConfigValues(configValueDtos []model.ConfigValueDto) {
if len(configValueDtos) > 0 {
writer := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', 0)
_, _ = fmt.Fprintf(writer, "%s\t%s\t%s\t%s\n",
"key", "value", "secured", "environment")
for _, configValueDto := range configValueDtos {
key := configValueDto.Key
value := configValueDto.Value
secured := configValueDto.Secured
environment := getValue(configValueDto.Environment)
_, _ = fmt.Fprintf(writer, "%s\t%s\t%t\t%s\n",
key, value, secured, environment)
}
_ = writer.Flush()
}
}
func getValue(value *string) string {
if value != nil {
return *value
}
return ""
}
When running the application the following output should be generated:
Received 3 ConfigValues from ConfigSeeder:
key value secured environment
a.key some value test false TEST
another.key another value false
secret.key secret value test true TEST
Example 2: Getting the value for a specific key
If only a single Config Value must be retrived, you can use the function GetConfigValue():
func main() {
// Setup Flags for ConfigSeeder access configuration
client := configseeder.ClientImplementation{}
client.InitFlags()
flag.Parse()
// initialize go client
err := client.Init(nil)
if err != nil {
fmt.Printf("Error while initializing configseeder client", err)
}
// Get configvalue from ConfigSeeder
key := "a.key"
configValue, err := client.GetConfigValue(key)
if err != nil {
fmt.Printf("Error while reading config value", err)
}
fmt.Printf("Value for key %s: %s", key, configValue.Value)
}
This code should generate the following output:
Value for key a.key: some value test