mirror of
https://github.com/featurebasedb/featurebase.git
synced 2026-08-28 10:54:59 +00:00
More config changes.
This commit is contained in:
parent
05db8dcb7b
commit
43f87b9b57
2 changed files with 60 additions and 16 deletions
|
|
@ -11,23 +11,16 @@ import (
|
|||
)
|
||||
|
||||
type Config struct {
|
||||
config map[string]interface{}
|
||||
lock sync.RWMutex
|
||||
loaded bool
|
||||
config map[string]interface{}
|
||||
lock sync.RWMutex
|
||||
filename string
|
||||
loaded bool
|
||||
}
|
||||
|
||||
var config *Config
|
||||
|
||||
func init() {
|
||||
config_file := os.Getenv("PILOSA_CONFIG")
|
||||
if config_file == "" {
|
||||
config_file = "pilosa.yaml"
|
||||
}
|
||||
var err error
|
||||
config, err = NewConfig(config_file)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
config = NewConfig("")
|
||||
}
|
||||
|
||||
func GetSafe(key string) (interface{}, bool) {
|
||||
|
|
@ -46,18 +39,38 @@ func GetString(key string) string {
|
|||
return config.GetString(key)
|
||||
}
|
||||
|
||||
func NewConfig(filename string) (*Config, error) {
|
||||
func NewConfig(filename string) *Config {
|
||||
self := Config{}
|
||||
self.config = make(map[string]interface{})
|
||||
data, err := ioutil.ReadFile(filename)
|
||||
self.filename = filename
|
||||
return &self
|
||||
}
|
||||
|
||||
func (self *Config) load() error {
|
||||
self.lock.Lock()
|
||||
defer self.lock.Unlock()
|
||||
if self.loaded {
|
||||
return nil
|
||||
}
|
||||
config_file := self.filename
|
||||
if config_file == "" {
|
||||
config_file = os.Getenv("PILOSA_CONFIG")
|
||||
if config_file == "" {
|
||||
log.Println("PILOSA_CONFIG not set, defaulting to pilosa.yaml")
|
||||
config_file = "pilosa.yaml"
|
||||
}
|
||||
}
|
||||
data, err := ioutil.ReadFile(config_file)
|
||||
if err != nil {
|
||||
return nil, errors.New("Problem with config file: " + err.Error())
|
||||
return errors.New("Problem with config file: " + err.Error())
|
||||
}
|
||||
goyaml.Unmarshal(data, self.config)
|
||||
return &self, nil
|
||||
self.loaded = true
|
||||
return nil
|
||||
}
|
||||
|
||||
func (self *Config) GetSafe(key string) (interface{}, bool) {
|
||||
self.load()
|
||||
self.lock.RLock()
|
||||
defer self.lock.RUnlock()
|
||||
value, ok := self.config[key]
|
||||
|
|
@ -65,6 +78,7 @@ func (self *Config) GetSafe(key string) (interface{}, bool) {
|
|||
}
|
||||
|
||||
func (self *Config) Get(key string) interface{} {
|
||||
self.load()
|
||||
self.lock.RLock()
|
||||
defer self.lock.RUnlock()
|
||||
return self.config[key]
|
||||
|
|
|
|||
|
|
@ -34,3 +34,33 @@ func TestConfig(t *testing.T) {
|
|||
So(GetString("temp"), ShouldEqual, "/tmp")
|
||||
})
|
||||
}
|
||||
|
||||
func TestConfigObject(t *testing.T) {
|
||||
err := os.Setenv("PILOSA_CONFIG", "")
|
||||
if err != nil {
|
||||
t.Fatal("Error setting PILOSA_CONFIG")
|
||||
}
|
||||
conf := NewConfig("test.yaml")
|
||||
Convey("config.Get()", t, func() {
|
||||
So(conf.Get("port_tcp"), ShouldEqual, 12000)
|
||||
So(conf.Get("port_http"), ShouldEqual, 15000)
|
||||
So(conf.Get("temp"), ShouldEqual, "/tmp")
|
||||
So(conf.Get("notfound"), ShouldBeNil)
|
||||
})
|
||||
Convey("config.GetSafe()", t, func() {
|
||||
val, ok := conf.GetSafe("port_tcp")
|
||||
So(ok, ShouldBeTrue)
|
||||
So(val, ShouldEqual, 12000)
|
||||
val, ok = conf.GetSafe("derp")
|
||||
So(ok, ShouldBeFalse)
|
||||
So(val, ShouldBeNil)
|
||||
})
|
||||
Convey("config.GetInt()", t, func() {
|
||||
So(conf.GetInt("port_tcp"), ShouldEqual, 12000)
|
||||
So(conf.GetInt("port_http"), ShouldEqual, 15000)
|
||||
So(conf.GetInt("notfound"), ShouldEqual, 0)
|
||||
})
|
||||
Convey("config.GetString()", t, func() {
|
||||
So(conf.GetString("temp"), ShouldEqual, "/tmp")
|
||||
})
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue