保留可配置入口,未启用
This commit is contained in:
44
pkg/config/viper.go
Normal file
44
pkg/config/viper.go
Normal 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
|
||||
}
|
||||
Reference in New Issue
Block a user