forked from appleboy/gorush
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
435 lines (385 loc) · 14 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
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
package config
import (
"bytes"
"fmt"
"io/ioutil"
"runtime"
"strings"
"github.com/spf13/viper"
)
var defaultConf = []byte(`
core:
enabled: true # enable httpd server
address: "" # ip address to bind (default: any)
shutdown_timeout: 30 # default is 30 second
port: "8088" # ignore this port number if auto_tls is enabled (listen 443).
worker_num: 0 # default worker number is runtime.NumCPU()
queue_num: 0 # default queue number is 8192
max_notification: 100
sync: false # set true if you need get error message from fail push notification in API response.
feedback_hook_url: "" # set webhook url if you need get error message asynchronously from fail push notification in API response.
feedback_timeout: 10 # default is 10 second
mode: "release"
ssl: false
cert_path: "cert.pem"
key_path: "key.pem"
cert_base64: ""
key_base64: ""
http_proxy: ""
pid:
enabled: false
path: "gorush.pid"
override: true
auto_tls:
enabled: false # Automatically install TLS certificates from Let's Encrypt.
folder: ".cache" # folder for storing TLS certificates
host: "" # which domains the Let's Encrypt will attempt
grpc:
enabled: false # enable gRPC server
port: 9000
api:
push_uri: "/api/push"
stat_go_uri: "/api/stat/go"
stat_app_uri: "/api/stat/app"
config_uri: "/api/config"
sys_stat_uri: "/sys/stats"
metric_uri: "/metrics"
health_uri: "/healthz"
android:
enabled: true
apikey: "YOUR_API_KEY"
max_retry: 0 # resend fail notification, default value zero is disabled
huawei:
enabled: false
appsecret: "YOUR_APP_SECRET"
appid: "YOUR_APP_ID"
max_retry: 0 # resend fail notification, default value zero is disabled
queue:
engine: "local" # support "local", "nsq", "nats" and "redis" default value is "local"
nsq:
addr: 127.0.0.1:4150
topic: gorush
channel: gorush
nats:
addr: 127.0.0.1:4222
subj: gorush
queue: gorush
redis:
addr: 127.0.0.1:6379
channel: gorush
size: 1024
ios:
enabled: false
key_path: ""
key_base64: "" # load iOS key from base64 input
key_type: "pem" # could be pem, p12 or p8 type
password: "" # certificate password, default as empty string.
production: false
max_concurrent_pushes: 100 # just for push ios notification
max_retry: 0 # resend fail notification, default value zero is disabled
key_id: "" # KeyID from developer account (Certificates, Identifiers & Profiles -> Keys)
team_id: "" # TeamID from developer account (View Account -> Membership)
log:
format: "string" # string or json
access_log: "stdout" # stdout: output to console, or define log path like "log/access_log"
access_level: "debug"
error_log: "stderr" # stderr: output to console, or define log path like "log/error_log"
error_level: "error"
hide_token: true
stat:
engine: "memory" # support memory, redis, boltdb, buntdb or leveldb
redis:
cluster: false
addr: "localhost:6379" # if cluster is true, you may set this to "localhost:6379,localhost:6380,localhost:6381"
password: ""
db: 0
boltdb:
path: "bolt.db"
bucket: "gorush"
buntdb:
path: "bunt.db"
leveldb:
path: "level.db"
badgerdb:
path: "badger.db"
`)
// ConfYaml is config structure.
type ConfYaml struct {
Core SectionCore `yaml:"core"`
API SectionAPI `yaml:"api"`
Android SectionAndroid `yaml:"android"`
Huawei SectionHuawei `yaml:"huawei"`
Ios SectionIos `yaml:"ios"`
Queue SectionQueue `yaml:"queue"`
Log SectionLog `yaml:"log"`
Stat SectionStat `yaml:"stat"`
GRPC SectionGRPC `yaml:"grpc"`
}
// SectionCore is sub section of config.
type SectionCore struct {
Enabled bool `yaml:"enabled"`
Address string `yaml:"address"`
ShutdownTimeout int64 `yaml:"shutdown_timeout"`
Port string `yaml:"port"`
MaxNotification int64 `yaml:"max_notification"`
WorkerNum int64 `yaml:"worker_num"`
QueueNum int64 `yaml:"queue_num"`
Mode string `yaml:"mode"`
Sync bool `yaml:"sync"`
SSL bool `yaml:"ssl"`
CertPath string `yaml:"cert_path"`
KeyPath string `yaml:"key_path"`
CertBase64 string `yaml:"cert_base64"`
KeyBase64 string `yaml:"key_base64"`
HTTPProxy string `yaml:"http_proxy"`
FeedbackURL string `yaml:"feedback_hook_url"`
FeedbackTimeout int64 `yaml:"feedback_timeout"`
PID SectionPID `yaml:"pid"`
AutoTLS SectionAutoTLS `yaml:"auto_tls"`
}
// SectionAutoTLS support Let's Encrypt setting.
type SectionAutoTLS struct {
Enabled bool `yaml:"enabled"`
Folder string `yaml:"folder"`
Host string `yaml:"host"`
}
// SectionAPI is sub section of config.
type SectionAPI struct {
PushURI string `yaml:"push_uri"`
StatGoURI string `yaml:"stat_go_uri"`
StatAppURI string `yaml:"stat_app_uri"`
ConfigURI string `yaml:"config_uri"`
SysStatURI string `yaml:"sys_stat_uri"`
MetricURI string `yaml:"metric_uri"`
HealthURI string `yaml:"health_uri"`
}
// SectionAndroid is sub section of config.
type SectionAndroid struct {
Enabled bool `yaml:"enabled"`
APIKey string `yaml:"apikey"`
MaxRetry int `yaml:"max_retry"`
}
// SectionHuawei is sub section of config.
type SectionHuawei struct {
Enabled bool `yaml:"enabled"`
AppSecret string `yaml:"appsecret"`
AppID string `yaml:"appid"`
MaxRetry int `yaml:"max_retry"`
}
// SectionIos is sub section of config.
type SectionIos struct {
Enabled bool `yaml:"enabled"`
KeyPath string `yaml:"key_path"`
KeyBase64 string `yaml:"key_base64"`
KeyType string `yaml:"key_type"`
Password string `yaml:"password"`
Production bool `yaml:"production"`
MaxConcurrentPushes uint `yaml:"max_concurrent_pushes"`
MaxRetry int `yaml:"max_retry"`
KeyID string `yaml:"key_id"`
TeamID string `yaml:"team_id"`
}
// SectionLog is sub section of config.
type SectionLog struct {
Format string `yaml:"format"`
AccessLog string `yaml:"access_log"`
AccessLevel string `yaml:"access_level"`
ErrorLog string `yaml:"error_log"`
ErrorLevel string `yaml:"error_level"`
HideToken bool `yaml:"hide_token"`
}
// SectionStat is sub section of config.
type SectionStat struct {
Engine string `yaml:"engine"`
Redis SectionRedis `yaml:"redis"`
BoltDB SectionBoltDB `yaml:"boltdb"`
BuntDB SectionBuntDB `yaml:"buntdb"`
LevelDB SectionLevelDB `yaml:"leveldb"`
BadgerDB SectionBadgerDB `yaml:"badgerdb"`
}
// SectionQueue is sub section of config.
type SectionQueue struct {
Engine string `yaml:"engine"`
NSQ SectionNSQ `yaml:"nsq"`
NATS SectionNATS `yaml:"nats"`
Redis SectionRedisQueue `yaml:"redis"`
}
// SectionNSQ is sub section of config.
type SectionNSQ struct {
Addr string `yaml:"addr"`
Topic string `yaml:"topic"`
Channel string `yaml:"channel"`
}
// SectionNATS is sub section of config.
type SectionNATS struct {
Addr string `yaml:"addr"`
Subj string `yaml:"subj"`
Queue string `yaml:"queue"`
}
// SectionRedisQueue is sub section of config.
type SectionRedisQueue struct {
Addr string `yaml:"addr"`
Channel string `yaml:"channel"`
Size int `yaml:"size"`
}
// SectionRedis is sub section of config.
type SectionRedis struct {
Cluster bool `yaml:"cluster"`
Addr string `yaml:"addr"`
Password string `yaml:"password"`
DB int `yaml:"db"`
}
// SectionBoltDB is sub section of config.
type SectionBoltDB struct {
Path string `yaml:"path"`
Bucket string `yaml:"bucket"`
}
// SectionBuntDB is sub section of config.
type SectionBuntDB struct {
Path string `yaml:"path"`
}
// SectionLevelDB is sub section of config.
type SectionLevelDB struct {
Path string `yaml:"path"`
}
// SectionBadgerDB is sub section of config.
type SectionBadgerDB struct {
Path string `yaml:"path"`
}
// SectionPID is sub section of config.
type SectionPID struct {
Enabled bool `yaml:"enabled"`
Path string `yaml:"path"`
Override bool `yaml:"override"`
}
// SectionGRPC is sub section of config.
type SectionGRPC struct {
Enabled bool `yaml:"enabled"`
Port string `yaml:"port"`
}
func setDefault() {
viper.SetDefault("ios.max_concurrent_pushes", uint(100))
}
// LoadConf load config from file and read in environment variables that match
func LoadConf(confPath ...string) (*ConfYaml, error) {
conf := &ConfYaml{}
// load default values
setDefault()
viper.SetConfigType("yaml")
viper.AutomaticEnv() // read in environment variables that match
viper.SetEnvPrefix("gorush") // will be uppercased automatically
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
if len(confPath) > 0 && confPath[0] != "" {
content, err := ioutil.ReadFile(confPath[0])
if err != nil {
return conf, err
}
if err := viper.ReadConfig(bytes.NewBuffer(content)); err != nil {
return conf, err
}
} else {
// Search config in home directory with name ".gorush" (without extension).
viper.AddConfigPath("/etc/gorush/")
viper.AddConfigPath("$HOME/.gorush")
viper.AddConfigPath(".")
viper.SetConfigName("config")
// If a config file is found, read it in.
if err := viper.ReadInConfig(); err == nil {
fmt.Println("Using config file:", viper.ConfigFileUsed())
} else if err := viper.ReadConfig(bytes.NewBuffer(defaultConf)); err != nil {
// load default config
return conf, err
}
}
// Core
conf.Core.Address = viper.GetString("core.address")
conf.Core.Port = viper.GetString("core.port")
conf.Core.ShutdownTimeout = int64(viper.GetInt("core.shutdown_timeout"))
conf.Core.Enabled = viper.GetBool("core.enabled")
conf.Core.WorkerNum = int64(viper.GetInt("core.worker_num"))
conf.Core.QueueNum = int64(viper.GetInt("core.queue_num"))
conf.Core.Mode = viper.GetString("core.mode")
conf.Core.Sync = viper.GetBool("core.sync")
conf.Core.FeedbackURL = viper.GetString("core.feedback_hook_url")
conf.Core.FeedbackTimeout = int64(viper.GetInt("core.feedback_timeout"))
conf.Core.SSL = viper.GetBool("core.ssl")
conf.Core.CertPath = viper.GetString("core.cert_path")
conf.Core.KeyPath = viper.GetString("core.key_path")
conf.Core.CertBase64 = viper.GetString("core.cert_base64")
conf.Core.KeyBase64 = viper.GetString("core.key_base64")
conf.Core.MaxNotification = int64(viper.GetInt("core.max_notification"))
conf.Core.HTTPProxy = viper.GetString("core.http_proxy")
conf.Core.PID.Enabled = viper.GetBool("core.pid.enabled")
conf.Core.PID.Path = viper.GetString("core.pid.path")
conf.Core.PID.Override = viper.GetBool("core.pid.override")
conf.Core.AutoTLS.Enabled = viper.GetBool("core.auto_tls.enabled")
conf.Core.AutoTLS.Folder = viper.GetString("core.auto_tls.folder")
conf.Core.AutoTLS.Host = viper.GetString("core.auto_tls.host")
// Api
conf.API.PushURI = viper.GetString("api.push_uri")
conf.API.StatGoURI = viper.GetString("api.stat_go_uri")
conf.API.StatAppURI = viper.GetString("api.stat_app_uri")
conf.API.ConfigURI = viper.GetString("api.config_uri")
conf.API.SysStatURI = viper.GetString("api.sys_stat_uri")
conf.API.MetricURI = viper.GetString("api.metric_uri")
conf.API.HealthURI = viper.GetString("api.health_uri")
// Android
conf.Android.Enabled = viper.GetBool("android.enabled")
conf.Android.APIKey = viper.GetString("android.apikey")
conf.Android.MaxRetry = viper.GetInt("android.max_retry")
// Huawei
conf.Huawei.Enabled = viper.GetBool("huawei.enabled")
conf.Huawei.AppSecret = viper.GetString("huawei.appsecret")
conf.Huawei.AppID = viper.GetString("huawei.appid")
conf.Huawei.MaxRetry = viper.GetInt("huawei.max_retry")
// iOS
conf.Ios.Enabled = viper.GetBool("ios.enabled")
conf.Ios.KeyPath = viper.GetString("ios.key_path")
conf.Ios.KeyBase64 = viper.GetString("ios.key_base64")
conf.Ios.KeyType = viper.GetString("ios.key_type")
conf.Ios.Password = viper.GetString("ios.password")
conf.Ios.Production = viper.GetBool("ios.production")
conf.Ios.MaxConcurrentPushes = viper.GetUint("ios.max_concurrent_pushes")
conf.Ios.MaxRetry = viper.GetInt("ios.max_retry")
conf.Ios.KeyID = viper.GetString("ios.key_id")
conf.Ios.TeamID = viper.GetString("ios.team_id")
// log
conf.Log.Format = viper.GetString("log.format")
conf.Log.AccessLog = viper.GetString("log.access_log")
conf.Log.AccessLevel = viper.GetString("log.access_level")
conf.Log.ErrorLog = viper.GetString("log.error_log")
conf.Log.ErrorLevel = viper.GetString("log.error_level")
conf.Log.HideToken = viper.GetBool("log.hide_token")
// Queue Engine
conf.Queue.Engine = viper.GetString("queue.engine")
conf.Queue.NSQ.Addr = viper.GetString("queue.nsq.addr")
conf.Queue.NSQ.Topic = viper.GetString("queue.nsq.topic")
conf.Queue.NSQ.Channel = viper.GetString("queue.nsq.channel")
conf.Queue.NATS.Addr = viper.GetString("queue.nats.addr")
conf.Queue.NATS.Subj = viper.GetString("queue.nats.subj")
conf.Queue.NATS.Queue = viper.GetString("queue.nats.queue")
conf.Queue.Redis.Addr = viper.GetString("queue.redis.addr")
conf.Queue.Redis.Channel = viper.GetString("queue.redis.channel")
conf.Queue.Redis.Size = viper.GetInt("queue.redis.size")
// Stat Engine
conf.Stat.Engine = viper.GetString("stat.engine")
conf.Stat.Redis.Cluster = viper.GetBool("stat.redis.cluster")
conf.Stat.Redis.Addr = viper.GetString("stat.redis.addr")
conf.Stat.Redis.Password = viper.GetString("stat.redis.password")
conf.Stat.Redis.DB = viper.GetInt("stat.redis.db")
conf.Stat.BoltDB.Path = viper.GetString("stat.boltdb.path")
conf.Stat.BoltDB.Bucket = viper.GetString("stat.boltdb.bucket")
conf.Stat.BuntDB.Path = viper.GetString("stat.buntdb.path")
conf.Stat.LevelDB.Path = viper.GetString("stat.leveldb.path")
conf.Stat.BadgerDB.Path = viper.GetString("stat.badgerdb.path")
// gRPC Server
conf.GRPC.Enabled = viper.GetBool("grpc.enabled")
conf.GRPC.Port = viper.GetString("grpc.port")
if conf.Core.WorkerNum == int64(0) {
conf.Core.WorkerNum = int64(runtime.NumCPU())
}
if conf.Core.QueueNum == int64(0) {
conf.Core.QueueNum = int64(8192)
}
return conf, nil
}