当前位置: 首页>>代码示例>>Golang>>正文


Golang viper.OnConfigChange函数代码示例

本文整理汇总了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)
	})
}
开发者ID:topiary-io,项目名称:topiary,代码行数:25,代码来源:config.go

示例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))
			}
		}
	})
}
开发者ID:jqs7,项目名称:Shan8Bot,代码行数:33,代码来源:init.go

示例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()
		}
	})
}
开发者ID:ridelore,项目名称:hugo,代码行数:11,代码来源:hugo.go

示例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)
}
开发者ID:RobinUS2,项目名称:indispenso,代码行数:16,代码来源:conf.go

示例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
}
开发者ID:bep,项目名称:alfn,代码行数:47,代码来源:root.go

示例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())
}
开发者ID:Dataman-Cloud,项目名称:seckilling,代码行数:22,代码来源:configuration.go

示例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)
	})
}
开发者ID:doesntgolf,项目名称:topiary,代码行数:24,代码来源:config.go

示例8: EnableAutoUpdate

func (c *Conf) EnableAutoUpdate() {
	viper.OnConfigChange(func(in fsnotify.Event) { c.Update() })
	viper.WatchConfig()
}
开发者ID:RobinUS2,项目名称:indispenso,代码行数:4,代码来源:conf.go


注:本文中的github.com/spf13/viper.OnConfigChange函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。