GithubHelp home page GithubHelp logo

gostrufig's Introduction

Gostrufig

Struct driven config management for your Go application!

As a developer, how many times have you been at the mercy of an underlying configuration library to populate configuration data stored in a struct? If you're anything like me, too many to count. That's why there's now Gostrufig! Using a few simple tags and the Gostrufig library, you can manage all of your configuration items right in the struct! Plus, as an added bonus, there's an included persistence layer that will pull data from Etcd. Want an example? First let's do a little importing of packages:

import (
	"github.com/brockwood/gostrufig"
	"github.com/brockwood/gostrufig/driver/etcd" // The gostrufig driver I would like to use
)

With that out of the way, let's get some config data:

type MyConfigInfo struct {
		DecodeDir   string `cfg-def:"/home/user/decoder"`
		Environment string `cfg-ns:"true" cfg-def:"developer"`
		Timer       int
		Type        string
		TestTimeout float64
}
ns := MyConfigInfo{}
etcddriver := etcd.GetGostrufigDriver()
gostrufig := GetGostrufig("appname", "http://localhost:2379", etcddriver)
gostrufig.RetrieveConfig(&ns)

Drivers

Gostrufig comes with a driver interface so that a program can retrieve their configuration data from a backend. Right now, the only driver is for Etcd but the interface is written in such a way that other backends can easily be added.

Struct Tags And Their Function

Does this wet your appetite? Good. Let me fill you in on those sweet sweet tags!

cfg-def

This dandy little tag will allow you to set a default value for that field. Don't worry, Gostrufig handles all the reflection to figure out how to turn that string into the value your struct desires. Set it and forget it!

cfg-ns

This guy might be a bit confusing at first but, in the age of microservices and centralized configs, it'll make more sense. If your app runs in multiple environments but shares a common config, this namespace tag will construct a unique path for that value. For the example above, the configuration would be located at /appname/developer in Etcd. If Environment was set to "production" instead, it would be /appname/production.

Environmental Awareness

To go along with setting a default value in the struct itself, Gostrufig will look at the shell environment for any configuration items. If you wish to override the Environment field above in the shell, just go ahead and set the value for APPNAME_ENVIRONMENT. A good example would be:

export APPNAME_ENVIRONMENT=production

Gostrufig will automagically look for the capitalized names combining the application name and the field name. It's just that easy!

TODO

This is just the start. Coming soon will be:

  • Allowing a program to commit a config back to the persistence layer (you know, if your app has never run against an Etcd server, save your config to Etcd on first run)
  • A YAML and/or JSON persistence layer
  • Notification when a configuration changes and allow an app to update the config without a restart

gostrufig's People

Contributors

achura avatar tim-kretschmer-c2fo avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

gostrufig's Issues

Add Godoc Comments

The initial checkin is void of any Godoc comments. Add those so we can be a better community library.

Clean up Etcd code

Clean up the Etcd code so that it works properly with nested structs.

Allow Encryption of Member Values

When persisting and retrieving values for a persistence layer, Gostrufig should support encrypting and decrypting values using PGP or similar mechanism.

How should gostrufig handle populating array values?

I am testing out configs in the c2fo node and go baselines to see what features are missing in gull. There is a current mismatch between what gull puts into etcd for an array and what gostrufig reads into a struct.

Here's an example

func TestArrayParse(t *testing.T) {
    type MyStruct struct {
        ServiceHosts []string
    }
    blankStruct := MyStruct{}
    os.Setenv("C2FO_SERVICEHOSTS", "[host1:6379, host2:6379]")
    gostrufig := GetGostrufig("appname", "http://localhost:2379", nil)
    gostrufig.setInitialStructValues(&blankStruct, "c2fo")
    populatedStruct := MyStruct{
        ServiceHosts: []string{
            "host1:6379",
            "host2:6379",
        },
    }
    if reflect.DeepEqual(blankStruct, populatedStruct) {
        t.Log("Blank and populated structs are the same.")
    } else {
        fmt.Printf("POP: [%+v]\n", populatedStruct)
        fmt.Printf("LOD: [%+v]\n", blankStruct)
        t.Errorf("Comparison of blank and populated structs failed.")
    }
}

In gofigure all the values loaded into etcd are serialized JSON. However, gostrufig is expecting raw strings and then reflecting on them. Currently, gull can either store json encoded values or stringified values. Here is a golang array after it has been turned into a string.

"[host1:6379, host2:6379]"

@brockwood do you have any thoughts on how gostrufig can handle arrays? I assume gostrufig is the tool that would need tweaking, but if something else needs to be changed let me know.

I am currently thinking that gostrufig could have a naive array handler that splits the string and tries to parse the parts of the array using the reflection already present in gostrufig.

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.