-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.go
More file actions
214 lines (191 loc) · 4.72 KB
/
config.go
File metadata and controls
214 lines (191 loc) · 4.72 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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package framework
import (
"fmt"
"os"
"path"
"runtime"
"strings"
"time"
"github.com/ZYallers/golib/funcs/nets"
"github.com/ZYallers/rpcx-framework/consts"
"github.com/ZYallers/rpcx-framework/types"
"github.com/spf13/viper"
)
var (
serviceMode string
serviceName string
serviceAddr string
serviceHostname string
serviceLogDir string
serviceVersion string
serviceVersionKey string
serviceErrorRobotToken string
serviceGracefulRobotToken string
serviceSqlRobotToken string
systemIP string
publicIP string
serviceDiscovery *types.Discovery
)
func ReadInConfig(args ...string) {
relativePath, configName, configType := ".", "service", "json"
argsLen := len(args)
if argsLen > 0 {
relativePath = args[0]
}
if argsLen > 1 {
configName = args[1]
}
if argsLen > 2 {
configType = args[2]
}
viper.SetConfigName(configName)
viper.SetConfigType(configType)
_, filePath, _, _ := runtime.Caller(1)
configPath := path.Join(path.Dir(filePath), relativePath)
if configPath != "" {
viper.AddConfigPath(configPath)
}
if err := viper.ReadInConfig(); err != nil {
panic(fmt.Errorf("read config file '%s/%s.%s' error: %s", configPath, configName, configType, err))
}
}
func ServiceMode() string {
if serviceMode == "" {
if modeKey := viper.GetString("global.modeKey"); modeKey == "" {
return consts.DevelopMode
} else {
if s := os.Getenv(modeKey); s == "" {
return consts.DevelopMode
} else {
serviceMode = s
}
}
}
return serviceMode
}
func ServiceName() string {
if serviceName == "" {
if s := viper.GetString("service.name"); s != "" {
serviceName = s
}
}
return serviceName
}
func ServiceAddr() string {
if serviceAddr == "" {
if s := viper.GetString("service.addr"); s != "" {
if ip := SystemIP(); ip != "" {
serviceAddr = strings.Replace(s, "0.0.0.0", ip, 1)
}
}
}
return serviceAddr
}
func ServiceHostname() string {
if serviceHostname == "" {
if s, _ := os.Hostname(); s != "" {
serviceHostname = strings.ToLower(s)
}
}
return serviceHostname
}
func ServiceLogDir() string {
if serviceLogDir == "" {
if s := viper.GetString("service.logDir"); s != "" {
serviceLogDir = s
}
}
return serviceLogDir
}
func ServiceVersion() string {
if serviceVersion == "" {
if s := viper.GetString("service.version"); s != "" {
serviceVersion = s
}
}
return serviceVersion
}
func ServiceVersionKey() string {
if serviceVersionKey == "" {
if s := viper.GetString("service.versionKey"); s != "" {
serviceVersionKey = s
}
}
return serviceVersionKey
}
func ServiceErrorRobotToken() string {
if serviceErrorRobotToken == "" {
if s := viper.GetString("service.errorRobotToken"); s != "" {
serviceErrorRobotToken = s
}
}
return serviceErrorRobotToken
}
func ServiceGracefulRobotToken() string {
if serviceGracefulRobotToken == "" {
if s := viper.GetString("service.gracefulRobotToken"); s != "" {
serviceGracefulRobotToken = s
}
}
return serviceGracefulRobotToken
}
func ServiceSqlRobotToken() string {
if serviceSqlRobotToken == "" {
if s := viper.GetString("service.sqlRobotToken"); s != "" {
serviceSqlRobotToken = s
}
}
return serviceSqlRobotToken
}
func ServiceDiscovery() *types.Discovery {
if serviceDiscovery != nil {
return serviceDiscovery
}
mode := ServiceMode()
basePath := viper.GetString(fmt.Sprintf("service.etcd.%s.basePath", mode))
if basePath == "" {
return nil
}
addr := viper.GetString(fmt.Sprintf("service.etcd.%s.addr", mode))
if len(addr) > 0 && addr[0:1] == "$" {
addr = os.Getenv(addr[1:])
}
if addr == "" {
return nil
}
if mode == consts.DevelopMode {
if hostname := ServiceHostname(); hostname != viper.GetString("global.server.development.hostname") {
if developerDockerHostname := os.Getenv("developer_docker_hostname"); developerDockerHostname != "" {
hostname = developerDockerHostname
}
basePath = strings.Replace(basePath, consts.DevelopMode, "developer@"+hostname, 1)
addr = strings.Replace(addr, "127.0.0.1", viper.GetString("global.server.development.ip"), 1)
}
}
interval := viper.GetInt64(fmt.Sprintf("service.etcd.%s.updateInterval", mode))
if interval <= 0 {
interval = 30
}
serviceDiscovery = &types.Discovery{
BasePath: basePath,
Addr: strings.Split(addr, ","),
UpdateInterval: time.Duration(interval) * time.Second,
}
return serviceDiscovery
}
func SystemIP() string {
if systemIP == "" {
if s := nets.SystemIP(); s != "" && s != "unknown" {
systemIP = s
}
}
return systemIP
}
func PublicIP() string {
if publicIP == "" {
if s := nets.PublicIP(); s != "" && s != "unknown" {
publicIP = s
}
}
return publicIP
}