1
0
Fork 0
Configuration tool with a GUI v2
Go to file
Gregory Eremin be052e2c75 Reflect magic 2015-08-29 21:51:12 +03:00
demo Reflect magic 2015-08-29 21:51:12 +03:00
static Add web server 2015-08-29 19:34:30 +03:00
LICENCE Add licence 2015-08-29 19:34:41 +03:00
README.md Readme fix #2 2015-08-29 20:44:48 +03:00
field.go Use json keys as field path tokens 2015-08-29 19:34:10 +03:00
field_test.go Fix tests 2015-08-29 20:46:42 +03:00
file_ops.go Add demo project 2015-08-29 19:58:24 +03:00
secondly.go Reflect magic 2015-08-29 21:51:12 +03:00
secondly_test.go Give package a new name 2015-08-29 15:41:20 +03:00
server.go Add web server 2015-08-29 19:34:30 +03:00
static.rice-box.go Add compiled assets 2015-08-29 20:37:08 +03:00

README.md

Secondly

Secondly is a configuration management plugin for Go projects. It taks care of the app's configuration, specifically of updating it in runtime.

Configuration

First we need to define a struct that will hold app's configuration. Let's make it simple for demostration purposes.

type Config struct {
    AppName string  `json:"app_name"`
    Version float32 `json:"version"`
}

Make sure you've defined json tags on each field.

Next, right where you will define your app's flags ask Secondly to add one for configuration file.

secondly.SetupFlags()
flag.Parse()

Now you can pass a configuration file to your program like this:

./app -config=config.json

Now we need to ask Secondly to take care of your configuration:

var conf Config
secondly.Manage(&conf)

// or asynchronously
go secondly.Manage(&conf)

If you prefer to configure the app asynchronously then you'll probably want to know when configuration is loaded, so there's a handly helper function just for that:

secondly.OnLoad(func(){
    log.Println("Configuration initialized")
})

Congratulations! You've just configured Secondly to read and initialize the configuration of your app. But this is not what you came for, right?

If you want a configuration GUI, simply start Secondly's web server on a port you want.

secondly.StartServer("", 5500)

Tired of restarting the app every time you modify the config? You're not alone.

secondly.HandleFSEvents()

Want some more control over when specifically the config will be reloaded? Ask Secondly to listen for SIGHUP syscalls.

secondly.HandleSIGHUP()

You can also set up callback functions on specific fields and receive a call when this fields value changes.

secondly.OnChange("NumWorkers", func(oldVal, newVal interface{}) {
    old := oldVal.(int)
    cur := newVal.(int)
    if cur > old {
        pool.AddWorkers(cur - old)
    } else {
        pool.StopWorkers(old - cur)
    }
    log.Println("Number of workers changed from %d to %d", old, cur)
}
})

Full example can be found here.

Demo Screenshot

Building

The only thing to keep in mind when building Secndly is to convert assets into a binary form so they could be kept in memory of your app and would not require any additional web server configuration.

go get github.com/GeertJohan/go.rice/rice
rice embed-go
go build

Licence

Secondly is distributed under the MIT Licence.