Skip to content

Commit

Permalink
refactor: 重构factory注册
Browse files Browse the repository at this point in the history
  • Loading branch information
am6737 committed Mar 11, 2024
1 parent b393fce commit 1264795
Show file tree
Hide file tree
Showing 15 changed files with 95 additions and 378 deletions.
95 changes: 29 additions & 66 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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()
Expand Down
47 changes: 47 additions & 0 deletions factory/factory.go
Original file line number Diff line number Diff line change
@@ -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
}
48 changes: 0 additions & 48 deletions internal/adapter/adapter.go

This file was deleted.

30 changes: 0 additions & 30 deletions internal/factory/factory.go

This file was deleted.

2 changes: 1 addition & 1 deletion internal/server/grpc/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion internal/server/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
29 changes: 2 additions & 27 deletions push/apns_push.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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()
}
29 changes: 2 additions & 27 deletions push/fcm_push.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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()
}
29 changes: 2 additions & 27 deletions push/honor_push.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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()
}
Loading

0 comments on commit 1264795

Please sign in to comment.