From 8ce565573ee9567eb09c8b37128ef13d49919abb Mon Sep 17 00:00:00 2001 From: Gordon <46924906+FGadvancer@users.noreply.github.com> Date: Sun, 7 Apr 2024 17:29:53 +0800 Subject: [PATCH] refactor: friends update --- cmd/openim-rpc/openim-rpc-friend/main.go | 6 +- internal/rpc/friend/black.go | 2 +- internal/rpc/friend/callback.go | 60 ++++++++++---------- internal/rpc/friend/friend.go | 40 +++++++------- pkg/common/cmd/auth.go | 12 ++-- pkg/common/cmd/constant.go | 3 +- pkg/common/cmd/conversation.go | 16 +++--- pkg/common/cmd/friend.go | 70 ++++++++++++++++++++++++ pkg/common/http/http_client.go | 10 ++-- 9 files changed, 143 insertions(+), 76 deletions(-) create mode 100644 pkg/common/cmd/friend.go diff --git a/cmd/openim-rpc/openim-rpc-friend/main.go b/cmd/openim-rpc/openim-rpc-friend/main.go index 3ea57d34f..745c40553 100644 --- a/cmd/openim-rpc/openim-rpc-friend/main.go +++ b/cmd/openim-rpc/openim-rpc-friend/main.go @@ -15,16 +15,12 @@ package main import ( - "github.com/openimsdk/open-im-server/v3/internal/rpc/friend" "github.com/openimsdk/open-im-server/v3/pkg/common/cmd" "github.com/openimsdk/tools/system/program" ) func main() { - rpcCmd := cmd.NewRpcCmd(cmd.RpcFriendServer, friend.Start) - rpcCmd.AddPortFlag() - rpcCmd.AddPrometheusPortFlag() - if err := rpcCmd.Exec(); err != nil { + if err := cmd.NewFriendRpcCmd().Exec(); err != nil { program.ExitWithError(err) } } diff --git a/internal/rpc/friend/black.go b/internal/rpc/friend/black.go index ad63445c4..591859194 100644 --- a/internal/rpc/friend/black.go +++ b/internal/rpc/friend/black.go @@ -68,7 +68,7 @@ func (s *friendServer) RemoveBlack(ctx context.Context, req *pbfriend.RemoveBlac } func (s *friendServer) AddBlack(ctx context.Context, req *pbfriend.AddBlackReq) (*pbfriend.AddBlackResp, error) { - if err := authverify.CheckAccessV3(ctx, req.OwnerUserID, &s.config.Manager, &s.config.IMAdmin); err != nil { + if err := authverify.CheckAccessV3(ctx, req.OwnerUserID, &s.config.Share.IMAdmin); err != nil { return nil, err } _, err := s.userRpcClient.GetUsersInfo(ctx, []string{req.OwnerUserID, req.BlackUserID}) diff --git a/internal/rpc/friend/callback.go b/internal/rpc/friend/callback.go index 4800f73fb..81f80082a 100644 --- a/internal/rpc/friend/callback.go +++ b/internal/rpc/friend/callback.go @@ -24,8 +24,8 @@ import ( "github.com/openimsdk/tools/utils/datautil" ) -func CallbackBeforeAddFriend(ctx context.Context, callback *config.Callback, req *pbfriend.ApplyToAddFriendReq) error { - if !callback.CallbackBeforeAddFriend.Enable { +func CallbackBeforeAddFriend(ctx context.Context, callback *config.Webhooks, req *pbfriend.ApplyToAddFriendReq) error { + if !callback.BeforeAddFriend.Enable { return nil } cbReq := &cbapi.CallbackBeforeAddFriendReq{ @@ -36,14 +36,14 @@ func CallbackBeforeAddFriend(ctx context.Context, callback *config.Callback, req Ex: req.Ex, } resp := &cbapi.CallbackBeforeAddFriendResp{} - if err := http.CallBackPostReturn(ctx, callback.CallbackUrl, cbReq, resp, callback.CallbackBeforeAddFriend); err != nil { + if err := http.CallBackPostReturn(ctx, callback.URL, cbReq, resp, callback.BeforeAddFriend); err != nil { return err } return nil } -func CallbackBeforeSetFriendRemark(ctx context.Context, callback *config.Callback, req *pbfriend.SetFriendRemarkReq) error { - if !callback.CallbackBeforeSetFriendRemark.Enable { +func CallbackBeforeSetFriendRemark(ctx context.Context, callback *config.Webhooks, req *pbfriend.SetFriendRemarkReq) error { + if !callback.BeforeSetFriendRemark.Enable { return nil } cbReq := &cbapi.CallbackBeforeSetFriendRemarkReq{ @@ -53,15 +53,15 @@ func CallbackBeforeSetFriendRemark(ctx context.Context, callback *config.Callbac Remark: req.Remark, } resp := &cbapi.CallbackBeforeSetFriendRemarkResp{} - if err := http.CallBackPostReturn(ctx, callback.CallbackUrl, cbReq, resp, callback.CallbackBeforeAddFriend); err != nil { + if err := http.CallBackPostReturn(ctx, callback.URL, cbReq, resp, callback.BeforeAddFriend); err != nil { return err } datautil.NotNilReplace(&req.Remark, &resp.Remark) return nil } -func CallbackAfterSetFriendRemark(ctx context.Context, callback *config.Callback, req *pbfriend.SetFriendRemarkReq) error { - if !callback.CallbackAfterSetFriendRemark.Enable { +func CallbackAfterSetFriendRemark(ctx context.Context, callback *config.Webhooks, req *pbfriend.SetFriendRemarkReq) error { + if !callback.AfterSetFriendRemark.Enable { return nil } cbReq := &cbapi.CallbackAfterSetFriendRemarkReq{ @@ -71,14 +71,14 @@ func CallbackAfterSetFriendRemark(ctx context.Context, callback *config.Callback Remark: req.Remark, } resp := &cbapi.CallbackAfterSetFriendRemarkResp{} - if err := http.CallBackPostReturn(ctx, callback.CallbackUrl, cbReq, resp, callback.CallbackBeforeAddFriend); err != nil { + if err := http.CallBackPostReturn(ctx, callback.URL, cbReq, resp, callback.BeforeAddFriend); err != nil { return err } return nil } -func CallbackBeforeAddBlack(ctx context.Context, callback *config.Callback, req *pbfriend.AddBlackReq) error { - if !callback.CallbackBeforeAddBlack.Enable { +func CallbackBeforeAddBlack(ctx context.Context, callback *config.Webhooks, req *pbfriend.AddBlackReq) error { + if !callback.BeforeAddBlack.Enable { return nil } cbReq := &cbapi.CallbackBeforeAddBlackReq{ @@ -87,14 +87,14 @@ func CallbackBeforeAddBlack(ctx context.Context, callback *config.Callback, req BlackUserID: req.BlackUserID, } resp := &cbapi.CallbackBeforeAddBlackResp{} - if err := http.CallBackPostReturn(ctx, callback.CallbackUrl, cbReq, resp, callback.CallbackBeforeAddBlack); err != nil { + if err := http.CallBackPostReturn(ctx, callback.URL, cbReq, resp, callback.BeforeAddBlack); err != nil { return err } return nil } -func CallbackAfterAddFriend(ctx context.Context, callback *config.Callback, req *pbfriend.ApplyToAddFriendReq) error { - if !callback.CallbackAfterAddFriend.Enable { +func CallbackAfterAddFriend(ctx context.Context, callback *config.Webhooks, req *pbfriend.ApplyToAddFriendReq) error { + if !callback.AfterAddFriend.Enable { return nil } cbReq := &cbapi.CallbackAfterAddFriendReq{ @@ -104,15 +104,15 @@ func CallbackAfterAddFriend(ctx context.Context, callback *config.Callback, req ReqMsg: req.ReqMsg, } resp := &cbapi.CallbackAfterAddFriendResp{} - if err := http.CallBackPostReturn(ctx, callback.CallbackUrl, cbReq, resp, callback.CallbackAfterAddFriend); err != nil { + if err := http.CallBackPostReturn(ctx, callback.URL, cbReq, resp, callback.AfterAddFriend); err != nil { return err } return nil } -func CallbackBeforeAddFriendAgree(ctx context.Context, callback *config.Callback, req *pbfriend.RespondFriendApplyReq) error { - if !callback.CallbackBeforeAddFriendAgree.Enable { +func CallbackBeforeAddFriendAgree(ctx context.Context, callback *config.Webhooks, req *pbfriend.RespondFriendApplyReq) error { + if !callback.BeforeAddFriendAgree.Enable { return nil } cbReq := &cbapi.CallbackBeforeAddFriendAgreeReq{ @@ -123,14 +123,14 @@ func CallbackBeforeAddFriendAgree(ctx context.Context, callback *config.Callback HandleResult: req.HandleResult, } resp := &cbapi.CallbackBeforeAddFriendAgreeResp{} - if err := http.CallBackPostReturn(ctx, callback.CallbackUrl, cbReq, resp, callback.CallbackBeforeAddFriendAgree); err != nil { + if err := http.CallBackPostReturn(ctx, callback.URL, cbReq, resp, callback.BeforeAddFriendAgree); err != nil { return err } return nil } -func CallbackAfterDeleteFriend(ctx context.Context, callback *config.Callback, req *pbfriend.DeleteFriendReq) error { - if !callback.CallbackAfterDeleteFriend.Enable { +func CallbackAfterDeleteFriend(ctx context.Context, callback *config.Webhooks, req *pbfriend.DeleteFriendReq) error { + if !callback.AfterDeleteFriend.Enable { return nil } cbReq := &cbapi.CallbackAfterDeleteFriendReq{ @@ -139,14 +139,14 @@ func CallbackAfterDeleteFriend(ctx context.Context, callback *config.Callback, r FriendUserID: req.FriendUserID, } resp := &cbapi.CallbackAfterDeleteFriendResp{} - if err := http.CallBackPostReturn(ctx, callback.CallbackUrl, cbReq, resp, callback.CallbackAfterDeleteFriend); err != nil { + if err := http.CallBackPostReturn(ctx, callback.URL, cbReq, resp, callback.AfterDeleteFriend); err != nil { return err } return nil } -func CallbackBeforeImportFriends(ctx context.Context, callback *config.Callback, req *pbfriend.ImportFriendReq) error { - if !callback.CallbackBeforeImportFriends.Enable { +func CallbackBeforeImportFriends(ctx context.Context, callback *config.Webhooks, req *pbfriend.ImportFriendReq) error { + if !callback.BeforeImportFriends.Enable { return nil } cbReq := &cbapi.CallbackBeforeImportFriendsReq{ @@ -155,7 +155,7 @@ func CallbackBeforeImportFriends(ctx context.Context, callback *config.Callback, FriendUserIDs: req.FriendUserIDs, } resp := &cbapi.CallbackBeforeImportFriendsResp{} - if err := http.CallBackPostReturn(ctx, callback.CallbackUrl, cbReq, resp, callback.CallbackBeforeImportFriends); err != nil { + if err := http.CallBackPostReturn(ctx, callback.URL, cbReq, resp, callback.BeforeImportFriends); err != nil { return err } if len(resp.FriendUserIDs) != 0 { @@ -164,8 +164,8 @@ func CallbackBeforeImportFriends(ctx context.Context, callback *config.Callback, return nil } -func CallbackAfterImportFriends(ctx context.Context, callback *config.Callback, req *pbfriend.ImportFriendReq) error { - if !callback.CallbackAfterImportFriends.Enable { +func CallbackAfterImportFriends(ctx context.Context, callback *config.Webhooks, req *pbfriend.ImportFriendReq) error { + if !callback.AfterImportFriends.Enable { return nil } cbReq := &cbapi.CallbackAfterImportFriendsReq{ @@ -174,14 +174,14 @@ func CallbackAfterImportFriends(ctx context.Context, callback *config.Callback, FriendUserIDs: req.FriendUserIDs, } resp := &cbapi.CallbackAfterImportFriendsResp{} - if err := http.CallBackPostReturn(ctx, callback.CallbackUrl, cbReq, resp, callback.CallbackAfterImportFriends); err != nil { + if err := http.CallBackPostReturn(ctx, callback.URL, cbReq, resp, callback.AfterImportFriends); err != nil { return err } return nil } -func CallbackAfterRemoveBlack(ctx context.Context, callback *config.Callback, req *pbfriend.RemoveBlackReq) error { - if !callback.CallbackAfterRemoveBlack.Enable { +func CallbackAfterRemoveBlack(ctx context.Context, callback *config.Webhooks, req *pbfriend.RemoveBlackReq) error { + if !callback.AfterRemoveBlack.Enable { return nil } cbReq := &cbapi.CallbackAfterRemoveBlackReq{ @@ -190,7 +190,7 @@ func CallbackAfterRemoveBlack(ctx context.Context, callback *config.Callback, re BlackUserID: req.BlackUserID, } resp := &cbapi.CallbackAfterRemoveBlackResp{} - if err := http.CallBackPostReturn(ctx, callback.CallbackUrl, cbReq, resp, callback.CallbackAfterRemoveBlack); err != nil { + if err := http.CallBackPostReturn(ctx, callback.URL, cbReq, resp, callback.AfterRemoveBlack); err != nil { return err } return nil diff --git a/internal/rpc/friend/friend.go b/internal/rpc/friend/friend.go index 32e05c1d1..1a7c17104 100644 --- a/internal/rpc/friend/friend.go +++ b/internal/rpc/friend/friend.go @@ -16,10 +16,10 @@ package friend import ( "context" + "github.com/openimsdk/open-im-server/v3/pkg/common/cmd" "github.com/openimsdk/tools/db/redisutil" "github.com/openimsdk/open-im-server/v3/pkg/authverify" - "github.com/openimsdk/open-im-server/v3/pkg/common/config" "github.com/openimsdk/open-im-server/v3/pkg/common/convert" "github.com/openimsdk/open-im-server/v3/pkg/common/db/cache" "github.com/openimsdk/open-im-server/v3/pkg/common/db/controller" @@ -45,15 +45,15 @@ type friendServer struct { notificationSender *notification.FriendNotificationSender conversationRpcClient rpcclient.ConversationRpcClient RegisterCenter discovery.SvcDiscoveryRegistry - config *config.GlobalConfig + config *cmd.FriendConfig } -func Start(ctx context.Context, config *config.GlobalConfig, client discovery.SvcDiscoveryRegistry, server *grpc.Server) error { - mgocli, err := mongoutil.NewMongoDB(ctx, config.Mongo.Build()) +func Start(ctx context.Context, config *cmd.FriendConfig, client discovery.SvcDiscoveryRegistry, server *grpc.Server) error { + mgocli, err := mongoutil.NewMongoDB(ctx, config.MongodbConfig.Build()) if err != nil { return err } - rdb, err := redisutil.NewRedisClient(ctx, config.Redis.Build()) + rdb, err := redisutil.NewRedisClient(ctx, config.RedisConfig.Build()) if err != nil { return err } @@ -74,12 +74,12 @@ func Start(ctx context.Context, config *config.GlobalConfig, client discovery.Sv } // Initialize RPC clients - userRpcClient := rpcclient.NewUserRpcClient(client, config.RpcRegisterName.OpenImUserName, &config.Manager, &config.IMAdmin) - msgRpcClient := rpcclient.NewMessageRpcClient(client, config.RpcRegisterName.OpenImMsgName) + userRpcClient := rpcclient.NewUserRpcClient(client, config.Share.RpcRegisterName.User, &config.Share.IMAdmin) + msgRpcClient := rpcclient.NewMessageRpcClient(client, config.Share.RpcRegisterName.Msg) // Initialize notification sender notificationSender := notification.NewFriendNotificationSender( - &config.Notification, + &config.NotificationConfig, &msgRpcClient, notification.WithRpcFunc(userRpcClient.GetUsersInfo), ) @@ -99,7 +99,7 @@ func Start(ctx context.Context, config *config.GlobalConfig, client discovery.Sv userRpcClient: &userRpcClient, notificationSender: notificationSender, RegisterCenter: client, - conversationRpcClient: rpcclient.NewConversationRpcClient(client, config.RpcRegisterName.OpenImConversationName), + conversationRpcClient: rpcclient.NewConversationRpcClient(client, config.Share.RpcRegisterName.Conversation), config: config, }) @@ -109,14 +109,14 @@ func Start(ctx context.Context, config *config.GlobalConfig, client discovery.Sv // ok. func (s *friendServer) ApplyToAddFriend(ctx context.Context, req *pbfriend.ApplyToAddFriendReq) (resp *pbfriend.ApplyToAddFriendResp, err error) { resp = &pbfriend.ApplyToAddFriendResp{} - if err := authverify.CheckAccessV3(ctx, req.FromUserID, &s.config.Manager, &s.config.IMAdmin); err != nil { + if err := authverify.CheckAccessV3(ctx, req.FromUserID, &s.config.Share.IMAdmin); err != nil { return nil, err } if req.ToUserID == req.FromUserID { return nil, servererrs.ErrCanNotAddYourself.WrapMsg("req.ToUserID", req.ToUserID) } - if err = CallbackBeforeAddFriend(ctx, &s.config.Callback, req); err != nil && err != servererrs.ErrCallbackContinue { + if err = CallbackBeforeAddFriend(ctx, &s.config.WebhooksConfig, req); err != nil && err != servererrs.ErrCallbackContinue { return nil, err } @@ -141,7 +141,7 @@ func (s *friendServer) ApplyToAddFriend(ctx context.Context, req *pbfriend.Apply return nil, err } - if err = CallbackAfterAddFriend(ctx, &s.config.Callback, req); err != nil && err != servererrs.ErrCallbackContinue { + if err = CallbackAfterAddFriend(ctx, &s.config.WebhooksConfig, req); err != nil && err != servererrs.ErrCallbackContinue { return nil, err } return resp, nil @@ -149,7 +149,7 @@ func (s *friendServer) ApplyToAddFriend(ctx context.Context, req *pbfriend.Apply // ok. func (s *friendServer) ImportFriends(ctx context.Context, req *pbfriend.ImportFriendReq) (resp *pbfriend.ImportFriendResp, err error) { - if err := authverify.CheckAdmin(ctx, &s.config.Manager, &s.config.IMAdmin); err != nil { + if err := authverify.CheckAdmin(ctx, &s.config.Share.IMAdmin); err != nil { return nil, err } if _, err := s.userRpcClient.GetUsersInfo(ctx, append([]string{req.OwnerUserID}, req.FriendUserIDs...)); err != nil { @@ -162,7 +162,7 @@ func (s *friendServer) ImportFriends(ctx context.Context, req *pbfriend.ImportFr return nil, errs.ErrArgs.WrapMsg("friend userID repeated") } - if err := CallbackBeforeImportFriends(ctx, &s.config.Callback, req); err != nil { + if err := CallbackBeforeImportFriends(ctx, &s.config.WebhooksConfig, req); err != nil { return nil, err } @@ -176,7 +176,7 @@ func (s *friendServer) ImportFriends(ctx context.Context, req *pbfriend.ImportFr HandleResult: constant.FriendResponseAgree, }) } - if err := CallbackAfterImportFriends(ctx, &s.config.Callback, req); err != nil { + if err := CallbackAfterImportFriends(ctx, &s.config.WebhooksConfig, req); err != nil { return nil, err } return &pbfriend.ImportFriendResp{}, nil @@ -185,7 +185,7 @@ func (s *friendServer) ImportFriends(ctx context.Context, req *pbfriend.ImportFr // ok. func (s *friendServer) RespondFriendApply(ctx context.Context, req *pbfriend.RespondFriendApplyReq) (resp *pbfriend.RespondFriendApplyResp, err error) { resp = &pbfriend.RespondFriendApplyResp{} - if err := authverify.CheckAccessV3(ctx, req.ToUserID, &s.config.Manager, &s.config.IMAdmin); err != nil { + if err := authverify.CheckAccessV3(ctx, req.ToUserID, &s.config.Share.IMAdmin); err != nil { return nil, err } @@ -196,7 +196,7 @@ func (s *friendServer) RespondFriendApply(ctx context.Context, req *pbfriend.Res HandleResult: req.HandleResult, } if req.HandleResult == constant.FriendResponseAgree { - if err := CallbackBeforeAddFriendAgree(ctx, &s.config.Callback, req); err != nil && err != servererrs.ErrCallbackContinue { + if err := CallbackBeforeAddFriendAgree(ctx, &s.config.WebhooksConfig, req); err != nil && err != servererrs.ErrCallbackContinue { return nil, err } err := s.friendDatabase.AgreeFriendRequest(ctx, &friendRequest) @@ -233,7 +233,7 @@ func (s *friendServer) DeleteFriend(ctx context.Context, req *pbfriend.DeleteFri return nil, err } s.notificationSender.FriendDeletedNotification(ctx, req) - if err := CallbackAfterDeleteFriend(ctx, &s.config.Callback, req); err != nil { + if err := CallbackAfterDeleteFriend(ctx, &s.config.WebhooksConfig, req); err != nil { return nil, err } return resp, nil @@ -242,7 +242,7 @@ func (s *friendServer) DeleteFriend(ctx context.Context, req *pbfriend.DeleteFri // ok. func (s *friendServer) SetFriendRemark(ctx context.Context, req *pbfriend.SetFriendRemarkReq) (resp *pbfriend.SetFriendRemarkResp, err error) { - if err = CallbackBeforeSetFriendRemark(ctx, &s.config.Callback, req); err != nil && err != servererrs.ErrCallbackContinue { + if err = CallbackBeforeSetFriendRemark(ctx, &s.config.WebhooksConfig, req); err != nil && err != servererrs.ErrCallbackContinue { return nil, err } resp = &pbfriend.SetFriendRemarkResp{} @@ -256,7 +256,7 @@ func (s *friendServer) SetFriendRemark(ctx context.Context, req *pbfriend.SetFri if err := s.friendDatabase.UpdateRemark(ctx, req.OwnerUserID, req.FriendUserID, req.Remark); err != nil { return nil, err } - if err := CallbackAfterSetFriendRemark(ctx, &s.config.Callback, req); err != nil && err != servererrs.ErrCallbackContinue { + if err := CallbackAfterSetFriendRemark(ctx, &s.config.WebhooksConfig, req); err != nil && err != servererrs.ErrCallbackContinue { return nil, err } s.notificationSender.FriendRemarkSetNotification(ctx, req.OwnerUserID, req.FriendUserID) diff --git a/pkg/common/cmd/auth.go b/pkg/common/cmd/auth.go index 19fdefd47..fbf01deb9 100644 --- a/pkg/common/cmd/auth.go +++ b/pkg/common/cmd/auth.go @@ -17,7 +17,7 @@ package cmd import ( "context" "github.com/openimsdk/open-im-server/v3/internal/rpc/auth" - config2 "github.com/openimsdk/open-im-server/v3/pkg/common/config" + "github.com/openimsdk/open-im-server/v3/pkg/common/config" "github.com/openimsdk/open-im-server/v3/pkg/common/startrpc" "github.com/openimsdk/tools/system/program" "github.com/spf13/cobra" @@ -30,10 +30,10 @@ type AuthRpcCmd struct { authConfig AuthConfig } type AuthConfig struct { - RpcConfig config2.Auth - RedisConfig config2.Redis - ZookeeperConfig config2.ZooKeeper - Share config2.Share + RpcConfig config.Auth + RedisConfig config.Redis + ZookeeperConfig config.ZooKeeper + Share config.Share } func NewAuthRpcCmd() *AuthRpcCmd { @@ -46,7 +46,7 @@ func NewAuthRpcCmd() *AuthRpcCmd { ShareFileName: {EnvPrefix: shareEnvPrefix, ConfigStruct: &authConfig.Share}, } ret.RootCmd = NewRootCmd(program.GetProcessName(), WithConfigMap(ret.configMap)) - ret.ctx = context.WithValue(context.Background(), "version", config2.Version) + ret.ctx = context.WithValue(context.Background(), "version", config.Version) ret.Command.PreRunE = func(cmd *cobra.Command, args []string) error { return ret.preRunE() } diff --git a/pkg/common/cmd/constant.go b/pkg/common/cmd/constant.go index cf550ebe9..e49785619 100644 --- a/pkg/common/cmd/constant.go +++ b/pkg/common/cmd/constant.go @@ -32,9 +32,9 @@ const ( FileName = "config.yaml" NotificationFileName = "notification.yaml" ShareFileName = "share.yaml" + WebhooksConfigFileName = "webhooks.yml" KafkaConfigFileName = "kafka.yml" RedisConfigFileName = "redis.yml" - WebhooksConfigFileName = "webhooks.yml" ZookeeperConfigFileName = "zookeeper.yml" MongodbConfigFileName = "mongodb.yml" MinioConfigFileName = "minio.yml" @@ -56,6 +56,7 @@ const ( const ( notificationEnvPrefix = "openim-notification" shareEnvPrefix = "openim-share" + webhooksEnvPrefix = "openim-webhooks" logEnvPrefix = "openim-log" redisEnvPrefix = "openim-redis" mongodbEnvPrefix = "openim-mongodb" diff --git a/pkg/common/cmd/conversation.go b/pkg/common/cmd/conversation.go index 022142d66..f6d242b15 100644 --- a/pkg/common/cmd/conversation.go +++ b/pkg/common/cmd/conversation.go @@ -17,7 +17,7 @@ package cmd import ( "context" "github.com/openimsdk/open-im-server/v3/internal/rpc/conversation" - config2 "github.com/openimsdk/open-im-server/v3/pkg/common/config" + "github.com/openimsdk/open-im-server/v3/pkg/common/config" "github.com/openimsdk/open-im-server/v3/pkg/common/startrpc" "github.com/openimsdk/tools/system/program" "github.com/spf13/cobra" @@ -30,12 +30,12 @@ type ConversationRpcCmd struct { conversationConfig ConversationConfig } type ConversationConfig struct { - RpcConfig config2.Conversation - RedisConfig config2.Redis - MongodbConfig config2.Mongo - ZookeeperConfig config2.ZooKeeper - NotificationConfig config2.Notification - Share config2.Share + RpcConfig config.Conversation + RedisConfig config.Redis + MongodbConfig config.Mongo + ZookeeperConfig config.ZooKeeper + NotificationConfig config.Notification + Share config.Share } func NewConversationRpcCmd() *ConversationRpcCmd { @@ -50,7 +50,7 @@ func NewConversationRpcCmd() *ConversationRpcCmd { NotificationFileName: {EnvPrefix: notificationEnvPrefix, ConfigStruct: &conversationConfig.NotificationConfig}, } ret.RootCmd = NewRootCmd(program.GetProcessName(), WithConfigMap(ret.configMap)) - ret.ctx = context.WithValue(context.Background(), "version", config2.Version) + ret.ctx = context.WithValue(context.Background(), "version", config.Version) ret.Command.PreRunE = func(cmd *cobra.Command, args []string) error { return ret.preRunE() } diff --git a/pkg/common/cmd/friend.go b/pkg/common/cmd/friend.go new file mode 100644 index 000000000..ee6dc63e2 --- /dev/null +++ b/pkg/common/cmd/friend.go @@ -0,0 +1,70 @@ +// Copyright © 2023 OpenIM. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package cmd + +import ( + "context" + "github.com/openimsdk/open-im-server/v3/internal/rpc/friend" + "github.com/openimsdk/open-im-server/v3/pkg/common/config" + "github.com/openimsdk/open-im-server/v3/pkg/common/startrpc" + "github.com/openimsdk/tools/system/program" + "github.com/spf13/cobra" +) + +type FriendRpcCmd struct { + *RootCmd + ctx context.Context + configMap map[string]StructEnvPrefix + friendConfig FriendConfig +} +type FriendConfig struct { + RpcConfig config.Conversation + RedisConfig config.Redis + MongodbConfig config.Mongo + ZookeeperConfig config.ZooKeeper + NotificationConfig config.Notification + Share config.Share + WebhooksConfig config.Webhooks +} + +func NewFriendRpcCmd() *FriendRpcCmd { + var friendConfig FriendConfig + ret := &FriendRpcCmd{friendConfig: friendConfig} + ret.configMap = map[string]StructEnvPrefix{ + OpenIMRPCConversationCfgFileName: {EnvPrefix: conversationEnvPrefix, ConfigStruct: &friendConfig.RpcConfig}, + RedisConfigFileName: {EnvPrefix: redisEnvPrefix, ConfigStruct: &friendConfig.RedisConfig}, + ZookeeperConfigFileName: {EnvPrefix: zoopkeeperEnvPrefix, ConfigStruct: &friendConfig.ZookeeperConfig}, + MongodbConfigFileName: {EnvPrefix: mongodbEnvPrefix, ConfigStruct: &friendConfig.MongodbConfig}, + ShareFileName: {EnvPrefix: shareEnvPrefix, ConfigStruct: &friendConfig.Share}, + NotificationFileName: {EnvPrefix: notificationEnvPrefix, ConfigStruct: &friendConfig.NotificationConfig}, + WebhooksConfigFileName: {EnvPrefix: webhooksEnvPrefix, ConfigStruct: &friendConfig.WebhooksConfig}, + } + ret.RootCmd = NewRootCmd(program.GetProcessName(), WithConfigMap(ret.configMap)) + ret.ctx = context.WithValue(context.Background(), "version", config.Version) + ret.Command.PreRunE = func(cmd *cobra.Command, args []string) error { + return ret.preRunE() + } + return ret +} + +func (a *FriendRpcCmd) Exec() error { + return a.Execute() +} + +func (a *FriendRpcCmd) preRunE() error { + return startrpc.Start(a.ctx, &a.friendConfig.ZookeeperConfig, &a.friendConfig.RpcConfig.Prometheus, a.friendConfig.RpcConfig.RPC.ListenIP, + a.friendConfig.RpcConfig.RPC.RegisterIP, a.friendConfig.RpcConfig.RPC.Ports, + a.Index(), a.friendConfig.Share.RpcRegisterName.Auth, &a.friendConfig, friend.Start) +} diff --git a/pkg/common/http/http_client.go b/pkg/common/http/http_client.go index 6f8c412bc..e455cbed7 100644 --- a/pkg/common/http/http_client.go +++ b/pkg/common/http/http_client.go @@ -36,12 +36,12 @@ func init() { http.DefaultTransport.(*http.Transport).MaxConnsPerHost = 100 // default: 2 } -func callBackPostReturn(ctx context.Context, url, command string, input interface{}, output callbackstruct.CallbackResp, callbackConfig config.CallBackConfig) error { +func callBackPostReturn(ctx context.Context, url, command string, input interface{}, output callbackstruct.CallbackResp, callbackConfig config.WebhookConfig) error { url = url + "/" + command log.ZInfo(ctx, "callback", "url", url, "input", input, "config", callbackConfig) - b, err := client.Post(ctx, url, nil, input, callbackConfig.CallbackTimeOut) + b, err := client.Post(ctx, url, nil, input, callbackConfig.Timeout) if err != nil { - if callbackConfig.CallbackFailedContinue != nil && *callbackConfig.CallbackFailedContinue { + if callbackConfig.FailedContinue { log.ZInfo(ctx, "callback failed but continue", err, "url", url) return nil } @@ -49,7 +49,7 @@ func callBackPostReturn(ctx context.Context, url, command string, input interfac return servererrs.ErrNetwork.WrapMsg(err.Error()) } if err = json.Unmarshal(b, output); err != nil { - if callbackConfig.CallbackFailedContinue != nil && *callbackConfig.CallbackFailedContinue { + if callbackConfig.FailedContinue { log.ZWarn(ctx, "callback failed but continue", err, "url", url) return nil } @@ -63,6 +63,6 @@ func callBackPostReturn(ctx context.Context, url, command string, input interfac return nil } -func CallBackPostReturn(ctx context.Context, url string, req callbackstruct.CallbackReq, resp callbackstruct.CallbackResp, callbackConfig config.CallBackConfig) error { +func CallBackPostReturn(ctx context.Context, url string, req callbackstruct.CallbackReq, resp callbackstruct.CallbackResp, callbackConfig config.WebhookConfig) error { return callBackPostReturn(ctx, url, req.GetCallbackCommand(), req, resp, callbackConfig) }