seaweedfs/weed/util/config.go

123 lines
3.1 KiB
Go
Raw Normal View History

2015-03-10 15:20:31 +08:00
package util
import (
"strings"
2021-01-12 18:28:13 +08:00
"sync"
"github.com/spf13/viper"
"github.com/seaweedfs/seaweedfs/weed/glog"
)
var (
ConfigurationFileDirectory DirectoryValueType
2024-07-17 00:15:55 +08:00
loadSecurityConfigOnce sync.Once
)
type DirectoryValueType string
func (s *DirectoryValueType) Set(value string) error {
*s = DirectoryValueType(value)
return nil
}
func (s *DirectoryValueType) String() string {
return string(*s)
}
type Configuration interface {
GetString(key string) string
GetBool(key string) bool
GetInt(key string) int
GetStringSlice(key string) []string
SetDefault(key string, value interface{})
2012-08-24 11:56:09 +08:00
}
2024-07-17 00:15:55 +08:00
func LoadSecurityConfiguration(){
loadSecurityConfigOnce.Do(func() {
LoadConfiguration("security", false)
})
}
func LoadConfiguration(configFileName string, required bool) (loaded bool) {
// find a filer store
viper.SetConfigName(configFileName) // name of config file (without extension)
viper.AddConfigPath(ResolvePath(ConfigurationFileDirectory.String())) // path to look for the config file in
viper.AddConfigPath(".") // optionally look for config in the working directory
viper.AddConfigPath("$HOME/.seaweedfs") // call multiple times to add many search paths
viper.AddConfigPath("/usr/local/etc/seaweedfs/") // search path for bsd-style config directory in
viper.AddConfigPath("/etc/seaweedfs/") // path to look for the config file in
if err := viper.MergeInConfig(); err != nil { // Handle errors reading the config file
2020-10-24 14:58:54 +08:00
if strings.Contains(err.Error(), "Not Found") {
2021-01-20 09:21:50 +08:00
glog.V(1).Infof("Reading %s: %v", viper.ConfigFileUsed(), err)
} else {
glog.Fatalf("Reading %s: %v", viper.ConfigFileUsed(), err)
2020-10-24 14:58:54 +08:00
}
if required {
glog.Fatalf("Failed to load %s.toml file from current directory, or $HOME/.seaweedfs/, or /etc/seaweedfs/"+
"\n\nPlease use this command to generate the default %s.toml file\n"+
" weed scaffold -config=%s -output=.\n\n\n",
configFileName, configFileName, configFileName)
} else {
return false
}
}
glog.V(1).Infof("Reading %s.toml from %s", configFileName, viper.ConfigFileUsed())
return true
2019-11-11 09:15:17 +08:00
}
2021-01-12 18:28:13 +08:00
type ViperProxy struct {
*viper.Viper
sync.Mutex
}
2021-01-14 05:20:33 +08:00
var (
vp = &ViperProxy{}
)
2021-01-12 18:28:13 +08:00
func (vp *ViperProxy) SetDefault(key string, value interface{}) {
vp.Lock()
defer vp.Unlock()
vp.Viper.SetDefault(key, value)
}
2021-01-14 05:14:48 +08:00
func (vp *ViperProxy) GetString(key string) string {
vp.Lock()
defer vp.Unlock()
return vp.Viper.GetString(key)
}
func (vp *ViperProxy) GetBool(key string) bool {
vp.Lock()
defer vp.Unlock()
return vp.Viper.GetBool(key)
}
func (vp *ViperProxy) GetInt(key string) int {
vp.Lock()
defer vp.Unlock()
return vp.Viper.GetInt(key)
}
func (vp *ViperProxy) GetStringSlice(key string) []string {
vp.Lock()
defer vp.Unlock()
return vp.Viper.GetStringSlice(key)
}
2021-01-12 18:28:13 +08:00
func GetViper() *ViperProxy {
2021-01-14 05:20:33 +08:00
vp.Lock()
defer vp.Unlock()
if vp.Viper == nil {
vp.Viper = viper.GetViper()
vp.AutomaticEnv()
vp.SetEnvPrefix("weed")
vp.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
}
return vp
}