1
0
Fork 0
secondly/secondly.go

217 lines
4.3 KiB
Go
Raw Normal View History

2015-08-29 12:41:20 +00:00
package secondly
2015-08-29 09:45:00 +00:00
import (
2015-08-29 16:50:22 +00:00
"bytes"
2015-08-29 09:45:00 +00:00
"encoding/json"
2015-08-29 10:51:28 +00:00
"flag"
2015-08-29 13:56:19 +00:00
"fmt"
2015-08-29 09:59:45 +00:00
"log"
2015-08-29 11:14:53 +00:00
"os"
"os/signal"
"path/filepath"
2015-08-29 09:45:00 +00:00
"reflect"
"strings"
2015-08-29 11:14:53 +00:00
"syscall"
"github.com/howeyc/fsnotify"
2015-08-29 09:45:00 +00:00
)
var (
2015-08-29 10:51:28 +00:00
config interface{} // config stores application config
configFile string
callbacks = make(map[string][]func(oldVal, newVal interface{}))
initialized bool
2015-08-29 17:36:27 +00:00
initFunc func()
2015-08-29 09:45:00 +00:00
)
// SetupFlags sets up Confection configuration flags.
func SetupFlags() {
2015-08-29 14:32:39 +00:00
if flag.Parsed() {
log.Fatalln("secondly.SetupFlags() must be called before flag.Parse()")
}
2015-08-29 10:51:28 +00:00
flag.StringVar(&configFile, "config", "config.json", "Path to config file")
}
2015-08-29 09:55:34 +00:00
// Manage accepts a pointer to a configuration struct.
func Manage(target interface{}) {
if ok := isStructPtr(target); !ok {
panic("Argument must be a pointer to a struct")
}
config = target
2015-08-29 11:10:56 +00:00
bootstrap()
2015-08-29 09:55:34 +00:00
}
2015-08-29 13:56:19 +00:00
// StartServer will start an HTTP server with web interface to edit config.
func StartServer(host string, port int) {
go startServer(fmt.Sprintf("%s:%d", host, port))
}
2015-08-29 11:14:53 +00:00
// HandleSIGHUP waits a SIGHUP system call and reloads configuration when
// receives one.
func HandleSIGHUP() {
ch := make(chan os.Signal, 1)
signal.Notify(ch, syscall.SIGHUP)
go func() {
for _ = range ch {
log.Println("SIGHUP received, reloading config")
readConfig()
}
}()
}
// HandleFSEvents listens to file system events and reloads configuration when
// config file is modified.
func HandleFSEvents() {
watcher, err := fsnotify.NewWatcher()
if err != nil {
panic(err)
}
if err := watcher.WatchFlags(filepath.Dir(configFile), fsnotify.FSN_MODIFY); err != nil {
panic(err)
}
fname := configFile
if ss := strings.Split(configFile, "/"); len(ss) > 1 {
fname = ss[len(ss)-1]
}
go func() {
for {
select {
case e := <-watcher.Event:
if e.Name != fname {
continue
}
if !e.IsModify() {
continue
}
log.Println("Config file was modified, reloading")
readConfig()
case err := <-watcher.Error:
log.Println("fsnotify error:", err)
}
}
}()
}
2015-08-29 17:36:27 +00:00
// OnLoad sets up a callback function that would be called once configuration
// is loaded for the first time.
func OnLoad(fun func()) {
initFunc = fun
}
2015-08-29 10:18:32 +00:00
// OnChange adds a callback function that is triggered every time a value of
// a field changes.
func OnChange(field string, fun func(oldVal, newVal interface{})) {
callbacks[field] = append(callbacks[field], fun)
}
2015-08-29 10:51:28 +00:00
func bootstrap() {
if configFile == "" {
2015-08-29 14:32:39 +00:00
log.Fatalln("path to config file is not set")
2015-08-29 10:51:28 +00:00
}
if fileExist(configFile) {
log.Println("Loading config file")
readConfig()
2015-08-29 10:51:28 +00:00
} else {
2015-08-29 14:32:39 +00:00
log.Fatalln("Config file not found")
}
}
func readConfig() {
body, err := readFile(configFile)
if err != nil {
panic(err)
}
updateConfig(body)
}
func writeConfig() {
2015-08-29 16:50:22 +00:00
if err := writeFile(configFile, marshal(config)); err != nil {
panic(err)
2015-08-29 10:51:28 +00:00
}
}
func updateConfig(body []byte) {
2015-08-29 09:59:45 +00:00
dupe := duplicate(config)
if err := json.Unmarshal(body, dupe); err != nil {
2015-08-29 14:32:39 +00:00
panic("Failed to update config")
2015-08-29 09:59:45 +00:00
return
}
2015-08-29 10:18:32 +00:00
defer triggerCallbacks(config, dupe)
// Setting new config
2015-08-29 09:59:45 +00:00
config = dupe
}
2015-08-29 16:50:22 +00:00
func marshal(obj interface{}) []byte {
body, err := json.Marshal(config)
if err != nil {
panic(err)
}
out := bytes.NewBuffer([]byte{})
// Indent with empty prefix and four spaces
if err = json.Indent(out, body, "", " "); err != nil {
panic(err)
}
// Adding a trailing newline
// It's good for your carma
out.WriteByte('\n')
return out.Bytes()
}
2015-08-29 10:18:32 +00:00
func triggerCallbacks(oldConf, newConf interface{}) {
2015-08-29 10:51:28 +00:00
// Don't trigger callbacks on fist load
if !initialized {
initialized = true
2015-08-29 17:44:48 +00:00
if initFunc != nil {
initFunc()
}
2015-08-29 10:51:28 +00:00
return
}
if len(callbacks) == 0 {
return
}
2015-08-29 12:29:56 +00:00
for fname, d := range diff(oldConf, newConf) {
if cbs, ok := callbacks[fname]; ok {
for _, cb := range cbs {
cb(d[0], d[1])
}
}
}
2015-08-29 10:18:32 +00:00
return
}
2015-08-29 09:45:00 +00:00
func isStructPtr(target interface{}) bool {
if val := reflect.ValueOf(target); val.Kind() == reflect.Ptr {
if val = reflect.Indirect(val); val.Kind() == reflect.Struct {
return true
}
}
return false
}
2015-08-29 09:55:18 +00:00
func duplicate(original interface{}) interface{} {
// Get the interface value
val := reflect.ValueOf(original)
// We expect a pointer to a struct, so now we need the underlying staruct
val = reflect.Indirect(val)
// Now we need the type (name) of this struct
typ := val.Type()
// Creating a duplicate instance of that struct
dupe := reflect.New(typ).Interface()
return dupe
}