-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconfig.go
executable file
·100 lines (84 loc) · 2.1 KB
/
config.go
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package hypertrace
import (
configLog "github.com/sirupsen/logrus"
"github.com/spf13/viper"
"strconv"
"strings"
)
var (
defCfg map[string]string
)
// initialize this configuration
func init() {
viper.SetEnvPrefix("trace")
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
viper.AutomaticEnv()
defCfg = make(map[string]string)
defCfg["loglevel"] = "warn" // trace,debug,info,warn,error,fatal
defCfg["adminpassword"] = "admin password is a secret"
defCfg["server.host"] = "0.0.0.0"
defCfg["server.port"] = "8080"
defCfg["database"] = "inmemory" // set to "mongodb" to use mongo
defCfg["mongo.database"] = "hypertrace"
defCfg["mongo.host"] = "localhost"
defCfg["mongo.port"] = "27017"
defCfg["mongo.user"] = "root"
defCfg["mongo.password"] = "root"
defCfg["tempid.valid.period.hour"] = "1"
defCfg["tempid.count"] = "100"
defCfg["tempid.crypt.key"] = "tH1Sis4nEncryPt10nKeydOn0tsHar3!"
for k := range defCfg {
err := viper.BindEnv(k)
if err != nil {
configLog.Errorf("Failed to bind env \"%s\" into configuration. Got %s", k, err)
}
}
}
// SetConfig put configuration key value
func SetConfig(key, value string) {
viper.Set(key, value)
}
// ConfigGet fetch configuration as string value
func ConfigGet(key string) string {
ret := viper.GetString(key)
if len(ret) == 0 {
if ret, ok := defCfg[key]; ok {
return ret
}
configLog.Debugf("%s config key not found", key)
}
return ret
}
// ConfigGetBoolean fetch configuration as boolean value
func ConfigGetBoolean(key string) bool {
if len(ConfigGet(key)) == 0 {
return false
}
b, err := strconv.ParseBool(ConfigGet(key))
if err != nil {
panic(err)
}
return b
}
// ConfigGetInt fetch configuration as integer value
func ConfigGetInt(key string) int {
if len(ConfigGet(key)) == 0 {
return 0
}
i, err := strconv.ParseInt(ConfigGet(key), 10, 64)
if err != nil {
panic(err)
}
return int(i)
}
// ConfigGetFloat fetch configuration as float value
func ConfigGetFloat(key string) float64 {
if len(ConfigGet(key)) == 0 {
return 0
}
f, err := strconv.ParseFloat(ConfigGet(key), 64)
if err != nil {
panic(err)
}
return f
}