From d9947ea44db6d463e31b63a1342c8e4e3df6927f Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Sat, 24 Jul 2021 01:56:33 +0800 Subject: [PATCH] chore: rename gorush to notify package (#609) Signed-off-by: Bo-Yi Wu --- Makefile | 2 +- main.go | 40 ++++++++++---------- {gorush => notify}/feedback.go | 2 +- {gorush => notify}/feedback_test.go | 2 +- {gorush => notify}/global.go | 2 +- {gorush => notify}/main_test.go | 2 +- {gorush => notify}/notification.go | 4 +- {gorush => notify}/notification_apns.go | 2 +- {gorush => notify}/notification_apns_test.go | 2 +- {gorush => notify}/notification_fcm.go | 2 +- {gorush => notify}/notification_fcm_test.go | 2 +- {gorush => notify}/notification_hms.go | 2 +- {gorush => notify}/notification_hms_test.go | 2 +- {gorush => notify}/notification_test.go | 2 +- queue/nsq/nsq.go | 6 +-- queue/simple/simple.go | 4 +- queue/simple/simple_test.go | 12 +++--- router/server.go | 10 ++--- router/server_test.go | 38 +++++++++---------- rpc/server.go | 8 ++-- 20 files changed, 73 insertions(+), 73 deletions(-) rename {gorush => notify}/feedback.go (98%) rename {gorush => notify}/feedback_test.go (99%) rename {gorush => notify}/global.go (96%) rename {gorush => notify}/main_test.go (94%) rename {gorush => notify}/notification.go (99%) rename {gorush => notify}/notification_apns.go (99%) rename {gorush => notify}/notification_apns_test.go (99%) rename {gorush => notify}/notification_fcm.go (99%) rename {gorush => notify}/notification_fcm_test.go (99%) rename {gorush => notify}/notification_hms.go (99%) rename {gorush => notify}/notification_hms_test.go (98%) rename {gorush => notify}/notification_test.go (97%) diff --git a/Makefile b/Makefile index 9749c33..ad9fb96 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ DEPLOY_ACCOUNT := appleboy DEPLOY_IMAGE := $(EXECUTABLE) GOFMT ?= gofumpt -l -s -extra -TARGETS ?= linux darwin windows openbsd +TARGETS ?= linux darwin windows ARCHS ?= amd64 GOFILES := $(shell find . -name "*.go" -type f) TAGS ?= sqlite diff --git a/main.go b/main.go index 8dae9b5..b538706 100644 --- a/main.go +++ b/main.go @@ -16,8 +16,8 @@ import ( "github.com/appleboy/gorush/config" "github.com/appleboy/gorush/core" - "github.com/appleboy/gorush/gorush" "github.com/appleboy/gorush/logx" + "github.com/appleboy/gorush/notify" "github.com/appleboy/gorush/queue" "github.com/appleboy/gorush/queue/nsq" "github.com/appleboy/gorush/queue/simple" @@ -116,7 +116,7 @@ func main() { } // Initialize push slots for concurrent iOS pushes - gorush.MaxConcurrentIOSPushes = make(chan struct{}, cfg.Ios.MaxConcurrentPushes) + notify.MaxConcurrentIOSPushes = make(chan struct{}, cfg.Ios.MaxConcurrentPushes) if opts.Ios.KeyPath != "" { cfg.Ios.KeyPath = opts.Ios.KeyPath @@ -176,7 +176,7 @@ func main() { } if cfg.Core.HTTPProxy != "" { - err = gorush.SetProxy(cfg.Core.HTTPProxy) + err = notify.SetProxy(cfg.Core.HTTPProxy) if err != nil { logx.LogError.Fatalf("Set Proxy error: %v", err) @@ -193,7 +193,7 @@ func main() { // send android notification if opts.Android.Enabled { cfg.Android.Enabled = opts.Android.Enabled - req := gorush.PushNotification{ + req := notify.PushNotification{ Cfg: cfg, Platform: core.PlatFormAndroid, Message: message, @@ -210,7 +210,7 @@ func main() { req.To = topic } - err := gorush.CheckMessage(req) + err := notify.CheckMessage(req) if err != nil { logx.LogError.Fatal(err) } @@ -219,7 +219,7 @@ func main() { return } - gorush.PushToAndroid(req) + notify.PushToAndroid(req) return } @@ -227,7 +227,7 @@ func main() { // send huawei notification if opts.Huawei.Enabled { cfg.Huawei.Enabled = opts.Huawei.Enabled - req := gorush.PushNotification{ + req := notify.PushNotification{ Cfg: cfg, Platform: core.PlatFormHuawei, Message: message, @@ -244,7 +244,7 @@ func main() { req.To = topic } - err := gorush.CheckMessage(req) + err := notify.CheckMessage(req) if err != nil { logx.LogError.Fatal(err) } @@ -253,7 +253,7 @@ func main() { return } - gorush.PushToHuawei(req) + notify.PushToHuawei(req) return } @@ -265,7 +265,7 @@ func main() { } cfg.Ios.Enabled = opts.Ios.Enabled - req := gorush.PushNotification{ + req := notify.PushNotification{ Cfg: cfg, Platform: core.PlatFormIos, Message: message, @@ -282,7 +282,7 @@ func main() { req.Topic = topic } - err := gorush.CheckMessage(req) + err := notify.CheckMessage(req) if err != nil { logx.LogError.Fatal(err) } @@ -291,15 +291,15 @@ func main() { return } - if err := gorush.InitAPNSClient(cfg); err != nil { + if err := notify.InitAPNSClient(cfg); err != nil { return } - gorush.PushToIOS(req) + notify.PushToIOS(req) return } - if err = gorush.CheckPushConf(cfg); err != nil { + if err = notify.CheckPushConf(cfg); err != nil { logx.LogError.Fatal(err) } @@ -353,23 +353,23 @@ func main() { } }) - // gorush.InitQueue(cfg.Core.WorkerNum, cfg.Core.QueueNum) - // gorush.InitWorkers(ctx, wg, cfg.Core.WorkerNum, cfg.Core.QueueNum) + // notify.InitQueue(cfg.Core.WorkerNum, cfg.Core.QueueNum) + // notify.InitWorkers(ctx, wg, cfg.Core.WorkerNum, cfg.Core.QueueNum) if cfg.Ios.Enabled { - if err = gorush.InitAPNSClient(cfg); err != nil { + if err = notify.InitAPNSClient(cfg); err != nil { logx.LogError.Fatal(err) } } if cfg.Android.Enabled { - if _, err = gorush.InitFCMClient(cfg, cfg.Android.APIKey); err != nil { + if _, err = notify.InitFCMClient(cfg, cfg.Android.APIKey); err != nil { logx.LogError.Fatal(err) } } if cfg.Huawei.Enabled { - if _, err = gorush.InitHMSClient(cfg, cfg.Huawei.AppSecret, cfg.Huawei.AppID); err != nil { + if _, err = notify.InitHMSClient(cfg, cfg.Huawei.AppSecret, cfg.Huawei.AppID); err != nil { logx.LogError.Fatal(err) } } @@ -399,7 +399,7 @@ func main() { } } -// Version control for gorush. +// Version control for notify. var Version = "No Version Provided" var usageStr = ` diff --git a/gorush/feedback.go b/notify/feedback.go similarity index 98% rename from gorush/feedback.go rename to notify/feedback.go index 27c6adf..790404c 100644 --- a/gorush/feedback.go +++ b/notify/feedback.go @@ -1,4 +1,4 @@ -package gorush +package notify import ( "bytes" diff --git a/gorush/feedback_test.go b/notify/feedback_test.go similarity index 99% rename from gorush/feedback_test.go rename to notify/feedback_test.go index dbf460d..ec20b50 100644 --- a/gorush/feedback_test.go +++ b/notify/feedback_test.go @@ -1,4 +1,4 @@ -package gorush +package notify import ( "log" diff --git a/gorush/global.go b/notify/global.go similarity index 96% rename from gorush/global.go rename to notify/global.go index cdee529..7433be6 100644 --- a/gorush/global.go +++ b/notify/global.go @@ -1,4 +1,4 @@ -package gorush +package notify import ( "github.com/appleboy/go-fcm" diff --git a/gorush/main_test.go b/notify/main_test.go similarity index 94% rename from gorush/main_test.go rename to notify/main_test.go index cccd67d..96b219e 100644 --- a/gorush/main_test.go +++ b/notify/main_test.go @@ -1,4 +1,4 @@ -package gorush +package notify import ( "log" diff --git a/gorush/notification.go b/notify/notification.go similarity index 99% rename from gorush/notification.go rename to notify/notification.go index 0eea6c1..dece32f 100644 --- a/gorush/notification.go +++ b/notify/notification.go @@ -1,4 +1,4 @@ -package gorush +package notify import ( "errors" @@ -8,12 +8,12 @@ import ( "strings" "sync" - "github.com/appleboy/go-fcm" "github.com/appleboy/gorush/config" "github.com/appleboy/gorush/core" "github.com/appleboy/gorush/logx" "github.com/appleboy/gorush/queue" + "github.com/appleboy/go-fcm" jsoniter "github.com/json-iterator/go" "github.com/msalihkarakasli/go-hms-push/push/model" ) diff --git a/gorush/notification_apns.go b/notify/notification_apns.go similarity index 99% rename from gorush/notification_apns.go rename to notify/notification_apns.go index 507db21..4361ea0 100644 --- a/gorush/notification_apns.go +++ b/notify/notification_apns.go @@ -1,4 +1,4 @@ -package gorush +package notify import ( "crypto/ecdsa" diff --git a/gorush/notification_apns_test.go b/notify/notification_apns_test.go similarity index 99% rename from gorush/notification_apns_test.go rename to notify/notification_apns_test.go index 457fe77..14fa1bf 100644 --- a/gorush/notification_apns_test.go +++ b/notify/notification_apns_test.go @@ -1,4 +1,4 @@ -package gorush +package notify import ( "log" diff --git a/gorush/notification_fcm.go b/notify/notification_fcm.go similarity index 99% rename from gorush/notification_fcm.go rename to notify/notification_fcm.go index 22b45ef..868bc78 100644 --- a/gorush/notification_fcm.go +++ b/notify/notification_fcm.go @@ -1,4 +1,4 @@ -package gorush +package notify import ( "errors" diff --git a/gorush/notification_fcm_test.go b/notify/notification_fcm_test.go similarity index 99% rename from gorush/notification_fcm_test.go rename to notify/notification_fcm_test.go index 2f625e0..f1f027b 100644 --- a/gorush/notification_fcm_test.go +++ b/notify/notification_fcm_test.go @@ -1,4 +1,4 @@ -package gorush +package notify import ( "os" diff --git a/gorush/notification_hms.go b/notify/notification_hms.go similarity index 99% rename from gorush/notification_hms.go rename to notify/notification_hms.go index 7ce95ed..6f5ecda 100644 --- a/gorush/notification_hms.go +++ b/notify/notification_hms.go @@ -1,4 +1,4 @@ -package gorush +package notify import ( "context" diff --git a/gorush/notification_hms_test.go b/notify/notification_hms_test.go similarity index 98% rename from gorush/notification_hms_test.go rename to notify/notification_hms_test.go index 9e00fe3..27e1d84 100644 --- a/gorush/notification_hms_test.go +++ b/notify/notification_hms_test.go @@ -1,4 +1,4 @@ -package gorush +package notify import ( "testing" diff --git a/gorush/notification_test.go b/notify/notification_test.go similarity index 97% rename from gorush/notification_test.go rename to notify/notification_test.go index 7e77562..559a5bb 100644 --- a/gorush/notification_test.go +++ b/notify/notification_test.go @@ -1,4 +1,4 @@ -package gorush +package notify import ( "testing" diff --git a/queue/nsq/nsq.go b/queue/nsq/nsq.go index 393838b..2c08b29 100644 --- a/queue/nsq/nsq.go +++ b/queue/nsq/nsq.go @@ -6,7 +6,7 @@ import ( "sync" "time" - "github.com/appleboy/gorush/gorush" + "github.com/appleboy/gorush/notify" "github.com/appleboy/gorush/queue" "github.com/nsqio/go-nsq" @@ -77,11 +77,11 @@ func NewWorker(opts ...Option) *Worker { // In this case, a message with an empty body is simply ignored/discarded. return nil } - var notification *gorush.PushNotification + var notification *notify.PushNotification if err := json.Unmarshal(msg.Body, ¬ification); err != nil { return err } - gorush.SendNotification(notification) + notify.SendNotification(notification) return nil }, } diff --git a/queue/simple/simple.go b/queue/simple/simple.go index e14d2f8..027e42b 100644 --- a/queue/simple/simple.go +++ b/queue/simple/simple.go @@ -4,7 +4,7 @@ import ( "errors" "runtime" - "github.com/appleboy/gorush/gorush" + "github.com/appleboy/gorush/notify" "github.com/appleboy/gorush/queue" ) @@ -84,7 +84,7 @@ func NewWorker(opts ...Option) *Worker { w := &Worker{ queueNotification: make(chan queue.QueuedMessage, runtime.NumCPU()<<1), runFunc: func(msg queue.QueuedMessage) error { - gorush.SendNotification(msg) + notify.SendNotification(msg) return nil }, } diff --git a/queue/simple/simple_test.go b/queue/simple/simple_test.go index df3f2de..2d5f3c5 100644 --- a/queue/simple/simple_test.go +++ b/queue/simple/simple_test.go @@ -5,8 +5,8 @@ import ( "testing" "time" - "github.com/appleboy/gorush/gorush" "github.com/appleboy/gorush/logx" + "github.com/appleboy/gorush/notify" "github.com/appleboy/gorush/queue" "github.com/stretchr/testify/assert" @@ -29,7 +29,7 @@ func TestQueueUsage(t *testing.T) { assert.Equal(t, runtime.NumCPU()<<1, w.Capacity()) assert.Equal(t, 0, w.Usage()) - w.Queue(&gorush.PushNotification{}) + w.Queue(¬ify.PushNotification{}) assert.Equal(t, 1, w.Usage()) } @@ -38,14 +38,14 @@ func TestMaxCapacity(t *testing.T) { assert.Equal(t, 2, w.Capacity()) assert.Equal(t, 0, w.Usage()) - assert.NoError(t, w.Queue(&gorush.PushNotification{})) + assert.NoError(t, w.Queue(¬ify.PushNotification{})) assert.Equal(t, 1, w.Usage()) - assert.NoError(t, w.Queue(&gorush.PushNotification{})) + assert.NoError(t, w.Queue(¬ify.PushNotification{})) assert.Equal(t, 2, w.Usage()) - assert.Error(t, w.Queue(&gorush.PushNotification{})) + assert.Error(t, w.Queue(¬ify.PushNotification{})) assert.Equal(t, 2, w.Usage()) - err := w.Queue(&gorush.PushNotification{}) + err := w.Queue(¬ify.PushNotification{}) assert.Equal(t, errMaxCapacity, err) } diff --git a/router/server.go b/router/server.go index 8caf229..a918ed8 100644 --- a/router/server.go +++ b/router/server.go @@ -11,9 +11,9 @@ import ( "github.com/appleboy/gorush/config" "github.com/appleboy/gorush/core" - "github.com/appleboy/gorush/gorush" "github.com/appleboy/gorush/logx" "github.com/appleboy/gorush/metric" + "github.com/appleboy/gorush/notify" "github.com/appleboy/gorush/queue" "github.com/appleboy/gorush/status" @@ -65,7 +65,7 @@ func versionHandler(c *gin.Context) { func pushHandler(cfg config.ConfYaml, q *queue.Queue) gin.HandlerFunc { return func(c *gin.Context) { - var form gorush.RequestPush + var form notify.RequestPush var msg string if err := c.ShouldBindWith(&form, binding.JSON); err != nil { @@ -228,7 +228,7 @@ func routerEngine(cfg config.ConfYaml, q *queue.Queue) *gin.Engine { } // markFailedNotification adds failure logs for all tokens in push notification -func markFailedNotification(cfg config.ConfYaml, notification *gorush.PushNotification, reason string) { +func markFailedNotification(cfg config.ConfYaml, notification *notify.PushNotification, reason string) { logx.LogError.Error(reason) for _, token := range notification.Tokens { notification.AddLog(logx.GetLogPushEntry(&logx.InputLog{ @@ -246,10 +246,10 @@ func markFailedNotification(cfg config.ConfYaml, notification *gorush.PushNotifi } // HandleNotification add notification to queue list. -func handleNotification(ctx context.Context, cfg config.ConfYaml, req gorush.RequestPush, q *queue.Queue) (int, []logx.LogPushEntry) { +func handleNotification(ctx context.Context, cfg config.ConfYaml, req notify.RequestPush, q *queue.Queue) (int, []logx.LogPushEntry) { var count int wg := sync.WaitGroup{} - newNotification := []*gorush.PushNotification{} + newNotification := []*notify.PushNotification{} if cfg.Core.Sync && !core.IsLocalQueue(core.Queue(cfg.Queue.Engine)) { cfg.Core.Sync = false diff --git a/router/server_test.go b/router/server_test.go index 0aa0803..7487a2f 100644 --- a/router/server_test.go +++ b/router/server_test.go @@ -13,7 +13,7 @@ import ( "github.com/appleboy/gorush/config" "github.com/appleboy/gorush/core" - "github.com/appleboy/gorush/gorush" + "github.com/appleboy/gorush/notify" "github.com/appleboy/gorush/queue" "github.com/appleboy/gorush/queue/simple" "github.com/appleboy/gorush/status" @@ -291,7 +291,7 @@ func TestEmptyNotifications(t *testing.T) { // notifications is empty. r.POST("/api/push"). SetJSON(gofight.D{ - "notifications": []gorush.PushNotification{}, + "notifications": []notify.PushNotification{}, }). Run(routerEngine(cfg, q), func(r gofight.HTTPResponse, rq gofight.HTTPRequest) { assert.Equal(t, http.StatusBadRequest, r.Code) @@ -322,7 +322,7 @@ func TestMutableContent(t *testing.T) { }, }). Run(routerEngine(cfg, q), func(r gofight.HTTPResponse, rq gofight.HTTPRequest) { - // json: cannot unmarshal number into Go struct field gorush.PushNotification.mutable_content of type bool + // json: cannot unmarshal number into Go struct field notify.PushNotification.mutable_content of type bool assert.Equal(t, http.StatusBadRequest, r.Code) }) } @@ -457,7 +457,7 @@ func TestSenMultipleNotifications(t *testing.T) { cfg.Ios.Enabled = true cfg.Ios.KeyPath = "../certificate/certificate-valid.pem" - err := gorush.InitAPNSClient(cfg) + err := notify.InitAPNSClient(cfg) assert.Nil(t, err) cfg.Android.Enabled = true @@ -465,8 +465,8 @@ func TestSenMultipleNotifications(t *testing.T) { androidToken := os.Getenv("ANDROID_TEST_TOKEN") - req := gorush.RequestPush{ - Notifications: []gorush.PushNotification{ + req := notify.RequestPush{ + Notifications: []notify.PushNotification{ // ios { Tokens: []string{"11aa01229f15f0f0c52029d8cf8cd0aeaf2365fe4cebc4af26cd6d76b7919ef7"}, @@ -493,7 +493,7 @@ func TestDisabledAndroidNotifications(t *testing.T) { cfg.Ios.Enabled = true cfg.Ios.KeyPath = "../certificate/certificate-valid.pem" - err := gorush.InitAPNSClient(cfg) + err := notify.InitAPNSClient(cfg) assert.Nil(t, err) cfg.Android.Enabled = false @@ -501,8 +501,8 @@ func TestDisabledAndroidNotifications(t *testing.T) { androidToken := os.Getenv("ANDROID_TEST_TOKEN") - req := gorush.RequestPush{ - Notifications: []gorush.PushNotification{ + req := notify.RequestPush{ + Notifications: []notify.PushNotification{ // ios { Tokens: []string{"11aa01229f15f0f0c5209d8cf8cd0aeaf2365fe4cebc4af26cd6d76b7919ef7"}, @@ -529,7 +529,7 @@ func TestSyncModeForNotifications(t *testing.T) { cfg.Ios.Enabled = true cfg.Ios.KeyPath = "../certificate/certificate-valid.pem" - err := gorush.InitAPNSClient(cfg) + err := notify.InitAPNSClient(cfg) assert.Nil(t, err) cfg.Android.Enabled = true @@ -540,8 +540,8 @@ func TestSyncModeForNotifications(t *testing.T) { androidToken := os.Getenv("ANDROID_TEST_TOKEN") - req := gorush.RequestPush{ - Notifications: []gorush.PushNotification{ + req := notify.RequestPush{ + Notifications: []notify.PushNotification{ // ios { Tokens: []string{ @@ -575,8 +575,8 @@ func TestSyncModeForTopicNotification(t *testing.T) { // enable sync mode cfg.Core.Sync = true - req := gorush.RequestPush{ - Notifications: []gorush.PushNotification{ + req := notify.RequestPush{ + Notifications: []notify.PushNotification{ // android { // error:InvalidParameters @@ -619,8 +619,8 @@ func TestSyncModeForDeviceGroupNotification(t *testing.T) { // enable sync mode cfg.Core.Sync = true - req := gorush.RequestPush{ - Notifications: []gorush.PushNotification{ + req := notify.RequestPush{ + Notifications: []notify.PushNotification{ // android { To: "aUniqueKey", @@ -641,7 +641,7 @@ func TestDisabledIosNotifications(t *testing.T) { cfg.Ios.Enabled = false cfg.Ios.KeyPath = "../certificate/certificate-valid.pem" - err := gorush.InitAPNSClient(cfg) + err := notify.InitAPNSClient(cfg) assert.Nil(t, err) cfg.Android.Enabled = true @@ -649,8 +649,8 @@ func TestDisabledIosNotifications(t *testing.T) { androidToken := os.Getenv("ANDROID_TEST_TOKEN") - req := gorush.RequestPush{ - Notifications: []gorush.PushNotification{ + req := notify.RequestPush{ + Notifications: []notify.PushNotification{ // ios { Tokens: []string{"11aa01229f15f0f0c52021d8cf3cd0ae1f2365fe4cebc4af26cd6d76b7919ef7"}, diff --git a/rpc/server.go b/rpc/server.go index af76fda..821ebc5 100644 --- a/rpc/server.go +++ b/rpc/server.go @@ -8,8 +8,8 @@ import ( "github.com/appleboy/gorush/config" "github.com/appleboy/gorush/core" - "github.com/appleboy/gorush/gorush" "github.com/appleboy/gorush/logx" + "github.com/appleboy/gorush/notify" "github.com/appleboy/gorush/rpc/proto" "google.golang.org/grpc" @@ -55,7 +55,7 @@ func (s *Server) Check(ctx context.Context, in *proto.HealthCheckRequest) (*prot // Send implements helloworld.GreeterServer func (s *Server) Send(ctx context.Context, in *proto.NotificationRequest) (*proto.NotificationReply, error) { badge := int(in.Badge) - notification := gorush.PushNotification{ + notification := notify.PushNotification{ Cfg: s.cfg, Platform: int(in.Platform), Tokens: in.Tokens, @@ -81,7 +81,7 @@ func (s *Server) Send(ctx context.Context, in *proto.NotificationRequest) (*prot } if in.Alert != nil { - notification.Alert = gorush.Alert{ + notification.Alert = notify.Alert{ Title: in.Alert.Title, Body: in.Alert.Body, Subtitle: in.Alert.Subtitle, @@ -102,7 +102,7 @@ func (s *Server) Send(ctx context.Context, in *proto.NotificationRequest) (*prot } } - go gorush.SendNotification(¬ification) + go notify.SendNotification(¬ification) return &proto.NotificationReply{ Success: true,