config.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package util
  2. import (
  3. "strings"
  4. "sync"
  5. "github.com/spf13/viper"
  6. "github.com/chrislusf/seaweedfs/weed/glog"
  7. )
  8. type Configuration interface {
  9. GetString(key string) string
  10. GetBool(key string) bool
  11. GetInt(key string) int
  12. GetStringSlice(key string) []string
  13. SetDefault(key string, value interface{})
  14. }
  15. func LoadConfiguration(configFileName string, required bool) (loaded bool) {
  16. // find a filer store
  17. viper.SetConfigName(configFileName) // name of config file (without extension)
  18. viper.AddConfigPath(".") // optionally look for config in the working directory
  19. viper.AddConfigPath("$HOME/.seaweedfs") // call multiple times to add many search paths
  20. viper.AddConfigPath("/usr/local/etc/seaweedfs/") // search path for bsd-style config directory in
  21. viper.AddConfigPath("/etc/seaweedfs/") // path to look for the config file in
  22. glog.V(1).Infof("Reading %s.toml from %s", configFileName, viper.ConfigFileUsed())
  23. if err := viper.MergeInConfig(); err != nil { // Handle errors reading the config file
  24. if strings.Contains(err.Error(), "Not Found") {
  25. glog.V(1).Infof("Reading %s: %v", viper.ConfigFileUsed(), err)
  26. } else {
  27. glog.Fatalf("Reading %s: %v", viper.ConfigFileUsed(), err)
  28. }
  29. if required {
  30. glog.Fatalf("Failed to load %s.toml file from current directory, or $HOME/.seaweedfs/, or /etc/seaweedfs/"+
  31. "\n\nPlease use this command to generate the default %s.toml file\n"+
  32. " weed scaffold -config=%s -output=.\n\n\n",
  33. configFileName, configFileName, configFileName)
  34. } else {
  35. return false
  36. }
  37. }
  38. return true
  39. }
  40. type ViperProxy struct {
  41. *viper.Viper
  42. sync.Mutex
  43. }
  44. var (
  45. vp = &ViperProxy{}
  46. )
  47. func (vp *ViperProxy) SetDefault(key string, value interface{}) {
  48. vp.Lock()
  49. defer vp.Unlock()
  50. vp.Viper.SetDefault(key, value)
  51. }
  52. func (vp *ViperProxy) GetString(key string) string {
  53. vp.Lock()
  54. defer vp.Unlock()
  55. return vp.Viper.GetString(key)
  56. }
  57. func (vp *ViperProxy) GetBool(key string) bool {
  58. vp.Lock()
  59. defer vp.Unlock()
  60. return vp.Viper.GetBool(key)
  61. }
  62. func (vp *ViperProxy) GetInt(key string) int {
  63. vp.Lock()
  64. defer vp.Unlock()
  65. return vp.Viper.GetInt(key)
  66. }
  67. func (vp *ViperProxy) GetStringSlice(key string) []string {
  68. vp.Lock()
  69. defer vp.Unlock()
  70. return vp.Viper.GetStringSlice(key)
  71. }
  72. func GetViper() *ViperProxy {
  73. vp.Lock()
  74. defer vp.Unlock()
  75. if vp.Viper == nil {
  76. vp.Viper = viper.GetViper()
  77. vp.AutomaticEnv()
  78. vp.SetEnvPrefix("weed")
  79. vp.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
  80. }
  81. return vp
  82. }