Skip to content

Commit

Permalink
perf: 优化PushService接口定义
Browse files Browse the repository at this point in the history
  • Loading branch information
am6737 committed Mar 7, 2024
1 parent 2be0de9 commit a4e6ddb
Show file tree
Hide file tree
Showing 35 changed files with 688 additions and 339 deletions.
2 changes: 1 addition & 1 deletion api/http/v1/dto/dto.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package dto

import (
"github.com/cossim/hipush/internal/notify"
"github.com/cossim/hipush/notify"
"time"
)

Expand Down
80 changes: 40 additions & 40 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import (
"github.com/cossim/hipush/internal/adapter"
"github.com/cossim/hipush/internal/consts"
"github.com/cossim/hipush/internal/factory"
"github.com/cossim/hipush/internal/push"
g "github.com/cossim/hipush/internal/server/grpc"
h "github.com/cossim/hipush/internal/server/http"
"github.com/cossim/hipush/push"
"github.com/go-logr/zapr"
"go.uber.org/zap"
"log"
Expand Down Expand Up @@ -57,45 +57,45 @@ func main() {
return adapter.NewPushServiceAdapter(svc)
})

//pushServiceFactory.Register(consts.PlatformHuawei.String(), func() push.PushService {
// svc, err := push.NewHMSService(cfg)
// if err != nil {
// panic(err)
// }
// return adapter.NewPushServiceAdapter(svc)
//})
//
//pushServiceFactory.Register(consts.PlatformVivo.String(), func() push.PushService {
// svc, err := push.NewVivoService(cfg)
// if err != nil {
// panic(err)
// }
// return adapter.NewPushServiceAdapter(svc)
//})
//
//pushServiceFactory.Register(consts.PlatformOppo.String(), func() push.PushService {
// svc, err := push.NewOppoService(cfg)
// if err != nil {
// panic(err)
// }
// return adapter.NewPushServiceAdapter(svc)
//})
//
//pushServiceFactory.Register(consts.PlatformXiaomi.String(), func() push.PushService {
// svc, err := push.NewXiaomiService(cfg)
// if err != nil {
// panic(err)
// }
// return adapter.NewPushServiceAdapter(svc)
//})
//
//pushServiceFactory.Register(consts.PlatformMeizu.String(), func() push.PushService {
// svc, err := push.NewMeizuService(cfg)
// if err != nil {
// panic(err)
// }
// return adapter.NewPushServiceAdapter(svc)
//})
pushServiceFactory.Register(consts.PlatformHuawei.String(), func() push.PushService {
svc, err := push.NewHMSService(cfg)
if err != nil {
panic(err)
}
return adapter.NewPushServiceAdapter(svc)
})

pushServiceFactory.Register(consts.PlatformVivo.String(), func() push.PushService {
svc, err := push.NewVivoService(cfg)
if err != nil {
panic(err)
}
return adapter.NewPushServiceAdapter(svc)
})

pushServiceFactory.Register(consts.PlatformOppo.String(), func() push.PushService {
svc, err := push.NewOppoService(cfg)
if err != nil {
panic(err)
}
return adapter.NewPushServiceAdapter(svc)
})

pushServiceFactory.Register(consts.PlatformXiaomi.String(), func() push.PushService {
svc, err := push.NewXiaomiService(cfg)
if err != nil {
panic(err)
}
return adapter.NewPushServiceAdapter(svc)
})

pushServiceFactory.Register(consts.PlatformMeizu.String(), func() push.PushService {
svc, err := push.NewMeizuService(cfg)
if err != nil {
panic(err)
}
return adapter.NewPushServiceAdapter(svc)
})

ctx, cancel := context.WithCancel(context.Background())
defer cancel()
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/cossim/hipush

go 1.22
go 1.22.0

