本文整理汇总了Golang中github.com/spf13/viper.OnConfigChange函数的典型用法代码示例。如果您正苦于以下问题:Golang OnConfigChange函数的具体用法?Golang OnConfigChange怎么用?Golang OnConfigChange使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了OnConfigChange函数的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: initConfig
func initConfig() {
// defaults
viper.SetDefault("adminroot", "/admin/") // front end location of admin
viper.SetDefault("admindir", "./admin") // location of admin assets (relative to site directory)
viper.SetDefault("contentdir", "content")
// config name and location
viper.SetConfigName("config")
viper.AddConfigPath("/etc/topiary")
viper.AddConfigPath(".")
// read config
err := viper.ReadInConfig()
if err != nil {
fmt.Println("No topiary config found. Using defaults.")
}
// watch config ; TODO : config to turn this on/off
viper.WatchConfig()
viper.OnConfigChange(func(e fsnotify.Event) {
fmt.Println("Config file changed:", e.Name)
})
}
示例2: init
func init() {
viper.SetConfigName("conf")
viper.AddConfigPath(".")
viper.WatchConfig()
viper.ReadInConfig()
viper.Unmarshal(&conf)
viper.OnConfigChange(func(e fsnotify.Event) {
if err := viper.Unmarshal(&conf); err != nil {
log.Println(err.Error())
} else {
log.Println("config auto reload!")
}
})
r = redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: conf.RedisPass,
})
scheduler.Every().Day().At("00:00").Run(func() {
if time.Now().Day() == 1 {
length := r.ZCard("Shan8Bot:K").Val()
if length < 25 {
return
}
i := rand.Int63n(length-25) + 25
for _, v := range r.ZRangeWithScores("Shan8Bot:K", i, i).Val() {
log.Println("add 100K for ", v.Member)
r.ZIncrBy("Shan8Bot:K", 100, v.Member.(string))
}
}
})
}
示例3: watchConfig
func watchConfig() {
viper.WatchConfig()
viper.OnConfigChange(func(e fsnotify.Event) {
fmt.Println("Config file changed:", e.Name)
utils.CheckErr(buildSite(true))
if !viper.GetBool("DisableLiveReload") {
// Will block forever trying to write to a channel that nobody is reading if livereload isn't initialized
livereload.ForceRefresh()
}
})
}
示例4: SetupNotificationConfig
func (c *Conf) SetupNotificationConfig(filename string, config NotificationServiceConfig) {
viperConfig := viper.New()
c.setupHome(viperConfig, viper.GetString("Home"))
viperConfig.SetConfigName(filename)
if err := viperConfig.ReadInConfig(); err != nil {
log.Printf("Cannot read notification config: %s", err)
}
viperConfig.Unmarshal(config)
viper.OnConfigChange(func(in fsnotify.Event) { viperConfig.ReadInConfig(); viperConfig.Unmarshal(config) })
viper.WatchConfig()
c.notificationConfigs = append(c.notificationConfigs, config)
}
示例5: startup
func startup() error {
var config lib.Config
if err := viper.Unmarshal(&config); err != nil {
return err
}
serverAndPort := fmt.Sprintf("%s:%d", serverInterface, serverPort)
if config.Feed.Link == "" {
config.Feed.Link = "http://" + serverAndPort
}
if config.Feed.MaxItems <= 0 {
config.Feed.MaxItems = 10
}
// enable live reloading of config
viper.OnConfigChange(func(e fsnotify.Event) {
fmt.Println("Config", e.Name, "changed ...")
if err := viper.Unmarshal(&config); err != nil {
fmt.Println("error: Failed to reload config: ", err)
return
}
shutdownIfNeededAndStart(config)
})
viper.WatchConfig()
shutdownIfNeededAndStart(config)
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "application/rss+xml; charset=utf-8")
fmt.Fprintf(w, app().GetFeed())
})
fmt.Printf("\nStarting server on http://%s ...\n\n", serverAndPort)
graceful.Run(serverAndPort, 10*time.Second, mux)
app().Shutdown()
fmt.Println("\nStopped ...")
return nil
}
示例6: initByConfigFile
func initByConfigFile() {
log.Println("init params by configfile..")
viper.SetConfigName("gate-conf")
viper.SetConfigType("yaml")
viper.AddConfigPath(".") // optionally look for config in the working directory
viper.AddConfigPath("$HOME/.seckilling/")
viper.AddConfigPath("/etc/seckilling/")
err := viper.ReadInConfig() // Find and read the config file
if err != nil { // Handle errors reading the config file
log.Panicf("Fatal error config file: %s \n", err)
}
if viper.GetBool("watch") {
viper.WatchConfig()
}
viper.OnConfigChange(func(e fsnotify.Event) {
log.Println("Config file changed:", e.Name)
})
log.Printf("loading config %s \n", viper.ConfigFileUsed())
}
示例7: initConfig
func initConfig() {
// defaults
viper.SetDefault("AdminLocation", "/admin/") // TODO : some helper to convert strings to paths ? ie: path("admin") -> "/admin/"
viper.SetDefault("contentdir", "content")
// config name and location
viper.SetConfigName("config")
viper.AddConfigPath("/etc/topiary")
viper.AddConfigPath(".")
// read config
err := viper.ReadInConfig()
if err != nil {
fmt.Println("No topiary config found. Using defaults.")
}
// watch config ; TODO : config to turn this on/off
viper.WatchConfig()
viper.OnConfigChange(func(e fsnotify.Event) {
fmt.Println("Config file changed:", e.Name)
})
}
示例8: EnableAutoUpdate
func (c *Conf) EnableAutoUpdate() {
viper.OnConfigChange(func(in fsnotify.Event) { c.Update() })
viper.WatchConfig()
}