Hot reload server config

This commit is contained in:
Ken-Håvard Lieng 2018-04-15 00:57:11 +02:00
parent 60190fbd98
commit d08bd43ba0

View File

@ -5,7 +5,9 @@ import (
"io/ioutil"
"log"
"os"
"time"
"github.com/fsnotify/fsnotify"
"github.com/spf13/cobra"
"github.com/spf13/viper"
@ -39,6 +41,18 @@ var rootCmd = &cobra.Command{
viper.SetConfigName("config")
viper.AddConfigPath(storage.Path.Root())
viper.ReadInConfig()
viper.WatchConfig()
prev := time.Now()
viper.OnConfigChange(func(e fsnotify.Event) {
now := time.Now()
// fsnotify sometimes fires twice
if now.Sub(prev) > time.Second {
log.Println("New config loaded")
prev = now
}
})
},
Run: func(cmd *cobra.Command, args []string) {