保留可配置入口,未启用

This commit is contained in:
nuknal
2024-05-30 11:46:50 +08:00
parent 07ee4d88d4
commit e4d6b35702
11 changed files with 159 additions and 28 deletions

44
pkg/config/viper.go Normal file
View File

@@ -0,0 +1,44 @@
package config
import (
"fmt"
"strings"
"log"
"github.com/fsnotify/fsnotify"
"github.com/spf13/viper"
)
var viperInst *viper.Viper
func InitViper(cfg string) *viper.Viper {
v := viper.New()
v.AutomaticEnv()
v.SetEnvPrefix("sjy01_ip")
v.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
v.SetConfigFile(cfg)
v.SetConfigType("yaml")
err := v.ReadInConfig()
if err != nil {
log.Println(fmt.Errorf("fatal error config file: %s", err.Error()))
return nil
}
v.WatchConfig()
v.OnConfigChange(func(e fsnotify.Event) {
if err := v.Unmarshal(&GCONFIG); err != nil {
fmt.Println(err)
}
log.Println("config file changed", e.Name, err)
})
if err := v.Unmarshal(&GCONFIG); err != nil {
fmt.Println(err)
}
return v
}