config.go 819 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. package main
  2. import (
  3. "fmt"
  4. "github.com/spf13/viper"
  5. "gopkg.in/fsnotify.v1"
  6. )
  7. func initConfig() {
  8. // defaults
  9. viper.SetDefault("adminroot", "/admin/") // front end location of admin
  10. viper.SetDefault("admindir", "./admin") // location of admin assets (relative to site directory)
  11. viper.SetDefault("contentdir", "content")
  12. // config name and location
  13. viper.SetConfigName("config")
  14. viper.AddConfigPath("/etc/topiary")
  15. viper.AddConfigPath(".")
  16. // read config
  17. err := viper.ReadInConfig()
  18. if err != nil {
  19. fmt.Println("No topiary config found. Using defaults.")
  20. }
  21. // watch config ; TODO : config to turn this on/off
  22. viper.WatchConfig()
  23. viper.OnConfigChange(func(e fsnotify.Event) {
  24. fmt.Println("Config file changed:", e.Name)
  25. })
  26. }
  27. func getConfig(s string) string {
  28. return viper.GetString(s)
  29. }