feat: watch config reloading

This commit is contained in:
Muyao CHEN 2024-10-01 21:58:57 +02:00
parent 3d1d40b4fc
commit 7e876324a2
2 changed files with 8 additions and 1 deletions

2
go.mod
View File

@ -3,6 +3,7 @@ module git.vinchent.xyz/vinchent/howmuch
go 1.23.1
require (
github.com/fsnotify/fsnotify v1.7.0
github.com/gin-gonic/gin v1.10.0
github.com/spf13/cobra v1.8.1
github.com/spf13/viper v1.19.0
@ -14,7 +15,6 @@ require (
github.com/bytedance/sonic/loader v0.1.1 // indirect
github.com/cloudwego/base64x v0.1.4 // indirect
github.com/cloudwego/iasm v0.2.0 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-playground/locales v0.14.1 // indirect

View File

@ -27,6 +27,7 @@ import (
"fmt"
"net/http"
"github.com/fsnotify/fsnotify"
"github.com/gin-gonic/gin"
"github.com/spf13/cobra"
"github.com/spf13/viper"
@ -79,6 +80,12 @@ func run() error {
settings, _ := json.MarshalIndent(viper.AllSettings(), "", " ")
// watching reloading conf
viper.OnConfigChange(func(e fsnotify.Event) {
fmt.Println("Config file changed:", e.Name)
})
viper.WatchConfig()
fmt.Println(string(settings))
fmt.Println(viper.GetString("db.username"))