require (
firebase.google.com/go v3.13.0+incompatible
Expand All @@ -18,7 +18,6 @@ require (
github.com/sideshow/apns2 v0.23.0
github.com/yilee/xiaomi-push v0.0.0-20170213073944-562fbb07388e
go.uber.org/zap v1.27.0
golang.org/x/net v0.21.0
google.golang.org/api v0.168.0
google.golang.org/grpc v1.62.0
google.golang.org/protobuf v1.32.0
Expand Down Expand Up @@ -71,6 +70,7 @@ require (
go.uber.org/multierr v1.10.0 // indirect
golang.org/x/arch v0.3.0 // indirect
golang.org/x/crypto v0.19.0 // indirect
golang.org/x/net v0.21.0 // indirect
golang.org/x/oauth2 v0.17.0 // indirect
golang.org/x/sync v0.6.0 // indirect
golang.org/x/sys v0.17.0 // indirect
Expand Down
42 changes: 22 additions & 20 deletions internal/adapter/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,47 @@ package adapter

import (
"context"
"github.com/cossim/hipush/internal/push"
"github.com/cossim/hipush/push"
)

type PushServiceAdapter struct {
pushService push.PushService
}

func NewPushServiceAdapter(pushService push.PushService) *PushServiceAdapter {
return &PushServiceAdapter{pushService: pushService}
}

func (p *PushServiceAdapter) Send(ctx context.Context, req interface{}, opt push.SendOption) error {
return p.pushService.Send(ctx, req, opt)
type PushServiceAdapter struct {
pushService push.PushService
}

func (p *PushServiceAdapter) MulticastSend(ctx context.Context, req interface{}) error {
return p.pushService.MulticastSend(ctx, req)
func (p *PushServiceAdapter) Send(ctx context.Context, req interface{}, opt ...push.SendOption) error {
return p.pushService.Send(ctx, req, opt...)
}

func (p *PushServiceAdapter) Subscribe(ctx context.Context, req interface{}) error {
return p.pushService.Subscribe(ctx, req)
func (p *PushServiceAdapter) SendMulticast(ctx context.Context, req interface{}, opt ...push.MulticastOption) error {
//TODO implement me
panic("implement me")
}

func (p *PushServiceAdapter) Unsubscribe(ctx context.Context, req interface{}) error {
return p.pushService.Unsubscribe(ctx, req)
func (p *PushServiceAdapter) Subscribe(ctx context.Context, req interface{}, opt ...push.SubscribeOption) error {
//TODO implement me
panic("implement me")
}

func (p *PushServiceAdapter) SendToTopic(ctx context.Context, req interface{}) error {
return p.pushService.SendToTopic(ctx, req)
func (p *PushServiceAdapter) Unsubscribe(ctx context.Context, req interface{}, opt ...push.UnsubscribeOption) error {
//TODO implement me
panic("implement me")
}

func (p *PushServiceAdapter) SendToCondition(ctx context.Context, req interface{}) error {
return p.pushService.SendToCondition(ctx, req)
func (p *PushServiceAdapter) SendToTopic(ctx context.Context, req interface{}, opt ...push.TopicOption) error {
//TODO implement me
panic("implement me")
}

func (p *PushServiceAdapter) CheckDevice(ctx context.Context, req interface{}) bool {
return p.pushService.CheckDevice(ctx, req)
func (p *PushServiceAdapter) CheckDevice(ctx context.Context, req interface{}, opt ...push.CheckDeviceOption) bool {
//TODO implement me
panic("implement me")
}

func (p *PushServiceAdapter) Name() string {
return p.pushService.Name()
//TODO implement me
panic("implement me")
}
2 changes: 1 addition & 1 deletion internal/factory/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package factory

import (
"errors"
"github.com/cossim/hipush/internal/push"
"github.com/cossim/hipush/push"
)

type PushServiceCreator func() push.PushService
Expand Down
27 changes: 0 additions & 27 deletions internal/notify/hms_notify.go

This file was deleted.

8 changes: 4 additions & 4 deletions internal/server/grpc/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"github.com/cossim/hipush/config"
"github.com/cossim/hipush/internal/consts"
"github.com/cossim/hipush/internal/factory"
"github.com/cossim/hipush/internal/notify"
"github.com/cossim/hipush/internal/push"
"github.com/cossim/hipush/notify"
"github.com/cossim/hipush/push"
"github.com/go-logr/logr"
"github.com/golang/protobuf/jsonpb"
"google.golang.org/grpc"
Expand Down Expand Up @@ -81,7 +81,7 @@ func (h *Handler) Push(ctx context.Context, req *v1.PushRequest) (*v1.PushRespon
return nil, err
}

if err := service.Send(ctx, r, push.SendOption{
if err := service.Send(ctx, r, &push.SendOptions{
DryRun: req.Option.DryRun,
Retry: int(req.Option.Retry),
}); err != nil {
Expand Down Expand Up @@ -127,7 +127,7 @@ func (h *Handler) getPushRequest(req *v1.PushRequest) (push.PushRequest, error)
ApnsID: req.AppID,
Tokens: req.Tokens,
Title: req.Title,
Message: req.Message,
Content: req.Message,
Topic: req.Topic,
Category: req.Category,
Sound: req.Sound,
Expand Down
6 changes: 3 additions & 3 deletions internal/server/http/android.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package http
import (
"encoding/json"
"github.com/cossim/hipush/api/http/v1/dto"
"github.com/cossim/hipush/internal/notify"
"github.com/cossim/hipush/internal/push"
"github.com/cossim/hipush/notify"
"github.com/cossim/hipush/push"
"github.com/gin-gonic/gin"
"net/http"
)
Expand Down Expand Up @@ -53,7 +53,7 @@ func (h *Handler) handleAndroidPush(c *gin.Context, req *dto.PushRequest) {
Data: nil,
Apns: nil,
}
if err := service.Send(c, rr, push.SendOption{}); err != nil {
if err := service.Send(c, rr, &push.SendOptions{}); err != nil {
h.logger.Error(err, "Failed to send push notification")
c.JSON(http.StatusInternalServerError, Response{Code: http.StatusBadRequest, Msg: err.Error(), Data: nil})
return
Expand Down
8 changes: 4 additions & 4 deletions internal/server/http/apns.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package http
import (
"encoding/json"
"github.com/cossim/hipush/api/http/v1/dto"
"github.com/cossim/hipush/internal/notify"
"github.com/cossim/hipush/internal/push"
"github.com/cossim/hipush/notify"
"github.com/cossim/hipush/push"
"github.com/gin-gonic/gin"
"net/http"
)
Expand Down Expand Up @@ -37,7 +37,7 @@ func (h *Handler) handleIOSPush(c *gin.Context, req *dto.PushRequest) {
Tokens: req.Token,
Title: r.Title,
Topic: req.AppID,
Message: r.Message,
Content: r.Message,
ApnsID: req.AppID,
Sound: r.Sound,
Production: r.Production,
Expand All @@ -49,7 +49,7 @@ func (h *Handler) handleIOSPush(c *gin.Context, req *dto.PushRequest) {
Data: r.Data,
Expiration: nil,
}
if err := service.Send(c, rr, push.SendOption{}); err != nil {
if err := service.Send(c, rr, &push.SendOptions{}); err != nil {
h.logger.Error(err, "Failed to send push notification")
c.JSON(http.StatusInternalServerError, Response{Code: http.StatusBadRequest, Msg: err.Error(), Data: nil})
return
Expand Down
6 changes: 3 additions & 3 deletions internal/server/http/huawei.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"github.com/cossim/go-hms-push/push/model"
"github.com/cossim/hipush/api/http/v1/dto"
"github.com/cossim/hipush/internal/consts"
"github.com/cossim/hipush/internal/notify"
"github.com/cossim/hipush/internal/push"
"github.com/cossim/hipush/notify"
"github.com/cossim/hipush/push"
"github.com/gin-gonic/gin"
"net/http"
)
Expand Down Expand Up @@ -53,7 +53,7 @@ func (h *Handler) handleHuaweiPush(c *gin.Context, req *dto.PushRequest) {
},
},
}
if err := service.Send(c, rr, push.SendOption{}); err != nil {
if err := service.Send(c, rr, &push.SendOptions{}); err != nil {
c.JSON(http.StatusInternalServerError, Response{Code: http.StatusBadRequest, Msg: err.Error(), Data: nil})
return
}
Expand Down
6 changes: 3 additions & 3 deletions internal/server/http/meizu.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"encoding/json"
"github.com/cossim/hipush/api/http/v1/dto"
"github.com/cossim/hipush/internal/consts"
"github.com/cossim/hipush/internal/notify"
"github.com/cossim/hipush/internal/push"
"github.com/cossim/hipush/notify"
"github.com/cossim/hipush/push"
"github.com/gin-gonic/gin"
"net/http"
)
Expand Down Expand Up @@ -51,7 +51,7 @@ func (h *Handler) handleMeizuPush(c *gin.Context, req *dto.PushRequest) {
ScheduledStartTime: r.ScheduledStartTime,
ScheduledEndTime: r.ScheduledEndTime,
}
if err := service.Send(c, rr, push.SendOption{
if err := service.Send(c, rr, &push.SendOptions{
DryRun: req.Option.DryRun,
Retry: req.Option.Retry,
}); err != nil {
Expand Down
6 changes: 3 additions & 3 deletions internal/server/http/oppo.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"encoding/json"
"github.com/cossim/hipush/api/http/v1/dto"
"github.com/cossim/hipush/internal/consts"
"github.com/cossim/hipush/internal/notify"
"github.com/cossim/hipush/internal/push"
"github.com/cossim/hipush/notify"
"github.com/cossim/hipush/push"
"github.com/gin-gonic/gin"
"net/http"
)
Expand Down Expand Up @@ -44,7 +44,7 @@ func (h *Handler) handleOppoPush(c *gin.Context, req *dto.PushRequest) {
Retry: req.Option.Retry,
},
}
if err := service.Send(c, rr, push.SendOption{}); err != nil {
if err := service.Send(c, rr, &push.SendOptions{}); err != nil {
c.JSON(http.StatusInternalServerError, Response{Code: http.StatusBadRequest, Msg: err.Error(), Data: nil})
return
}
Expand Down
6 changes: 3 additions & 3 deletions internal/server/http/vivo.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"encoding/json"
"github.com/cossim/hipush/api/http/v1/dto"
"github.com/cossim/hipush/internal/consts"
"github.com/cossim/hipush/internal/notify"
"github.com/cossim/hipush/internal/push"
"github.com/cossim/hipush/notify"
"github.com/cossim/hipush/push"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
"net/http"
Expand Down Expand Up @@ -49,7 +49,7 @@ func (h *Handler) handleVivoPush(c *gin.Context, req *dto.PushRequest) {
Foreground: r.Foreground,
Development: true,
}
if err := service.Send(c, rr, push.SendOption{}); err != nil {
if err := service.Send(c, rr, &push.SendOptions{}); err != nil {
c.JSON(http.StatusInternalServerError, Response{Code: http.StatusBadRequest, Msg: err.Error(), Data: nil})
return
}
Expand Down
Loading

0 comments on commit a4e6ddb

Please sign in to comment.