From d08bd43ba0a38a772e11957dc44bcdc916f55aef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ken-H=C3=A5vard=20Lieng?= Date: Sun, 15 Apr 2018 00:57:11 +0200 Subject: [PATCH] Hot reload server config --- commands/dispatch.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/commands/dispatch.go b/commands/dispatch.go index 5baf1900..d78de678 100644 --- a/commands/dispatch.go +++ b/commands/dispatch.go @@ -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) {