config.go 3.1 KB

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