Giter VIP home page Giter VIP logo

consulstructure's Introduction

consulstructure

consulstructure is a Go library for decoding Consul data into a Go structure and keeping it in sync with Consul.

The primary use case for this library is to be able to build native Consul-based configuration into your Go applications without needing glue such as consul-template.

Installation

Standard go get:

$ go get github.com/mitchellh/consulstructure

Features

Below is a high-level feature list:

  • Watch a key prefix in Consul KV to populate a Go structure.

  • Notification on a channel when configuration is updated.

  • Configuration structures support all Go primitive types, maps, and structs. Slices and arrays aren't supported since they don't mean anything in the data model of Consul KV.

  • Nested and embedded structs in configuration structures work.

  • Set quiescence periods to avoid a stampede of configuration updates when many keys are updated in a short period of time.

  • Supports all connection features of Consul: multi-datacenter, encryption, and ACLs.

Usage & Example

For docs see the Godoc.

An example is shown below:

import (
    "fmt"

    "github.com/mitchellh/consulstructure"
)

// Create a configuration struct that'll be filled by Consul.
type Config struct {
    Addr     string
    DataPath string `consul:"data_path"`
}

// Create our decoder
updateCh := make(chan interface{})
errCh := make(chan error)
decoder := &consulstructure.Decoder{
    Target:   &Config{},
    Prefix:   "services/myservice",
    UpdateCh: updateCh,
    ErrCh:    errCh,
}

// Run the decoder and wait for changes
go decoder.Run()
for {
    select {
    case v := <-updateCh:
        fmt.Printf("Updated config: %#v\n", v.(*Config))
    case err := <-errCh:
        fmt.Printf("Error: %s\n", err)
    }
}

But Why Not a File?

A file is the most portable and technology agnostic way to get configuration into an application. I'm not advocating this instead of using files for the general case.

For organizations that have chosen Consul as their technology for configuration, services, etc. being able to build services that can be started without any further configuration and immediately start running is very attractive. You no longer have a configuration step where you have to setup services and file templates and so on with tools like consul-template.

You just install the Go binary, start it, and it is going. To update it, you just update the settings in Consul, and the application automatically updates. No more SIGHUP necessary, no more manual restarts.

consulstructure's People

Contributors

mitchellh avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.