consul

package module
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 10, 2020 License: MIT Imports: 5 Imported by: 0

README

Go Report Card

Configify: Consul Plugin

This is the Consul plugin for Configify, a Go library for grabbing values from various places that have your program's configuration data. You can use data from your Consul cluster's Key/Value exactly the same way you would use your OS's environment variables.

Getting Started

go get github.com/robsignorelli/configify-consul

Basic Usage

You'll need to provide your own Consul client instance. This way if you're also using Consul for service discovery, you can reuse that connection to read from your Key/Values as well.

// Connect to Consul. You can use it outside of Configify, too.
client, err := api.NewClient(api.DefaultConfig())
if err != nil {
	// Handle as you see fit...
}

// Create a source to read from Consul 
source, err := consul.NewSource(consul.Options{
    Client:  client,
    Options: configify.Options{Context: context.Background()},
})
if err != nil {
	// Handle as you see fit...
}

// Just like any configify.Source, you can read values individually...
stringValue, ok := source.String("HTTP_HOST")
int16Value, ok := source.Int16("HTTP_PORT")
...

Struct Binding

You can look at the Struct Binding example in configify proper. All you need to do is feed your Consul-backed source to the binder rather than the Environment-backed binder.

https://github.com/robsignorelli/configify#struct-binding

Watch for Updates

You can let configify automatically fire a callback whenever we detect changes to your app's config, so you can react accordingly. Now you can update your configuration w/o having to restart your program.

source, _ := consul.NewSource(...)
source.Watch(func (updated configify.Source) {
	myConfig.RetryCount, _ := updated.Int("RETRY_COUNT")
	myConfig.SomeToken, _ := updated.String("SOME_TOKEN")
	...
})

Be aware that your Watcher callback does NOT fire when the Consul source loads config values for the first time (i.e. when you call NewSource). It only fires when it detects a modification to the KV store any time after the source was initialized.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewSource

func NewSource(opts ...configify.Option) (configify.SourceWatcher, error)

NewSource creates a new config source that is backed by a Consul Key/Value store. You provide the consul client (so you can share connections w/ your service discovery and such) and this will extract config values for you.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL