Set openTSDB defaults in service

pull/2900/head
Philip O'Toole 2015-06-10 20:41:32 -07:00
parent 3273db25f2
commit e294db3c49
2 changed files with 26 additions and 4 deletions

View File

@ -30,3 +30,22 @@ func NewConfig() Config {
ConsistencyLevel: DefaultConsistencyLevel,
}
}
// WithDefaults takes the given config and returns a new config with any required
// default values set.
func (c *Config) WithDefaults() *Config {
d := *c
if d.BindAddress == "" {
d.BindAddress = DefaultBindAddress
}
if d.Database == "" {
d.Database = DefaultDatabase
}
if d.RetentionPolicy == "" {
d.RetentionPolicy = DefaultRetentionPolicy
}
if d.ConsistencyLevel == "" {
d.ConsistencyLevel = DefaultConsistencyLevel
}
return &d
}

View File

@ -44,16 +44,19 @@ type Service struct {
// NewService returns a new instance of Service.
func NewService(c Config) (*Service, error) {
consistencyLevel, err := cluster.ParseConsistencyLevel(c.ConsistencyLevel)
// Use defaults where necessary.
d := c.WithDefaults()
consistencyLevel, err := cluster.ParseConsistencyLevel(d.ConsistencyLevel)
if err != nil {
return nil, err
}
s := &Service{
err: make(chan error),
BindAddress: c.BindAddress,
Database: c.Database,
RetentionPolicy: c.RetentionPolicy,
BindAddress: d.BindAddress,
Database: d.Database,
RetentionPolicy: d.RetentionPolicy,
ConsistencyLevel: consistencyLevel,
Logger: log.New(os.Stderr, "[opentsdb] ", log.LstdFlags),
}