From 7bf29e8e5a11523230c3a7c9c5b66c62d8b26e62 Mon Sep 17 00:00:00 2001 From: icey-yu <1186114839@qq.com> Date: Wed, 12 Jun 2024 11:08:16 +0800 Subject: [PATCH] feat:get fcm config from url --- config/openim-push.yml | 1 + internal/push/offlinepush/fcm/push.go | 21 +++++++++++++++++++-- pkg/common/config/config.go | 1 + 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/config/openim-push.yml b/config/openim-push.yml index a1abfcf88..abade180c 100644 --- a/config/openim-push.yml +++ b/config/openim-push.yml @@ -24,6 +24,7 @@ geTui: channelName: '' fcm: serviceAccount: "x.json" + verifyUrl: "" jpns: appKey: '' masterSecret: '' diff --git a/internal/push/offlinepush/fcm/push.go b/internal/push/offlinepush/fcm/push.go index f7ca385eb..22c1e0e28 100644 --- a/internal/push/offlinepush/fcm/push.go +++ b/internal/push/offlinepush/fcm/push.go @@ -17,6 +17,7 @@ package fcm import ( "context" "github.com/openimsdk/open-im-server/v3/internal/push/offlinepush/options" + "github.com/openimsdk/tools/utils/httputil" "path/filepath" firebase "firebase.google.com/go" @@ -45,8 +46,24 @@ func NewClient(pushConf *config.Push, cache cache.ThirdCache, fcmConfigPath stri //if err != nil { // return nil, err //} - credentialsFilePath := filepath.Join(fcmConfigPath, "config", pushConf.FCM.ServiceAccount) - opt := option.WithCredentialsFile(credentialsFilePath) + var opt option.ClientOption + switch { + case len(pushConf.FCM.ServiceAccount) != 0: + // with file path + credentialsFilePath := filepath.Join(fcmConfigPath, pushConf.FCM.ServiceAccount) + opt = option.WithCredentialsFile(credentialsFilePath) + case len(pushConf.FCM.VerifyUrl) != 0: + // with verify url + client := httputil.NewHTTPClient(httputil.NewClientConfig()) + resp, err := client.Get(pushConf.FCM.VerifyUrl) + if err != nil { + return nil, errs.Wrap(err) + } + opt = option.WithCredentialsJSON(resp) + default: + return nil, errs.New("no FCM config") + } + fcmApp, err := firebase.NewApp(context.Background(), nil, opt) if err != nil { return nil, errs.Wrap(err) diff --git a/pkg/common/config/config.go b/pkg/common/config/config.go index 5313c196a..1c52b6e7f 100644 --- a/pkg/common/config/config.go +++ b/pkg/common/config/config.go @@ -203,6 +203,7 @@ type Push struct { } `mapstructure:"geTui"` FCM struct { ServiceAccount string `mapstructure:"serviceAccount"` + VerifyUrl string `mapstructure:"verifyUrl"` } `mapstructure:"fcm"` JPNS struct { AppKey string `mapstructure:"appKey"`