-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
133 lines (105 loc) · 2.11 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package config
import (
"fmt"
"github.com/litsoftware/litmedia/pkg/jsonh"
"github.com/litsoftware/litmedia/pkg/path"
_ "github.com/litsoftware/litmedia/pkg/runtime"
"github.com/spf13/viper"
"os"
"path/filepath"
"strings"
)
var (
err error
v *viper.Viper
)
func init() {
v = viper.New()
v.SetConfigType("yaml")
v.SetConfigName("app")
p := fmt.Sprintf("%s/%s", path.RootPath(), "configs")
v.AddConfigPath(p)
initForTestMode()
initForCiTestMode()
findConfigFiles()
err = v.ReadInConfig()
if err != nil {
fmt.Printf("parse configuration file err %#v\n", err)
os.Exit(1)
}
if v.GetBool("watch") {
v.WatchConfig()
}
check()
}
func findConfigFiles() {
pwd := os.Getenv("PWD")
for i := 0; i < 10; i = i + 1 {
if strings.HasSuffix(pwd, "media-service") {
v.AddConfigPath(fmt.Sprintf("%s/%s", pwd, "configs"))
break
}
pwd = filepath.Dir(pwd)
}
}
func initForTestMode() {
if os.Getenv("TESTING_MODE") == "1" {
v.SetConfigName("app_test")
}
}
func initForCiTestMode() {
if os.Getenv("CI_TESTING_MODE") == "1" {
v.SetConfigName("app_test_ci")
}
}
func SetDefault(key string, value interface{}) {
v.SetDefault(key, value)
}
func GetNormal(key string) interface{} {
return v.Get(key)
}
func GetBool(key string) bool {
return v.GetBool(key)
}
func GetString(key string) string {
return v.GetString(key)
}
func GetStringDefault(key string, d string) string {
s := v.GetString(key)
if s == "" {
return d
}
return s
}
func GetStringSlice(key string) []string {
return v.GetStringSlice(key)
}
func GetInt(key string) int {
return v.GetInt(key)
}
func GetFloat64(key string) float64 {
return v.GetFloat64(key)
}
func GetInt64(key string) int64 {
return v.GetInt64(key)
}
func GetInt32(key string) int32 {
return v.GetInt32(key)
}
func GetMap(key string) map[string]interface{} {
return v.GetStringMap(key)
}
func GetStruct(key string, obj interface{}) {
m := v.GetStringMap(key)
jsonh.ConvertTo(m, obj)
}
func GetEnv(key, defaultValue string) string {
value := os.Getenv(key)
if value == "" {
return defaultValue
}
return value
}
func GetConfig() *viper.Viper {
return v
}