diff --git a/.drone.yml b/.drone.yml index f11451c..b0d2452 100644 --- a/.drone.yml +++ b/.drone.yml @@ -7,29 +7,11 @@ platform: arch: amd64 steps: -- name: vet - pull: always - image: golang:1.16 - commands: - - make vet - volumes: - - name: gopath - path: /go - - name: lint pull: always - image: golang:1.16 + image: golangci/golangci-lint:v1.41.1 commands: - - make lint - volumes: - - name: gopath - path: /go - -- name: misspell - pull: always - image: golang:1.16 - commands: - - make misspell-check + - golangci-lint run -v volumes: - name: gopath path: /go @@ -127,19 +109,6 @@ steps: commands: - ./release/linux/amd64/gorush --help -- name: dryrun - pull: always - image: plugins/docker:linux-amd64 - settings: - cache_from: appleboy/gorush - dockerfile: docker/Dockerfile.linux.amd64 - dry_run: true - repo: appleboy/gorush - tags: linux-amd64 - when: - event: - - pull_request - - name: publish pull: always image: plugins/docker:linux-amd64 @@ -206,19 +175,6 @@ steps: commands: - ./release/linux/arm64/gorush --help -- name: dryrun - pull: always - image: plugins/docker:linux-arm64 - settings: - cache_from: appleboy/gorush - dockerfile: docker/Dockerfile.linux.arm64 - dry_run: true - repo: appleboy/gorush - tags: linux-arm64 - when: - event: - - pull_request - - name: publish pull: always image: plugins/docker:linux-arm64 @@ -285,19 +241,6 @@ steps: commands: - ./release/linux/arm/gorush --help -- name: dryrun - pull: always - image: plugins/docker:linux-arm - settings: - cache_from: appleboy/gorush - dockerfile: docker/Dockerfile.linux.arm - dry_run: true - repo: appleboy/gorush - tags: linux-arm - when: - event: - - pull_request - - name: publish pull: always image: plugins/docker:linux-arm diff --git a/logx/log.go b/logx/log.go index ccda24d..056ac5b 100644 --- a/logx/log.go +++ b/logx/log.go @@ -14,14 +14,11 @@ import ( ) var ( - green = string([]byte{27, 91, 57, 55, 59, 52, 50, 109}) - white = string([]byte{27, 91, 57, 48, 59, 52, 55, 109}) - yellow = string([]byte{27, 91, 57, 55, 59, 52, 51, 109}) - red = string([]byte{27, 91, 57, 55, 59, 52, 49, 109}) - blue = string([]byte{27, 91, 57, 55, 59, 52, 52, 109}) - magenta = string([]byte{27, 91, 57, 55, 59, 52, 53, 109}) - cyan = string([]byte{27, 91, 57, 55, 59, 52, 54, 109}) - reset = string([]byte{27, 91, 48, 109}) + green = string([]byte{27, 91, 57, 55, 59, 52, 50, 109}) + yellow = string([]byte{27, 91, 57, 55, 59, 52, 51, 109}) + red = string([]byte{27, 91, 57, 55, 59, 52, 49, 109}) + blue = string([]byte{27, 91, 57, 55, 59, 52, 52, 109}) + reset = string([]byte{27, 91, 48, 109}) ) // LogPushEntry is push response log diff --git a/main.go b/main.go index 726f658..376c6fd 100644 --- a/main.go +++ b/main.go @@ -219,7 +219,9 @@ func main() { return } - notify.PushToAndroid(req, cfg) + if _, err := notify.PushToAndroid(req, cfg); err != nil { + return + } return } @@ -252,7 +254,9 @@ func main() { return } - notify.PushToHuawei(req, cfg) + if _, err := notify.PushToHuawei(req, cfg); err != nil { + return + } return } @@ -292,7 +296,10 @@ func main() { if err := notify.InitAPNSClient(cfg); err != nil { return } - notify.PushToIOS(req, cfg) + + if _, err := notify.PushToIOS(req, cfg); err != nil { + return + } return } @@ -397,9 +404,7 @@ func main() { // check job completely g.Go(func() error { - select { - case <-finished: - } + <-finished return nil }) diff --git a/notify/main_test.go b/notify/main_test.go index 96b219e..899ef0b 100644 --- a/notify/main_test.go +++ b/notify/main_test.go @@ -2,6 +2,7 @@ package notify import ( "log" + "os" "testing" "github.com/appleboy/gorush/config" @@ -14,5 +15,5 @@ func TestMain(m *testing.M) { log.Fatal(err) } - m.Run() + os.Exit(m.Run()) } diff --git a/notify/notification_apns_test.go b/notify/notification_apns_test.go index 6f87270..21b4a61 100644 --- a/notify/notification_apns_test.go +++ b/notify/notification_apns_test.go @@ -96,8 +96,8 @@ func TestIOSNotificationStructure(t *testing.T) { soundVolume, _ := jsonparser.GetFloat(data, "aps", "sound", "volume") contentAvailable, _ := jsonparser.GetInt(data, "aps", "content-available") category, _ := jsonparser.GetString(data, "aps", "category") - key1 := dat["key1"].(interface{}) - key2 := dat["key2"].(interface{}) + key1 := dat["key1"] + key2 := dat["key2"] aps := dat["aps"].(map[string]interface{}) urlArgs := aps["url-args"].([]interface{}) @@ -732,7 +732,9 @@ func TestPushToIOS(t *testing.T) { } // send fail - PushToIOS(req, cfg) + resp, err := PushToIOS(req, cfg) + assert.Nil(t, err) + assert.Len(t, resp.Logs, 2) } func TestApnsHostFromRequest(t *testing.T) { diff --git a/notify/notification_fcm_test.go b/notify/notification_fcm_test.go index 4d300be..f07c337 100644 --- a/notify/notification_fcm_test.go +++ b/notify/notification_fcm_test.go @@ -46,7 +46,9 @@ func TestPushToAndroidWrongToken(t *testing.T) { } // Android Success count: 0, Failure count: 2 - PushToAndroid(req, cfg) + resp, err := PushToAndroid(req, cfg) + assert.Nil(t, err) + assert.Len(t, resp.Logs, 2) } func TestPushToAndroidRightTokenForJSONLog(t *testing.T) { @@ -65,7 +67,9 @@ func TestPushToAndroidRightTokenForJSONLog(t *testing.T) { Message: "Welcome", } - PushToAndroid(req, cfg) + resp, err := PushToAndroid(req, cfg) + assert.Nil(t, err) + assert.Len(t, resp.Logs, 0) } func TestPushToAndroidRightTokenForStringLog(t *testing.T) { @@ -82,7 +86,9 @@ func TestPushToAndroidRightTokenForStringLog(t *testing.T) { Message: "Welcome", } - PushToAndroid(req, cfg) + resp, err := PushToAndroid(req, cfg) + assert.Nil(t, err) + assert.Len(t, resp.Logs, 0) } func TestOverwriteAndroidAPIKey(t *testing.T) { @@ -200,7 +206,10 @@ func TestCheckAndroidMessage(t *testing.T) { TimeToLive: &timeToLive, } - PushToAndroid(req, cfg) + // the message's TimeToLive field must be an integer between 0 and 2419200 (4 weeks) + resp, err := PushToAndroid(req, cfg) + assert.NotNil(t, err) + assert.Nil(t, resp) } func TestAndroidNotificationStructure(t *testing.T) { diff --git a/pipeline.libsonnet b/pipeline.libsonnet index 03278ab..0f8b5de 100644 --- a/pipeline.libsonnet +++ b/pipeline.libsonnet @@ -7,40 +7,12 @@ arch: 'amd64', }, steps: [ - { - name: 'vet', - image: 'golang:1.16', - pull: 'always', - commands: [ - 'make vet', - ], - volumes: [ - { - name: 'gopath', - path: '/go', - }, - ], - }, { name: 'lint', - image: 'golang:1.16', + image: 'golangci/golangci-lint:v1.41.1', pull: 'always', commands: [ - 'make lint', - ], - volumes: [ - { - name: 'gopath', - path: '/go', - }, - ], - }, - { - name: 'misspell', - image: 'golang:1.16', - pull: 'always', - commands: [ - 'make misspell-check', + 'golangci-lint run -v', ], volumes: [ { @@ -190,22 +162,6 @@ './release/' + os + '/' + arch + '/' + name + ' --help', ], }, - { - name: 'dryrun', - image: 'plugins/docker:' + os + '-' + arch, - pull: 'always', - settings: { - daemon_off: false, - dry_run: true, - tags: os + '-' + arch, - dockerfile: 'docker/Dockerfile.' + os + '.' + arch, - repo: 'appleboy/' + name, - cache_from: 'appleboy/' + name, - }, - when: { - event: [ 'pull_request' ], - }, - }, { name: 'publish', image: 'plugins/docker:' + os + '-' + arch, diff --git a/router/server.go b/router/server.go index fd68ef8..bd3529d 100644 --- a/router/server.go +++ b/router/server.go @@ -284,7 +284,7 @@ func handleNotification(ctx context.Context, cfg *config.ConfYaml, req notify.Re if core.IsLocalQueue(core.Queue(cfg.Queue.Engine)) && cfg.Core.Sync { func(msg *notify.PushNotification, cfg *config.ConfYaml) { - q.QueueTask(func(ctx context.Context) error { + if err := q.QueueTask(func(ctx context.Context) error { defer wg.Done() resp, err := notify.SendNotification(msg, cfg) if err != nil { @@ -292,19 +292,17 @@ func handleNotification(ctx context.Context, cfg *config.ConfYaml, req notify.Re } // add log - for _, v := range resp.Logs { - logs = append(logs, v) - } + logs = append(logs, resp.Logs...) return nil - }) + }); err != nil { + logx.LogError.Error(err) + } }(notification, cfg) } else if err := q.Queue(notification); err != nil { resp := markFailedNotification(cfg, notification, "max capacity reached") // add log - for _, v := range resp { - logs = append(logs, v) - } + logs = append(logs, resp...) wg.Done() } diff --git a/router/server_normal.go b/router/server_normal.go index ada245a..14c6969 100644 --- a/router/server_normal.go +++ b/router/server_normal.go @@ -82,13 +82,11 @@ func RunHTTPServer(ctx context.Context, cfg *config.ConfYaml, q *queue.Queue, s func listenAndServe(ctx context.Context, s *http.Server, cfg *config.ConfYaml) error { var g errgroup.Group g.Go(func() error { - select { - case <-ctx.Done(): - timeout := time.Duration(cfg.Core.ShutdownTimeout) * time.Second - ctx, cancel := context.WithTimeout(context.Background(), timeout) - defer cancel() - return s.Shutdown(ctx) - } + <-ctx.Done() + timeout := time.Duration(cfg.Core.ShutdownTimeout) * time.Second + ctx, cancel := context.WithTimeout(context.Background(), timeout) + defer cancel() + return s.Shutdown(ctx) }) g.Go(func() error { if err := s.ListenAndServe(); err != nil && err != http.ErrServerClosed { @@ -102,13 +100,11 @@ func listenAndServe(ctx context.Context, s *http.Server, cfg *config.ConfYaml) e func listenAndServeTLS(ctx context.Context, s *http.Server, cfg *config.ConfYaml) error { var g errgroup.Group g.Go(func() error { - select { - case <-ctx.Done(): - timeout := time.Duration(cfg.Core.ShutdownTimeout) * time.Second - ctx, cancel := context.WithTimeout(context.Background(), timeout) - defer cancel() - return s.Shutdown(ctx) - } + <-ctx.Done() + timeout := time.Duration(cfg.Core.ShutdownTimeout) * time.Second + ctx, cancel := context.WithTimeout(context.Background(), timeout) + defer cancel() + return s.Shutdown(ctx) }) g.Go(func() error { if err := s.ListenAndServeTLS("", ""); err != nil && err != http.ErrServerClosed { diff --git a/router/server_test.go b/router/server_test.go index ab87214..aae869d 100644 --- a/router/server_test.go +++ b/router/server_test.go @@ -59,7 +59,7 @@ func TestMain(m *testing.M) { q.Wait() }() - m.Run() + os.Exit(m.Run()) } func initTest() *config.ConfYaml { @@ -232,7 +232,6 @@ func TestRootHandler(t *testing.T) { assert.Equal(t, "Welcome to notification server.", value) assert.Equal(t, http.StatusOK, r.Code) - assert.Equal(t, "application/json; charset=utf-8", r.HeaderMap.Get("Content-Type")) }) } @@ -291,7 +290,6 @@ func TestMissingNotificationsParameter(t *testing.T) { r.POST("/api/push"). Run(routerEngine(cfg, q), func(r gofight.HTTPResponse, rq gofight.HTTPRequest) { assert.Equal(t, http.StatusBadRequest, r.Code) - assert.Equal(t, "application/json; charset=utf-8", r.HeaderMap.Get("Content-Type")) }) } diff --git a/rpc/client_grpc_health.go b/rpc/client_grpc_health.go index 98c4980..39fce02 100644 --- a/rpc/client_grpc_health.go +++ b/rpc/client_grpc_health.go @@ -7,6 +7,7 @@ import ( "google.golang.org/grpc" "google.golang.org/grpc/codes" + "google.golang.org/grpc/status" ) // generate protobuffs @@ -41,7 +42,7 @@ func (c *healthClient) Check(ctx context.Context) (bool, error) { } return false, nil } - switch grpc.Code(err) { + switch status.Code(err) { case codes.Aborted, codes.DataLoss, diff --git a/rpc/example/go/health/main.go b/rpc/example/go/health/main.go index 323b8a4..91d66f2 100644 --- a/rpc/example/go/health/main.go +++ b/rpc/example/go/health/main.go @@ -8,6 +8,7 @@ import ( "github.com/appleboy/gorush/rpc" "google.golang.org/grpc" + "google.golang.org/grpc/status" ) const ( @@ -27,7 +28,7 @@ func main() { for { ok, err := client.Check(context.Background()) if !ok || err != nil { - log.Printf("can't connect grpc server: %v, code: %v\n", err, grpc.Code(err)) + log.Printf("can't connect grpc server: %v, code: %v\n", err, status.Code(err)) } else { log.Println("connect the grpc server successfully") } diff --git a/rpc/server.go b/rpc/server.go index a0d68c8..d688315 100644 --- a/rpc/server.go +++ b/rpc/server.go @@ -101,7 +101,12 @@ func (s *Server) Send(ctx context.Context, in *proto.NotificationRequest) (*prot } } - go notify.SendNotification(¬ification, s.cfg) + go func() { + _, err := notify.SendNotification(¬ification, s.cfg) + if err != nil { + logx.LogError.Error(err) + } + }() return &proto.NotificationReply{ Success: true, @@ -131,11 +136,9 @@ func RunGRPCServer(ctx context.Context, cfg *config.ConfYaml) error { } logx.LogAccess.Info("gRPC server is running on " + cfg.GRPC.Port + " port.") go func() { - select { - case <-ctx.Done(): - s.GracefulStop() // graceful shutdown - logx.LogAccess.Info("shutdown the gRPC server") - } + <-ctx.Done() + s.GracefulStop() // graceful shutdown + logx.LogAccess.Info("shutdown the gRPC server") }() if err = s.Serve(lis); err != nil { logx.LogError.Fatalln(err) diff --git a/status/status_test.go b/status/status_test.go index 617001b..b057f43 100644 --- a/status/status_test.go +++ b/status/status_test.go @@ -1,6 +1,7 @@ package status import ( + "os" "testing" "time" @@ -10,7 +11,7 @@ import ( ) func TestMain(m *testing.M) { - m.Run() + os.Exit(m.Run()) } func TestStorageDriverExist(t *testing.T) { @@ -76,7 +77,7 @@ func TestStatForRedisEngine(t *testing.T) { err := InitAppStatus(cfg) assert.Nil(t, err) - StatStorage.Init() + assert.Nil(t, StatStorage.Init()) StatStorage.Reset() StatStorage.AddTotalCount(100) diff --git a/storage/badger/badger.go b/storage/badger/badger.go index 72a1fee..b0b02d1 100644 --- a/storage/badger/badger.go +++ b/storage/badger/badger.go @@ -1,7 +1,6 @@ package badger import ( - "fmt" "log" "os" "strconv" @@ -84,7 +83,7 @@ func (s *Storage) getBadger(key string, count *int64) { return err } - i, err := strconv.ParseInt(fmt.Sprintf("%s", val), 10, 64) + i, err := strconv.ParseInt(string(val), 10, 64) if err != nil { return err }