qnc-server-old/core/viper.go
2024-09-14 10:48:09 +08:00

29 lines
590 B
Go

package core
import (
"fmt"
"github.com/fsnotify/fsnotify"
"github.com/spf13/viper"
"qnc-server/config"
)
func Viper() {
v := viper.New()
v.SetConfigName("config")
v.SetConfigType("yaml")
v.AddConfigPath(".")
if err := v.ReadInConfig(); err != nil {
panic(fmt.Errorf("fatal error config file: %w", err))
}
v.WatchConfig()
v.OnConfigChange(func(e fsnotify.Event) {
if err := v.Unmarshal(&config.ConfigData); err != nil {
fmt.Println(err)
}
})
if err := v.Unmarshal(&config.ConfigData); err != nil {
panic(fmt.Errorf("unable to decode into struct: %w", err))
}
}