🎨 增加 Go 后端

This commit is contained in:
江西小徐
2024-04-07 11:08:05 +08:00
parent 04c82ab25d
commit 61275be2bf
165 changed files with 7043 additions and 3599 deletions
+44
View File
@@ -0,0 +1,44 @@
package config
import (
"flag"
"fmt"
"github.com/fsnotify/fsnotify"
"github.com/spf13/viper"
"os"
"whois-go/internal/global"
)
func NewConfig() *viper.Viper {
envConf := os.Getenv("APP_CONF")
if envConf == "" {
flag.StringVar(&envConf, "conf", "config/local.yml", "config path, eg: -conf config/local.yml")
flag.Parse()
}
if envConf == "" {
envConf = "config/local.yml"
}
fmt.Println("load conf file:", envConf)
return getConfig(envConf)
}
func getConfig(path string) *viper.Viper {
conf := viper.New()
conf.SetConfigFile(path)
err := conf.ReadInConfig()
if err != nil {
panic(err)
}
conf.WatchConfig()
conf.OnConfigChange(func(e fsnotify.Event) {
fmt.Println("config file changed:", e.Name)
if err = conf.Unmarshal(&global.G_CONFIG); err != nil {
fmt.Println(err)
}
})
if err = conf.Unmarshal(&global.G_CONFIG); err != nil {
panic(err)
}
return conf
}