From 126479539a91023d70c5abbd4c42453f04d25cd9 Mon Sep 17 00:00:00 2001 From: lms <1359816810@qq.com> Date: Mon, 11 Mar 2024 11:40:08 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E9=87=8D=E6=9E=84factory=E6=B3=A8?= =?UTF-8?q?=E5=86=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/main.go | 95 +++++++++++------------------------- factory/factory.go | 47 ++++++++++++++++++ internal/adapter/adapter.go | 48 ------------------ internal/factory/factory.go | 30 ------------ internal/server/grpc/grpc.go | 2 +- internal/server/http/http.go | 2 +- push/apns_push.go | 29 +---------- push/fcm_push.go | 29 +---------- push/honor_push.go | 29 +---------- push/huawei_push.go | 29 +---------- push/meizu_push.go | 29 +---------- push/oppo_push.go | 29 +---------- push/push.go | 17 +------ push/vivo_push.go | 29 +---------- push/xiaomi_push.go | 29 +---------- 15 files changed, 95 insertions(+), 378 deletions(-) create mode 100644 factory/factory.go delete mode 100644 internal/adapter/adapter.go delete mode 100644 internal/factory/factory.go diff --git a/cmd/main.go b/cmd/main.go index 67efc56..073e9c2 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -4,9 +4,7 @@ import ( "context" "flag" "github.com/cossim/hipush/config" - "github.com/cossim/hipush/consts" - "github.com/cossim/hipush/internal/adapter" - "github.com/cossim/hipush/internal/factory" + "github.com/cossim/hipush/factory" g "github.com/cossim/hipush/internal/server/grpc" h "github.com/cossim/hipush/internal/server/http" "github.com/cossim/hipush/push" @@ -49,69 +47,34 @@ func main() { logger := zapr.NewLogger(zapLogger) pushServiceFactory := factory.NewPushServiceFactory() - pushServiceFactory.Register(consts.PlatformIOS.String(), func() push.PushService { - svc, err := push.NewAPNsService(cfg, logger) - if err != nil { - panic(err) - } - return adapter.NewPushServiceAdapter(svc) - }) - - pushServiceFactory.Register(consts.PlatformAndroid.String(), func() push.PushService { - svc, err := push.NewFCMService(cfg, logger) - if err != nil { - panic(err) - } - return adapter.NewPushServiceAdapter(svc) - }) - - pushServiceFactory.Register(consts.PlatformHuawei.String(), func() push.PushService { - svc, err := push.NewHMSService(cfg, logger) - if err != nil { - panic(err) - } - return adapter.NewPushServiceAdapter(svc) - }) - - pushServiceFactory.Register(consts.PlatformVivo.String(), func() push.PushService { - svc, err := push.NewVivoService(cfg, logger) - if err != nil { - panic(err) - } - return adapter.NewPushServiceAdapter(svc) - }) - - pushServiceFactory.Register(consts.PlatformOppo.String(), func() push.PushService { - svc, err := push.NewOppoService(cfg, logger) - if err != nil { - panic(err) - } - return adapter.NewPushServiceAdapter(svc) - }) - - pushServiceFactory.Register(consts.PlatformXiaomi.String(), func() push.PushService { - svc, err := push.NewXiaomiService(cfg, logger) - if err != nil { - panic(err) - } - return adapter.NewPushServiceAdapter(svc) - }) - - pushServiceFactory.Register(consts.PlatformMeizu.String(), func() push.PushService { - svc, err := push.NewMeizuService(cfg, logger) - if err != nil { - panic(err) - } - return adapter.NewPushServiceAdapter(svc) - }) - - pushServiceFactory.Register(consts.PlatformHonor.String(), func() push.PushService { - svc, err := push.NewHonorService(cfg, logger) - if err != nil { - panic(err) - } - return adapter.NewPushServiceAdapter(svc) - }) + if err := pushServiceFactory.Register( + func() (push.PushService, error) { + return push.NewAPNsService(cfg, logger) + }, + func() (push.PushService, error) { + return push.NewFCMService(cfg, logger) + }, + func() (push.PushService, error) { + return push.NewHMSService(cfg, logger) + }, + func() (push.PushService, error) { + return push.NewXiaomiService(cfg, logger) + }, + func() (push.PushService, error) { + return push.NewVivoService(cfg, logger) + }, + func() (push.PushService, error) { + return push.NewOppoService(cfg, logger) + }, + func() (push.PushService, error) { + return push.NewMeizuService(cfg, logger) + }, + func() (push.PushService, error) { + return push.NewHonorService(cfg, logger) + }, + ); err != nil { + panic(err) + } ctx, cancel := context.WithCancel(context.Background()) defer cancel() diff --git a/factory/factory.go b/factory/factory.go new file mode 100644 index 0000000..b329d51 --- /dev/null +++ b/factory/factory.go @@ -0,0 +1,47 @@ +package factory + +import ( + "errors" + "github.com/cossim/hipush/push" +) + +type PushServiceCreator func() (push.PushService, error) + +type PushServiceFactory struct { + creators map[string]push.PushService +} + +func NewPushServiceFactory() *PushServiceFactory { + return &PushServiceFactory{ + creators: make(map[string]push.PushService), + } +} + +func WithPushServiceCreator(creator func() (push.PushService, error)) PushServiceCreator { + return func() (push.PushService, error) { + ps, err := creator() + if err != nil { + return nil, err + } + return ps, nil + } +} + +func (f *PushServiceFactory) Register(creators ...PushServiceCreator) error { + for _, c := range creators { + ps, err := c() + if err != nil { + return err + } + f.creators[ps.Name()] = ps + } + return nil +} + +func (f *PushServiceFactory) GetPushService(name string) (push.PushService, error) { + ps, ok := f.creators[name] + if !ok { + return nil, errors.New("unsupported platform") + } + return ps, nil +} diff --git a/internal/adapter/adapter.go b/internal/adapter/adapter.go deleted file mode 100644 index d1d9cab..0000000 --- a/internal/adapter/adapter.go +++ /dev/null @@ -1,48 +0,0 @@ -package adapter - -import ( - "context" - "github.com/cossim/hipush/push" -) - -func NewPushServiceAdapter(pushService push.PushService) *PushServiceAdapter { - return &PushServiceAdapter{pushService: pushService} -} - -type PushServiceAdapter struct { - pushService push.PushService -} - -func (p *PushServiceAdapter) Send(ctx context.Context, req interface{}, opt ...push.SendOption) error { - return p.pushService.Send(ctx, req, opt...) -} - -func (p *PushServiceAdapter) SendMulticast(ctx context.Context, req interface{}, opt ...push.MulticastOption) error { - //TODO implement me - panic("implement me") -} - -func (p *PushServiceAdapter) Subscribe(ctx context.Context, req interface{}, opt ...push.SubscribeOption) error { - //TODO implement me - panic("implement me") -} - -func (p *PushServiceAdapter) Unsubscribe(ctx context.Context, req interface{}, opt ...push.UnsubscribeOption) error { - //TODO implement me - panic("implement me") -} - -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{}, opt ...push.CheckDeviceOption) bool { - //TODO implement me - panic("implement me") -} - -func (p *PushServiceAdapter) Name() string { - //TODO implement me - panic("implement me") -} diff --git a/internal/factory/factory.go b/internal/factory/factory.go deleted file mode 100644 index 7ab40ea..0000000 --- a/internal/factory/factory.go +++ /dev/null @@ -1,30 +0,0 @@ -package factory - -import ( - "errors" - "github.com/cossim/hipush/push" -) - -type PushServiceCreator func() push.PushService - -type PushServiceFactory struct { - creators map[string]push.PushService -} - -func NewPushServiceFactory() *PushServiceFactory { - return &PushServiceFactory{ - creators: make(map[string]push.PushService), - } -} - -func (f *PushServiceFactory) Register(name string, creator PushServiceCreator) { - f.creators[name] = creator() -} - -func (f *PushServiceFactory) GetPushService(name string) (push.PushService, error) { - creator, ok := f.creators[name] - if !ok { - return nil, errors.New("unsupported platform") - } - return creator, nil -} diff --git a/internal/server/grpc/grpc.go b/internal/server/grpc/grpc.go index 4baf751..8cf986c 100644 --- a/internal/server/grpc/grpc.go +++ b/internal/server/grpc/grpc.go @@ -8,7 +8,7 @@ import ( "github.com/cossim/hipush/api/grpc/v1" "github.com/cossim/hipush/config" "github.com/cossim/hipush/consts" - "github.com/cossim/hipush/internal/factory" + "github.com/cossim/hipush/factory" "github.com/cossim/hipush/notify" "github.com/cossim/hipush/push" "github.com/cossim/hipush/status" diff --git a/internal/server/http/http.go b/internal/server/http/http.go index a7cbc17..63aae02 100644 --- a/internal/server/http/http.go +++ b/internal/server/http/http.go @@ -7,7 +7,7 @@ import ( "github.com/cossim/hipush/api/http/v1/dto" "github.com/cossim/hipush/config" "github.com/cossim/hipush/consts" - "github.com/cossim/hipush/internal/factory" + "github.com/cossim/hipush/factory" "github.com/cossim/hipush/status" "github.com/gin-gonic/gin" "github.com/go-logr/logr" diff --git a/push/apns_push.go b/push/apns_push.go index 62aaff2..57d8349 100644 --- a/push/apns_push.go +++ b/push/apns_push.go @@ -7,6 +7,7 @@ import ( "errors" "fmt" "github.com/cossim/hipush/config" + "github.com/cossim/hipush/consts" "github.com/cossim/hipush/notify" "github.com/cossim/hipush/status" "github.com/go-logr/logr" @@ -359,32 +360,6 @@ func (a *APNsService) checkNotification(req *notify.ApnsPushNotification) error return nil } -func (a *APNsService) SendMulticast(ctx context.Context, req interface{}, opt ...MulticastOption) error { - //TODO implement me - panic("implement me") -} - -func (a *APNsService) Subscribe(ctx context.Context, req interface{}, opt ...SubscribeOption) error { - //TODO implement me - panic("implement me") -} - -func (a *APNsService) Unsubscribe(ctx context.Context, req interface{}, opt ...UnsubscribeOption) error { - //TODO implement me - panic("implement me") -} - -func (a *APNsService) SendToTopic(ctx context.Context, req interface{}, opt ...TopicOption) error { - //TODO implement me - panic("implement me") -} - -func (a *APNsService) CheckDevice(ctx context.Context, req interface{}, opt ...CheckDeviceOption) bool { - //TODO implement me - panic("implement me") -} - func (a *APNsService) Name() string { - //TODO implement me - panic("implement me") + return consts.PlatformIOS.String() } diff --git a/push/fcm_push.go b/push/fcm_push.go index 4dfcc85..9b1f768 100644 --- a/push/fcm_push.go +++ b/push/fcm_push.go @@ -7,6 +7,7 @@ import ( "firebase.google.com/go/messaging" "fmt" "github.com/cossim/hipush/config" + "github.com/cossim/hipush/consts" "github.com/cossim/hipush/notify" "github.com/cossim/hipush/status" "github.com/go-logr/logr" @@ -269,32 +270,6 @@ func (f *FCMService) buildAndroidNotification(req *notify.FCMPushNotification) * return notification } -func (f *FCMService) SendMulticast(ctx context.Context, req interface{}, opt ...MulticastOption) error { - //TODO implement me - panic("implement me") -} - -func (f *FCMService) Subscribe(ctx context.Context, req interface{}, opt ...SubscribeOption) error { - //TODO implement me - panic("implement me") -} - -func (f *FCMService) Unsubscribe(ctx context.Context, req interface{}, opt ...UnsubscribeOption) error { - //TODO implement me - panic("implement me") -} - -func (f *FCMService) SendToTopic(ctx context.Context, req interface{}, opt ...TopicOption) error { - //TODO implement me - panic("implement me") -} - -func (f *FCMService) CheckDevice(ctx context.Context, req interface{}, opt ...CheckDeviceOption) bool { - //TODO implement me - panic("implement me") -} - func (f *FCMService) Name() string { - //TODO implement me - panic("implement me") + return consts.PlatformAndroid.String() } diff --git a/push/honor_push.go b/push/honor_push.go index 561b04a..7516d60 100644 --- a/push/honor_push.go +++ b/push/honor_push.go @@ -6,6 +6,7 @@ import ( "fmt" hClient "github.com/cossim/hipush/client/push" "github.com/cossim/hipush/config" + "github.com/cossim/hipush/consts" "github.com/cossim/hipush/notify" "github.com/cossim/hipush/status" "github.com/go-logr/logr" @@ -213,32 +214,6 @@ func (h *HonorService) buildAndroidNotification(req *notify.HonorPushNotificatio return sendMessageReq } -func (h *HonorService) SendMulticast(ctx context.Context, req interface{}, opt ...MulticastOption) error { - //TODO implement me - panic("implement me") -} - -func (h *HonorService) Subscribe(ctx context.Context, req interface{}, opt ...SubscribeOption) error { - //TODO implement me - panic("implement me") -} - -func (h *HonorService) Unsubscribe(ctx context.Context, req interface{}, opt ...UnsubscribeOption) error { - //TODO implement me - panic("implement me") -} - -func (h *HonorService) SendToTopic(ctx context.Context, req interface{}, opt ...TopicOption) error { - //TODO implement me - panic("implement me") -} - -func (h *HonorService) CheckDevice(ctx context.Context, req interface{}, opt ...CheckDeviceOption) bool { - //TODO implement me - panic("implement me") -} - func (h *HonorService) Name() string { - //TODO implement me - panic("implement me") + return consts.PlatformHonor.String() } diff --git a/push/huawei_push.go b/push/huawei_push.go index 56a5b5c..fa4e291 100644 --- a/push/huawei_push.go +++ b/push/huawei_push.go @@ -9,6 +9,7 @@ import ( client "github.com/cossim/go-hms-push/push/core" "github.com/cossim/go-hms-push/push/model" "github.com/cossim/hipush/config" + "github.com/cossim/hipush/consts" "github.com/cossim/hipush/notify" "github.com/cossim/hipush/status" "github.com/go-logr/logr" @@ -290,32 +291,6 @@ func (h *HMSService) buildNotification(req *notify.HMSPushNotification) (*model. return msgRequest, nil } -func (h *HMSService) SendMulticast(ctx context.Context, req interface{}, opt ...MulticastOption) error { - //TODO implement me - panic("implement me") -} - -func (h *HMSService) Subscribe(ctx context.Context, req interface{}, opt ...SubscribeOption) error { - //TODO implement me - panic("implement me") -} - -func (h *HMSService) Unsubscribe(ctx context.Context, req interface{}, opt ...UnsubscribeOption) error { - //TODO implement me - panic("implement me") -} - -func (h *HMSService) SendToTopic(ctx context.Context, req interface{}, opt ...TopicOption) error { - //TODO implement me - panic("implement me") -} - -func (h *HMSService) CheckDevice(ctx context.Context, req interface{}, opt ...CheckDeviceOption) bool { - //TODO implement me - panic("implement me") -} - func (h *HMSService) Name() string { - //TODO implement me - panic("implement me") + return consts.PlatformHuawei.String() } diff --git a/push/meizu_push.go b/push/meizu_push.go index 88ccc30..8d54b15 100644 --- a/push/meizu_push.go +++ b/push/meizu_push.go @@ -7,6 +7,7 @@ import ( "fmt" mzp "github.com/cossim/go-meizu-push-sdk" "github.com/cossim/hipush/config" + "github.com/cossim/hipush/consts" "github.com/cossim/hipush/notify" "github.com/cossim/hipush/status" "github.com/go-logr/logr" @@ -200,32 +201,6 @@ func (m *MeizuService) buildNotification(req *notify.MeizuPushNotification) (str return string(message), nil } -func (m *MeizuService) SendMulticast(ctx context.Context, req interface{}, opt ...MulticastOption) error { - //TODO implement me - panic("implement me") -} - -func (m *MeizuService) Subscribe(ctx context.Context, req interface{}, opt ...SubscribeOption) error { - //TODO implement me - panic("implement me") -} - -func (m *MeizuService) Unsubscribe(ctx context.Context, req interface{}, opt ...UnsubscribeOption) error { - //TODO implement me - panic("implement me") -} - -func (m *MeizuService) SendToTopic(ctx context.Context, req interface{}, opt ...TopicOption) error { - //TODO implement me - panic("implement me") -} - -func (m *MeizuService) CheckDevice(ctx context.Context, req interface{}, opt ...CheckDeviceOption) bool { - //TODO implement me - panic("implement me") -} - func (m *MeizuService) Name() string { - //TODO implement me - panic("implement me") + return consts.PlatformMeizu.String() } diff --git a/push/oppo_push.go b/push/oppo_push.go index 80123ca..d23d33d 100644 --- a/push/oppo_push.go +++ b/push/oppo_push.go @@ -6,6 +6,7 @@ import ( "fmt" op "github.com/316014408/oppo-push" "github.com/cossim/hipush/config" + "github.com/cossim/hipush/consts" "github.com/cossim/hipush/notify" "github.com/cossim/hipush/status" "github.com/go-logr/logr" @@ -191,32 +192,6 @@ func (o *OppoService) buildNotification(req *notify.OppoPushNotification) (*op.M return m, nil } -func (o *OppoService) SendMulticast(ctx context.Context, req interface{}, opt ...MulticastOption) error { - //TODO implement me - panic("implement me") -} - -func (o *OppoService) Subscribe(ctx context.Context, req interface{}, opt ...SubscribeOption) error { - //TODO implement me - panic("implement me") -} - -func (o *OppoService) Unsubscribe(ctx context.Context, req interface{}, opt ...UnsubscribeOption) error { - //TODO implement me - panic("implement me") -} - -func (o *OppoService) SendToTopic(ctx context.Context, req interface{}, opt ...TopicOption) error { - //TODO implement me - panic("implement me") -} - -func (o *OppoService) CheckDevice(ctx context.Context, req interface{}, opt ...CheckDeviceOption) bool { - //TODO implement me - panic("implement me") -} - func (o *OppoService) Name() string { - //TODO implement me - panic("implement me") + return consts.PlatformOppo.String() } diff --git a/push/push.go b/push/push.go index be617dd..1b99d91 100644 --- a/push/push.go +++ b/push/push.go @@ -95,21 +95,6 @@ type PushService interface { // Send 发送消息给单个设备 Send(ctx context.Context, req interface{}, opt ...SendOption) error - // SendMulticast 发送消息给多个设备 - SendMulticast(ctx context.Context, req interface{}, opt ...MulticastOption) error - - // Subscribe 订阅特定主题 - Subscribe(ctx context.Context, req interface{}, opt ...SubscribeOption) error - - // Unsubscribe 取消订阅特定主题 - Unsubscribe(ctx context.Context, req interface{}, opt ...UnsubscribeOption) error - - // SendToTopic 发送消息到特定主题 - SendToTopic(ctx context.Context, req interface{}, opt ...TopicOption) error - - // CheckDevice 检查设备是否可用 - CheckDevice(ctx context.Context, req interface{}, opt ...CheckDeviceOption) bool - - // Name 获取推送服务的名称 + // Name 获取推送的手机厂商名称 Name() string } diff --git a/push/vivo_push.go b/push/vivo_push.go index 9957425..58cdaaf 100644 --- a/push/vivo_push.go +++ b/push/vivo_push.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" "github.com/cossim/hipush/config" + "github.com/cossim/hipush/consts" "github.com/cossim/hipush/notify" "github.com/cossim/hipush/status" vp "github.com/cossim/vivo-push" @@ -214,32 +215,6 @@ func (v *VivoService) buildNotification(req *notify.VivoPushNotification) (*vp.M return message, nil } -func (v *VivoService) SendMulticast(ctx context.Context, req interface{}, opt ...MulticastOption) error { - //TODO implement me - panic("implement me") -} - -func (v *VivoService) Subscribe(ctx context.Context, req interface{}, opt ...SubscribeOption) error { - //TODO implement me - panic("implement me") -} - -func (v *VivoService) Unsubscribe(ctx context.Context, req interface{}, opt ...UnsubscribeOption) error { - //TODO implement me - panic("implement me") -} - -func (v *VivoService) SendToTopic(ctx context.Context, req interface{}, opt ...TopicOption) error { - //TODO implement me - panic("implement me") -} - -func (v *VivoService) CheckDevice(ctx context.Context, req interface{}, opt ...CheckDeviceOption) bool { - //TODO implement me - panic("implement me") -} - func (v *VivoService) Name() string { - //TODO implement me - panic("implement me") + return consts.PlatformVivo.String() } diff --git a/push/xiaomi_push.go b/push/xiaomi_push.go index 115982a..f3bff9b 100644 --- a/push/xiaomi_push.go +++ b/push/xiaomi_push.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" "github.com/cossim/hipush/config" + "github.com/cossim/hipush/consts" "github.com/cossim/hipush/notify" "github.com/cossim/hipush/status" "github.com/go-logr/logr" @@ -193,32 +194,6 @@ func (x *XiaomiPushService) send(ctx context.Context, appID string, tokens []str return newTokens, nil } -func (x *XiaomiPushService) SendMulticast(ctx context.Context, req interface{}, opt ...MulticastOption) error { - //TODO implement me - panic("implement me") -} - -func (x *XiaomiPushService) Subscribe(ctx context.Context, req interface{}, opt ...SubscribeOption) error { - //TODO implement me - panic("implement me") -} - -func (x *XiaomiPushService) Unsubscribe(ctx context.Context, req interface{}, opt ...UnsubscribeOption) error { - //TODO implement me - panic("implement me") -} - -func (x *XiaomiPushService) SendToTopic(ctx context.Context, req interface{}, opt ...TopicOption) error { - //TODO implement me - panic("implement me") -} - -func (x *XiaomiPushService) CheckDevice(ctx context.Context, req interface{}, opt ...CheckDeviceOption) bool { - //TODO implement me - panic("implement me") -} - func (x *XiaomiPushService) Name() string { - //TODO implement me - panic("implement me") + return consts.PlatformXiaomi.String() }