forked from appleboy/gorush
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
334 lines (275 loc) · 8.88 KB
/
main.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
package main
import (
"flag"
"fmt"
"log"
"net/http"
"os"
"path/filepath"
"strconv"
"github.com/appleboy/gorush/config"
"github.com/appleboy/gorush/gorush"
"github.com/appleboy/gorush/rpc"
"golang.org/x/sync/errgroup"
)
func main() {
opts := config.ConfYaml{}
var (
ping bool
showVersion bool
configFile string
topic string
message string
token string
proxy string
title string
)
flag.BoolVar(&showVersion, "version", false, "Print version information.")
flag.BoolVar(&showVersion, "v", false, "Print version information.")
flag.StringVar(&configFile, "c", "", "Configuration file path.")
flag.StringVar(&configFile, "config", "", "Configuration file path.")
flag.StringVar(&opts.Core.PID.Path, "pid", "", "PID file path.")
flag.StringVar(&opts.Ios.KeyPath, "i", "", "iOS certificate key file path")
flag.StringVar(&opts.Ios.KeyPath, "key", "", "iOS certificate key file path")
flag.StringVar(&opts.Ios.Password, "P", "", "iOS certificate password for gorush")
flag.StringVar(&opts.Ios.Password, "password", "", "iOS certificate password for gorush")
flag.StringVar(&opts.Android.APIKey, "k", "", "Android api key configuration for gorush")
flag.StringVar(&opts.Android.APIKey, "apikey", "", "Android api key configuration for gorush")
flag.StringVar(&opts.Core.Address, "A", "", "address to bind")
flag.StringVar(&opts.Core.Address, "address", "", "address to bind")
flag.StringVar(&opts.Core.Port, "p", "", "port number for gorush")
flag.StringVar(&opts.Core.Port, "port", "", "port number for gorush")
flag.StringVar(&token, "t", "", "token string")
flag.StringVar(&token, "token", "", "token string")
flag.StringVar(&opts.Stat.Engine, "e", "", "store engine")
flag.StringVar(&opts.Stat.Engine, "engine", "", "store engine")
flag.StringVar(&opts.Stat.Redis.Addr, "redis-addr", "", "redis addr")
flag.StringVar(&message, "m", "", "notification message")
flag.StringVar(&message, "message", "", "notification message")
flag.StringVar(&title, "title", "", "notification title")
flag.BoolVar(&opts.Android.Enabled, "android", false, "send android notification")
flag.BoolVar(&opts.Ios.Enabled, "ios", false, "send ios notification")
flag.BoolVar(&opts.Ios.Production, "production", false, "production mode in iOS")
flag.StringVar(&topic, "topic", "", "apns topic in iOS")
flag.StringVar(&proxy, "proxy", "", "http proxy url")
flag.BoolVar(&ping, "ping", false, "ping server")
flag.Usage = usage
flag.Parse()
gorush.SetVersion(Version)
// Show version and exit
if showVersion {
gorush.PrintGoRushVersion()
os.Exit(0)
}
var err error
// set default parameters.
gorush.PushConf, err = config.LoadConf(configFile)
if err != nil {
log.Printf("Load yaml config file error: '%v'", err)
return
}
if opts.Ios.KeyPath != "" {
gorush.PushConf.Ios.KeyPath = opts.Ios.KeyPath
}
if opts.Ios.Password != "" {
gorush.PushConf.Ios.Password = opts.Ios.Password
}
if opts.Android.APIKey != "" {
gorush.PushConf.Android.APIKey = opts.Android.APIKey
}
if opts.Stat.Engine != "" {
gorush.PushConf.Stat.Engine = opts.Stat.Engine
}
if opts.Stat.Redis.Addr != "" {
gorush.PushConf.Stat.Redis.Addr = opts.Stat.Redis.Addr
}
// overwrite server port and address
if opts.Core.Port != "" {
gorush.PushConf.Core.Port = opts.Core.Port
}
if opts.Core.Address != "" {
gorush.PushConf.Core.Address = opts.Core.Address
}
if err = gorush.InitLog(); err != nil {
log.Fatalf("Can't load log module, error: %v", err)
}
// set http proxy for GCM
if proxy != "" {
err = gorush.SetProxy(proxy)
if err != nil {
gorush.LogError.Fatalf("Set Proxy error: %v", err)
}
} else if gorush.PushConf.Core.HTTPProxy != "" {
err = gorush.SetProxy(gorush.PushConf.Core.HTTPProxy)
if err != nil {
gorush.LogError.Fatalf("Set Proxy error: %v", err)
}
}
if ping {
if err := pinger(); err != nil {
gorush.LogError.Warnf("ping server error: %v", err)
}
return
}
// send android notification
if opts.Android.Enabled {
gorush.PushConf.Android.Enabled = opts.Android.Enabled
req := gorush.PushNotification{
Platform: gorush.PlatFormAndroid,
Message: message,
Title: title,
}
// send message to single device
if token != "" {
req.Tokens = []string{token}
}
// send topic message
if topic != "" {
req.To = topic
}
err := gorush.CheckMessage(req)
if err != nil {
gorush.LogError.Fatal(err)
}
if err := gorush.InitAppStatus(); err != nil {
return
}
gorush.PushToAndroid(req)
return
}
// send ios notification
if opts.Ios.Enabled {
if opts.Ios.Production {
gorush.PushConf.Ios.Production = opts.Ios.Production
}
gorush.PushConf.Ios.Enabled = opts.Ios.Enabled
req := gorush.PushNotification{
Platform: gorush.PlatFormIos,
Message: message,
Title: title,
}
// send message to single device
if token != "" {
req.Tokens = []string{token}
}
// send topic message
if topic != "" {
req.Topic = topic
}
err := gorush.CheckMessage(req)
if err != nil {
gorush.LogError.Fatal(err)
}
if err := gorush.InitAppStatus(); err != nil {
return
}
if err := gorush.InitAPNSClient(); err != nil {
return
}
gorush.PushToIOS(req)
return
}
if err = gorush.CheckPushConf(); err != nil {
gorush.LogError.Fatal(err)
}
if opts.Core.PID.Path != "" {
gorush.PushConf.Core.PID.Path = opts.Core.PID.Path
gorush.PushConf.Core.PID.Enabled = true
gorush.PushConf.Core.PID.Override = true
}
if err = createPIDFile(); err != nil {
gorush.LogError.Fatal(err)
}
if err = gorush.InitAppStatus(); err != nil {
return
}
gorush.InitWorkers(gorush.PushConf.Core.WorkerNum, gorush.PushConf.Core.QueueNum)
var g errgroup.Group
g.Go(gorush.InitAPNSClient)
g.Go(func() error {
_, err := gorush.InitFCMClient(gorush.PushConf.Android.APIKey)
return err
})
g.Go(gorush.RunHTTPServer) // Run httpd server
g.Go(rpc.RunGRPCServer) // Run gRPC internal server
if err = g.Wait(); err != nil {
gorush.LogError.Fatal(err)
}
}
// Version control for gorush.
var Version = "No Version Provided"
var usageStr = `
________ .__
/ _____/ ____ _______ __ __ ______| |__
/ \ ___ / _ \\_ __ \| | \/ ___/| | \
\ \_\ \( <_> )| | \/| | /\___ \ | Y \
\______ / \____/ |__| |____//____ >|___| /
\/ \/ \/
Usage: gorush [options]
Server Options:
-A, --address <address> Address to bind (default: any)
-p, --port <port> Use port for clients (default: 8088)
-c, --config <file> Configuration file path
-m, --message <message> Notification message
-t, --token <token> Notification token
-e, --engine <engine> Storage engine (memory, redis ...)
--title <title> Notification title
--proxy <proxy> Proxy URL (only for GCM)
--pid <pid path> Process identifier path
--redis-addr <redis addr> Redis addr (default: localhost:6379)
--ping healthy check command for container
iOS Options:
-i, --key <file> certificate key file path
-P, --password <password> certificate key password
--ios enabled iOS (default: false)
--production iOS production mode (default: false)
Android Options:
-k, --apikey <api_key> Android API Key
--android enabled android (default: false)
Common Options:
--topic <topic> iOS or Android topic message
-h, --help Show this message
-v, --version Show version
`
// usage will print out the flag options for the server.
func usage() {
fmt.Printf("%s\n", usageStr)
os.Exit(0)
}
// handles pinging the endpoint and returns an error if the
// agent is in an unhealthy state.
func pinger() error {
resp, err := http.Get("http://localhost:" + gorush.PushConf.Core.Port + gorush.PushConf.API.HealthURI)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return fmt.Errorf("server returned non-200 status code")
}
return nil
}
func createPIDFile() error {
if !gorush.PushConf.Core.PID.Enabled {
return nil
}
pidPath := gorush.PushConf.Core.PID.Path
_, err := os.Stat(pidPath)
if os.IsNotExist(err) || gorush.PushConf.Core.PID.Override {
currentPid := os.Getpid()
if err := os.MkdirAll(filepath.Dir(pidPath), os.ModePerm); err != nil {
return fmt.Errorf("Can't create PID folder on %v", err)
}
file, err := os.Create(pidPath)
if err != nil {
return fmt.Errorf("Can't create PID file: %v", err)
}
defer file.Close()
if _, err := file.WriteString(strconv.FormatInt(int64(currentPid), 10)); err != nil {
return fmt.Errorf("Can'write PID information on %s: %v", pidPath, err)
}
} else {
return fmt.Errorf("%s already exists", pidPath)
}
return nil
}