-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathconfig.go
More file actions
82 lines (69 loc) · 1.73 KB
/
Copy pathconfig.go
File metadata and controls
82 lines (69 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package goconfig
import (
"fmt"
"github.com/newrelic/go-agent"
"github.com/spf13/viper"
)
type Config interface {
GetValue(string) string
GetIntValue(string) int
}
type configuration map[string]interface{}
var config configuration
type BaseConfig struct {
}
func (self BaseConfig) Load() {
viper.SetDefault("port", "3000")
viper.SetDefault("log_level", "warn")
viper.SetDefault("redis_password", "")
viper.AutomaticEnv()
viper.SetConfigName("application")
viper.AddConfigPath("./")
viper.AddConfigPath("../")
viper.SetConfigType("yaml")
viper.ReadInConfig()
config = configuration{}
config["newrelic"] = getNewRelicConfigOrPanic()
}
func (self BaseConfig) Newrelic() newrelic.Config {
return config["newrelic"].(newrelic.Config)
}
func (self BaseConfig) GetValue(key string) string {
if _, ok := config[key]; !ok {
config[key] = getStringOrPanic(key)
}
return config[key].(string)
}
func (self BaseConfig) GetOptionalValue(key string, defaultValue string) string {
fmt.Println(config)
if _, ok := config[key]; !ok {
var value string
if value = viper.GetString(key); !viper.IsSet(key) {
value = defaultValue
}
config[key] = value
}
return config[key].(string)
}
func (self BaseConfig) GetIntValue(key string) int {
if _, ok := config[key]; !ok {
config[key] = getIntOrPanic(key)
}
return config[key].(int)
}
func (self BaseConfig) GetOptionalIntValue(key string, defaultValue int) int {
if _, ok := config[key]; !ok {
var value int
if value = viper.GetInt(key); !viper.IsSet(key) {
value = defaultValue
}
config[key] = value
}
return config[key].(int)
}
func (self BaseConfig) GetFeature(key string) bool {
if _, ok := config[key]; !ok {
config[key] = getFeature(key)
}
return config[key].(bool)
}