2016-03-27 02:58:57 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-02-04 05:27:27 +00:00
|
|
|
"context"
|
2016-03-27 02:58:57 +00:00
|
|
|
"flag"
|
2016-05-31 08:09:08 +00:00
|
|
|
"fmt"
|
2016-03-27 02:58:57 +00:00
|
|
|
"log"
|
2020-01-25 02:16:41 +00:00
|
|
|
"net"
|
2018-03-17 15:07:38 +00:00
|
|
|
"net/http"
|
2016-05-31 06:52:18 +00:00
|
|
|
"os"
|
2020-02-04 05:27:27 +00:00
|
|
|
"os/signal"
|
2017-01-10 09:13:12 +00:00
|
|
|
"path/filepath"
|
2016-08-19 13:16:10 +00:00
|
|
|
"strconv"
|
2020-02-04 05:27:27 +00:00
|
|
|
"sync"
|
|
|
|
"syscall"
|
2020-01-25 02:16:41 +00:00
|
|
|
"time"
|
2016-10-03 05:42:29 +00:00
|
|
|
|
|
|
|
"github.com/appleboy/gorush/config"
|
2021-07-13 08:32:39 +00:00
|
|
|
"github.com/appleboy/gorush/core"
|
2016-10-03 05:42:29 +00:00
|
|
|
"github.com/appleboy/gorush/gorush"
|
2021-07-13 08:32:39 +00:00
|
|
|
"github.com/appleboy/gorush/logx"
|
2017-07-24 07:06:23 +00:00
|
|
|
"github.com/appleboy/gorush/rpc"
|
2017-08-15 04:29:59 +00:00
|
|
|
|
|
|
|
"golang.org/x/sync/errgroup"
|
2016-03-27 02:58:57 +00:00
|
|
|
)
|
|
|
|
|
2020-02-04 05:27:27 +00:00
|
|
|
func withContextFunc(ctx context.Context, f func()) context.Context {
|
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
|
|
go func() {
|
|
|
|
c := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
|
|
|
|
defer signal.Stop(c)
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
case <-c:
|
|
|
|
cancel()
|
|
|
|
f()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return ctx
|
|
|
|
}
|
|
|
|
|
2016-03-27 02:58:57 +00:00
|
|
|
func main() {
|
2016-05-31 06:52:18 +00:00
|
|
|
opts := config.ConfYaml{}
|
|
|
|
|
2017-06-24 04:04:00 +00:00
|
|
|
var (
|
2018-03-17 15:07:38 +00:00
|
|
|
ping bool
|
2017-06-24 04:04:00 +00:00
|
|
|
showVersion bool
|
|
|
|
configFile string
|
|
|
|
topic string
|
|
|
|
message string
|
|
|
|
token string
|
|
|
|
title string
|
|
|
|
)
|
2016-05-31 06:52:18 +00:00
|
|
|
|
|
|
|
flag.BoolVar(&showVersion, "version", false, "Print version information.")
|
2021-07-11 12:26:22 +00:00
|
|
|
flag.BoolVar(&showVersion, "V", false, "Print version information.")
|
2017-01-07 13:16:27 +00:00
|
|
|
flag.StringVar(&configFile, "c", "", "Configuration file path.")
|
|
|
|
flag.StringVar(&configFile, "config", "", "Configuration file path.")
|
|
|
|
flag.StringVar(&opts.Core.PID.Path, "pid", "", "PID file path.")
|
2016-06-12 11:09:41 +00:00
|
|
|
flag.StringVar(&opts.Ios.KeyPath, "i", "", "iOS certificate key file path")
|
2016-06-12 11:25:06 +00:00
|
|
|
flag.StringVar(&opts.Ios.KeyPath, "key", "", "iOS certificate key file path")
|
2020-05-18 14:32:50 +00:00
|
|
|
flag.StringVar(&opts.Ios.KeyID, "key-id", "", "iOS Key ID for P8 token")
|
|
|
|
flag.StringVar(&opts.Ios.TeamID, "team-id", "", "iOS Team ID for P8 token")
|
2016-05-31 08:09:08 +00:00
|
|
|
flag.StringVar(&opts.Ios.Password, "P", "", "iOS certificate password for gorush")
|
2016-05-31 06:52:18 +00:00
|
|
|
flag.StringVar(&opts.Ios.Password, "password", "", "iOS certificate password for gorush")
|
|
|
|
flag.StringVar(&opts.Android.APIKey, "k", "", "Android api key configuration for gorush")
|
2016-06-12 11:25:06 +00:00
|
|
|
flag.StringVar(&opts.Android.APIKey, "apikey", "", "Android api key configuration for gorush")
|
2021-03-28 14:13:02 +00:00
|
|
|
flag.StringVar(&opts.Huawei.AppSecret, "hk", "", "Huawei api key configuration for gorush")
|
|
|
|
flag.StringVar(&opts.Huawei.AppSecret, "hmskey", "", "Huawei api key configuration for gorush")
|
|
|
|
flag.StringVar(&opts.Huawei.AppID, "hid", "", "HMS app id configuration for gorush")
|
|
|
|
flag.StringVar(&opts.Huawei.AppID, "hmsid", "", "HMS app id configuration for gorush")
|
2017-07-31 08:31:07 +00:00
|
|
|
flag.StringVar(&opts.Core.Address, "A", "", "address to bind")
|
|
|
|
flag.StringVar(&opts.Core.Address, "address", "", "address to bind")
|
2016-05-31 06:52:18 +00:00
|
|
|
flag.StringVar(&opts.Core.Port, "p", "", "port number for gorush")
|
2016-05-31 08:09:08 +00:00
|
|
|
flag.StringVar(&opts.Core.Port, "port", "", "port number for gorush")
|
2016-05-31 06:52:18 +00:00
|
|
|
flag.StringVar(&token, "t", "", "token string")
|
2016-05-31 08:09:08 +00:00
|
|
|
flag.StringVar(&token, "token", "", "token string")
|
2017-06-07 07:59:14 +00:00
|
|
|
flag.StringVar(&opts.Stat.Engine, "e", "", "store engine")
|
|
|
|
flag.StringVar(&opts.Stat.Engine, "engine", "", "store engine")
|
2017-06-07 11:09:21 +00:00
|
|
|
flag.StringVar(&opts.Stat.Redis.Addr, "redis-addr", "", "redis addr")
|
2016-05-31 06:52:18 +00:00
|
|
|
flag.StringVar(&message, "m", "", "notification message")
|
2016-05-31 08:09:08 +00:00
|
|
|
flag.StringVar(&message, "message", "", "notification message")
|
2017-01-21 07:46:44 +00:00
|
|
|
flag.StringVar(&title, "title", "", "notification title")
|
2016-05-31 06:52:18 +00:00
|
|
|
flag.BoolVar(&opts.Android.Enabled, "android", false, "send android notification")
|
2020-09-04 03:01:21 +00:00
|
|
|
flag.BoolVar(&opts.Huawei.Enabled, "huawei", false, "send huawei notification")
|
2016-05-31 06:52:18 +00:00
|
|
|
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")
|
2019-12-07 23:30:24 +00:00
|
|
|
flag.StringVar(&opts.Core.HTTPProxy, "proxy", "", "http proxy url")
|
2018-03-17 15:07:38 +00:00
|
|
|
flag.BoolVar(&ping, "ping", false, "ping server")
|
2016-03-27 02:58:57 +00:00
|
|
|
|
2016-05-31 08:09:08 +00:00
|
|
|
flag.Usage = usage
|
2016-03-27 02:58:57 +00:00
|
|
|
flag.Parse()
|
|
|
|
|
2016-05-09 06:48:07 +00:00
|
|
|
gorush.SetVersion(Version)
|
|
|
|
|
2016-05-31 06:52:18 +00:00
|
|
|
// Show version and exit
|
|
|
|
if showVersion {
|
2016-04-13 07:22:04 +00:00
|
|
|
gorush.PrintGoRushVersion()
|
2016-05-31 06:52:18 +00:00
|
|
|
os.Exit(0)
|
2016-03-27 02:58:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
|
|
|
|
// set default parameters.
|
2017-10-23 03:06:30 +00:00
|
|
|
gorush.PushConf, err = config.LoadConf(configFile)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Load yaml config file error: '%v'", err)
|
2016-03-27 02:58:57 +00:00
|
|
|
|
2017-10-23 03:06:30 +00:00
|
|
|
return
|
2016-03-27 02:58:57 +00:00
|
|
|
}
|
|
|
|
|
2020-04-23 13:02:53 +00:00
|
|
|
// Initialize push slots for concurrent iOS pushes
|
|
|
|
gorush.MaxConcurrentIOSPushes = make(chan struct{}, gorush.PushConf.Ios.MaxConcurrentPushes)
|
|
|
|
|
2016-06-12 11:09:41 +00:00
|
|
|
if opts.Ios.KeyPath != "" {
|
|
|
|
gorush.PushConf.Ios.KeyPath = opts.Ios.KeyPath
|
2016-03-27 02:58:57 +00:00
|
|
|
}
|
|
|
|
|
2020-05-18 14:32:50 +00:00
|
|
|
if opts.Ios.KeyID != "" {
|
|
|
|
gorush.PushConf.Ios.KeyID = opts.Ios.KeyID
|
|
|
|
}
|
|
|
|
|
|
|
|
if opts.Ios.TeamID != "" {
|
|
|
|
gorush.PushConf.Ios.TeamID = opts.Ios.TeamID
|
|
|
|
}
|
|
|
|
|
2016-05-31 06:52:18 +00:00
|
|
|
if opts.Ios.Password != "" {
|
|
|
|
gorush.PushConf.Ios.Password = opts.Ios.Password
|
2016-05-30 07:53:16 +00:00
|
|
|
}
|
|
|
|
|
2016-05-31 06:52:18 +00:00
|
|
|
if opts.Android.APIKey != "" {
|
|
|
|
gorush.PushConf.Android.APIKey = opts.Android.APIKey
|
2016-03-27 04:55:31 +00:00
|
|
|
}
|
|
|
|
|
2021-03-28 14:13:02 +00:00
|
|
|
if opts.Huawei.AppSecret != "" {
|
|
|
|
gorush.PushConf.Huawei.AppSecret = opts.Huawei.AppSecret
|
2020-09-04 03:01:21 +00:00
|
|
|
}
|
|
|
|
|
2021-03-28 14:13:02 +00:00
|
|
|
if opts.Huawei.AppID != "" {
|
|
|
|
gorush.PushConf.Huawei.AppID = opts.Huawei.AppID
|
2020-09-04 03:01:21 +00:00
|
|
|
}
|
|
|
|
|
2017-06-07 07:59:14 +00:00
|
|
|
if opts.Stat.Engine != "" {
|
|
|
|
gorush.PushConf.Stat.Engine = opts.Stat.Engine
|
|
|
|
}
|
|
|
|
|
2017-06-07 11:09:21 +00:00
|
|
|
if opts.Stat.Redis.Addr != "" {
|
|
|
|
gorush.PushConf.Stat.Redis.Addr = opts.Stat.Redis.Addr
|
|
|
|
}
|
|
|
|
|
2017-07-31 08:31:07 +00:00
|
|
|
// overwrite server port and address
|
2016-05-31 06:52:18 +00:00
|
|
|
if opts.Core.Port != "" {
|
|
|
|
gorush.PushConf.Core.Port = opts.Core.Port
|
2016-03-27 02:58:57 +00:00
|
|
|
}
|
2017-07-31 08:31:07 +00:00
|
|
|
if opts.Core.Address != "" {
|
|
|
|
gorush.PushConf.Core.Address = opts.Core.Address
|
|
|
|
}
|
2016-03-27 02:58:57 +00:00
|
|
|
|
2021-07-13 08:32:39 +00:00
|
|
|
if err = logx.InitLog(
|
|
|
|
gorush.PushConf.Log.AccessLevel,
|
|
|
|
gorush.PushConf.Log.AccessLog,
|
|
|
|
gorush.PushConf.Log.ErrorLevel,
|
|
|
|
gorush.PushConf.Log.ErrorLog,
|
|
|
|
); err != nil {
|
2017-10-23 03:06:30 +00:00
|
|
|
log.Fatalf("Can't load log module, error: %v", err)
|
2016-04-05 06:50:08 +00:00
|
|
|
}
|
2016-04-05 05:15:47 +00:00
|
|
|
|
2019-12-07 23:30:24 +00:00
|
|
|
if opts.Core.HTTPProxy != "" {
|
|
|
|
gorush.PushConf.Core.HTTPProxy = opts.Core.HTTPProxy
|
|
|
|
}
|
2016-07-29 01:38:06 +00:00
|
|
|
|
2019-12-07 23:30:24 +00:00
|
|
|
if gorush.PushConf.Core.HTTPProxy != "" {
|
2016-07-29 01:38:06 +00:00
|
|
|
err = gorush.SetProxy(gorush.PushConf.Core.HTTPProxy)
|
|
|
|
|
|
|
|
if err != nil {
|
2021-07-13 08:32:39 +00:00
|
|
|
logx.LogError.Fatalf("Set Proxy error: %v", err)
|
2016-07-29 01:38:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-17 15:07:38 +00:00
|
|
|
if ping {
|
|
|
|
if err := pinger(); err != nil {
|
2021-07-13 08:32:39 +00:00
|
|
|
logx.LogError.Warnf("ping server error: %v", err)
|
2018-03-17 15:07:38 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-04-10 08:00:17 +00:00
|
|
|
// send android notification
|
2016-05-31 06:52:18 +00:00
|
|
|
if opts.Android.Enabled {
|
|
|
|
gorush.PushConf.Android.Enabled = opts.Android.Enabled
|
2016-04-13 07:22:04 +00:00
|
|
|
req := gorush.PushNotification{
|
2021-07-13 08:32:39 +00:00
|
|
|
Platform: core.PlatFormAndroid,
|
2016-05-31 06:52:18 +00:00
|
|
|
Message: message,
|
2017-01-21 07:46:44 +00:00
|
|
|
Title: title,
|
2016-04-10 08:00:17 +00:00
|
|
|
}
|
|
|
|
|
2017-10-24 09:00:08 +00:00
|
|
|
// send message to single device
|
|
|
|
if token != "" {
|
|
|
|
req.Tokens = []string{token}
|
|
|
|
}
|
|
|
|
|
|
|
|
// send topic message
|
|
|
|
if topic != "" {
|
|
|
|
req.To = topic
|
|
|
|
}
|
|
|
|
|
2016-04-24 07:54:41 +00:00
|
|
|
err := gorush.CheckMessage(req)
|
2016-04-24 06:30:17 +00:00
|
|
|
if err != nil {
|
2021-07-13 08:32:39 +00:00
|
|
|
logx.LogError.Fatal(err)
|
2016-04-24 06:30:17 +00:00
|
|
|
}
|
|
|
|
|
2017-05-12 03:17:55 +00:00
|
|
|
if err := gorush.InitAppStatus(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-04-13 07:22:04 +00:00
|
|
|
gorush.PushToAndroid(req)
|
2016-04-02 15:51:42 +00:00
|
|
|
|
2016-04-02 23:27:10 +00:00
|
|
|
return
|
2016-04-02 15:51:42 +00:00
|
|
|
}
|
|
|
|
|
2020-09-04 03:01:21 +00:00
|
|
|
// send huawei notification
|
|
|
|
if opts.Huawei.Enabled {
|
|
|
|
gorush.PushConf.Huawei.Enabled = opts.Huawei.Enabled
|
|
|
|
req := gorush.PushNotification{
|
2021-07-13 08:32:39 +00:00
|
|
|
Platform: core.PlatFormHuawei,
|
2020-09-04 03:01:21 +00:00
|
|
|
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 {
|
2021-07-13 08:32:39 +00:00
|
|
|
logx.LogError.Fatal(err)
|
2020-09-04 03:01:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := gorush.InitAppStatus(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
gorush.PushToHuawei(req)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-09-30 15:06:12 +00:00
|
|
|
// send ios notification
|
2016-05-31 06:52:18 +00:00
|
|
|
if opts.Ios.Enabled {
|
|
|
|
if opts.Ios.Production {
|
|
|
|
gorush.PushConf.Ios.Production = opts.Ios.Production
|
2016-04-10 08:00:17 +00:00
|
|
|
}
|
|
|
|
|
2016-05-31 06:52:18 +00:00
|
|
|
gorush.PushConf.Ios.Enabled = opts.Ios.Enabled
|
2016-04-13 07:22:04 +00:00
|
|
|
req := gorush.PushNotification{
|
2021-07-13 08:32:39 +00:00
|
|
|
Platform: core.PlatFormIos,
|
2016-05-31 06:52:18 +00:00
|
|
|
Message: message,
|
2017-01-21 07:46:44 +00:00
|
|
|
Title: title,
|
2016-04-10 08:00:17 +00:00
|
|
|
}
|
|
|
|
|
2017-10-24 09:00:08 +00:00
|
|
|
// send message to single device
|
|
|
|
if token != "" {
|
|
|
|
req.Tokens = []string{token}
|
|
|
|
}
|
|
|
|
|
|
|
|
// send topic message
|
2016-05-31 06:52:18 +00:00
|
|
|
if topic != "" {
|
|
|
|
req.Topic = topic
|
2016-04-28 06:39:34 +00:00
|
|
|
}
|
|
|
|
|
2016-04-24 07:54:41 +00:00
|
|
|
err := gorush.CheckMessage(req)
|
|
|
|
if err != nil {
|
2021-07-13 08:32:39 +00:00
|
|
|
logx.LogError.Fatal(err)
|
2016-04-24 07:54:41 +00:00
|
|
|
}
|
|
|
|
|
2017-05-12 03:17:55 +00:00
|
|
|
if err := gorush.InitAppStatus(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := gorush.InitAPNSClient(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2016-04-13 07:22:04 +00:00
|
|
|
gorush.PushToIOS(req)
|
2016-04-10 08:00:17 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-04-13 07:22:04 +00:00
|
|
|
if err = gorush.CheckPushConf(); err != nil {
|
2021-07-13 08:32:39 +00:00
|
|
|
logx.LogError.Fatal(err)
|
2016-04-10 08:00:17 +00:00
|
|
|
}
|
|
|
|
|
2017-01-07 13:16:27 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2016-08-19 13:16:10 +00:00
|
|
|
if err = createPIDFile(); err != nil {
|
2021-07-13 08:32:39 +00:00
|
|
|
logx.LogError.Fatal(err)
|
2016-08-19 13:16:10 +00:00
|
|
|
}
|
|
|
|
|
2017-05-12 03:17:55 +00:00
|
|
|
if err = gorush.InitAppStatus(); err != nil {
|
2021-07-13 08:32:39 +00:00
|
|
|
logx.LogError.Fatal(err)
|
2017-05-12 03:17:55 +00:00
|
|
|
}
|
|
|
|
|
2020-02-04 09:10:12 +00:00
|
|
|
finished := make(chan struct{})
|
2020-02-04 05:27:27 +00:00
|
|
|
wg := &sync.WaitGroup{}
|
|
|
|
wg.Add(int(gorush.PushConf.Core.WorkerNum))
|
|
|
|
ctx := withContextFunc(context.Background(), func() {
|
2021-07-13 08:32:39 +00:00
|
|
|
logx.LogAccess.Info("close the notification queue channel, current queue len: ", len(gorush.QueueNotification))
|
2020-02-04 05:27:27 +00:00
|
|
|
close(gorush.QueueNotification)
|
|
|
|
wg.Wait()
|
2021-07-13 08:32:39 +00:00
|
|
|
logx.LogAccess.Info("the notification queue has been clear")
|
2020-04-23 07:39:24 +00:00
|
|
|
close(finished)
|
|
|
|
// close the connection with storage
|
2021-07-13 08:32:39 +00:00
|
|
|
logx.LogAccess.Info("close the storage connection: ", gorush.PushConf.Stat.Engine)
|
2020-04-23 07:39:24 +00:00
|
|
|
if err := gorush.StatStorage.Close(); err != nil {
|
2021-07-13 08:32:39 +00:00
|
|
|
logx.LogError.Fatal("can't close the storage connection: ", err.Error())
|
2020-04-23 07:39:24 +00:00
|
|
|
}
|
2020-02-04 05:27:27 +00:00
|
|
|
})
|
2017-05-12 03:17:55 +00:00
|
|
|
|
2020-02-04 05:27:27 +00:00
|
|
|
gorush.InitWorkers(ctx, wg, gorush.PushConf.Core.WorkerNum, gorush.PushConf.Core.QueueNum)
|
2017-07-24 07:06:23 +00:00
|
|
|
|
2021-06-21 16:17:57 +00:00
|
|
|
if gorush.PushConf.Ios.Enabled {
|
|
|
|
if err = gorush.InitAPNSClient(); err != nil {
|
2021-07-13 08:32:39 +00:00
|
|
|
logx.LogError.Fatal(err)
|
2021-06-21 16:17:57 +00:00
|
|
|
}
|
2020-02-04 05:27:27 +00:00
|
|
|
}
|
2017-07-25 08:41:30 +00:00
|
|
|
|
2021-06-21 16:17:57 +00:00
|
|
|
if gorush.PushConf.Android.Enabled {
|
|
|
|
if _, err = gorush.InitFCMClient(gorush.PushConf.Android.APIKey); err != nil {
|
2021-07-13 08:32:39 +00:00
|
|
|
logx.LogError.Fatal(err)
|
2021-06-21 16:17:57 +00:00
|
|
|
}
|
2020-02-04 05:27:27 +00:00
|
|
|
}
|
|
|
|
|
2021-06-21 16:17:57 +00:00
|
|
|
if gorush.PushConf.Huawei.Enabled {
|
|
|
|
if _, err = gorush.InitHMSClient(gorush.PushConf.Huawei.AppSecret, gorush.PushConf.Huawei.AppID); err != nil {
|
2021-07-13 08:32:39 +00:00
|
|
|
logx.LogError.Fatal(err)
|
2021-06-21 16:17:57 +00:00
|
|
|
}
|
2020-09-04 03:01:21 +00:00
|
|
|
}
|
|
|
|
|
2020-02-04 05:27:27 +00:00
|
|
|
var g errgroup.Group
|
2017-07-25 08:41:30 +00:00
|
|
|
|
2020-02-04 14:42:06 +00:00
|
|
|
// Run httpd server
|
2020-02-04 09:10:12 +00:00
|
|
|
g.Go(func() error {
|
|
|
|
return gorush.RunHTTPServer(ctx)
|
2020-02-04 14:42:06 +00:00
|
|
|
})
|
2020-02-04 09:10:12 +00:00
|
|
|
|
2020-02-04 14:42:06 +00:00
|
|
|
// Run gRPC internal server
|
|
|
|
g.Go(func() error {
|
|
|
|
return rpc.RunGRPCServer(ctx)
|
|
|
|
})
|
2020-02-04 09:10:12 +00:00
|
|
|
|
|
|
|
// check job completely
|
|
|
|
g.Go(func() error {
|
|
|
|
select {
|
|
|
|
case <-finished:
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
2017-07-24 07:06:23 +00:00
|
|
|
|
|
|
|
if err = g.Wait(); err != nil {
|
2021-07-13 08:32:39 +00:00
|
|
|
logx.LogError.Fatal(err)
|
2017-05-12 03:17:55 +00:00
|
|
|
}
|
2016-03-27 02:58:57 +00:00
|
|
|
}
|
2017-08-02 14:31:31 +00:00
|
|
|
|
|
|
|
// 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
|
2019-12-07 23:30:24 +00:00
|
|
|
--proxy <proxy> Proxy URL
|
2017-08-02 14:31:31 +00:00
|
|
|
--pid <pid path> Process identifier path
|
|
|
|
--redis-addr <redis addr> Redis addr (default: localhost:6379)
|
2018-03-17 15:07:38 +00:00
|
|
|
--ping healthy check command for container
|
2017-08-02 14:31:31 +00:00
|
|
|
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)
|
2020-09-04 03:01:21 +00:00
|
|
|
Huawei Options:
|
2021-03-28 14:13:02 +00:00
|
|
|
-hk, --hmskey <hms_key> HMS App Secret
|
|
|
|
-hid, --hmsid <hms_id> HMS App ID
|
2020-09-04 03:01:21 +00:00
|
|
|
--huawei enabled huawei (default: false)
|
2017-08-02 14:31:31 +00:00
|
|
|
Common Options:
|
2020-09-04 03:01:21 +00:00
|
|
|
--topic <topic> iOS, Android or Huawei topic message
|
2017-08-02 14:31:31 +00:00
|
|
|
-h, --help Show this message
|
2021-07-11 12:26:22 +00:00
|
|
|
-V, --version Show version
|
2017-08-02 14:31:31 +00:00
|
|
|
`
|
|
|
|
|
|
|
|
// usage will print out the flag options for the server.
|
|
|
|
func usage() {
|
|
|
|
fmt.Printf("%s\n", usageStr)
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
2018-03-17 15:07:38 +00:00
|
|
|
// handles pinging the endpoint and returns an error if the
|
|
|
|
// agent is in an unhealthy state.
|
|
|
|
func pinger() error {
|
2021-01-23 01:39:06 +00:00
|
|
|
transport := &http.Transport{
|
2020-01-25 02:16:41 +00:00
|
|
|
Dial: (&net.Dialer{
|
|
|
|
Timeout: 5 * time.Second,
|
|
|
|
}).Dial,
|
|
|
|
TLSHandshakeTimeout: 5 * time.Second,
|
|
|
|
}
|
2021-01-23 01:39:06 +00:00
|
|
|
client := &http.Client{
|
2020-01-25 02:16:41 +00:00
|
|
|
Timeout: time.Second * 10,
|
|
|
|
Transport: transport,
|
|
|
|
}
|
|
|
|
resp, err := client.Get("http://localhost:" + gorush.PushConf.Core.Port + gorush.PushConf.API.HealthURI)
|
2018-03-17 15:07:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
2020-01-25 02:16:41 +00:00
|
|
|
if resp.StatusCode != http.StatusOK {
|
2018-03-17 15:07:38 +00:00
|
|
|
return fmt.Errorf("server returned non-200 status code")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-08-02 14:31:31 +00:00
|
|
|
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 {
|
2019-10-16 07:01:58 +00:00
|
|
|
return fmt.Errorf("Can't write PID information on %s: %v", pidPath, err)
|
2017-08-02 14:31:31 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("%s already exists", pidPath)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|