From cc05aab07e4b249b8c3cc30c0d85eddd670bd5e5 Mon Sep 17 00:00:00 2001 From: withchao <993506633@qq.com> Date: Wed, 3 Apr 2024 17:18:35 +0800 Subject: [PATCH] feat: s3 api addr --- config/templates/open-im-ng-example.conf | 1 + go.mod | 4 +- go.sum | 8 ++-- internal/api/third.go | 48 ++++++++++++++++++++++-- internal/rpc/msg/verify.go | 2 - internal/rpc/third/s3.go | 13 +++---- pkg/common/db/controller/friend.go | 1 - pkg/rpcclient/notification/group.go | 19 ---------- 8 files changed, 57 insertions(+), 39 deletions(-) diff --git a/config/templates/open-im-ng-example.conf b/config/templates/open-im-ng-example.conf index 62befa638..259834777 100644 --- a/config/templates/open-im-ng-example.conf +++ b/config/templates/open-im-ng-example.conf @@ -66,6 +66,7 @@ server { proxy_set_header Connection "Upgrade"; proxy_set_header X-real-ip $remote_addr; proxy_set_header X-Forwarded-For $remote_addr; + proxy_set_header X-Request-Api $scheme://$host/api; proxy_pass http://im_api/; } location ^~/grafana/ { #10007 prometheus diff --git a/go.mod b/go.mod index d06edc1d9..e7e1c1028 100644 --- a/go.mod +++ b/go.mod @@ -14,8 +14,8 @@ require ( github.com/lestrrat-go/file-rotatelogs v2.4.0+incompatible // indirect github.com/mitchellh/mapstructure v1.5.0 github.com/openimsdk/localcache v0.0.1 - github.com/openimsdk/protocol v0.0.59 - github.com/openimsdk/tools v0.0.47-alpha.10 + github.com/openimsdk/protocol v0.0.60 + github.com/openimsdk/tools v0.0.47-alpha.11 github.com/pkg/errors v0.9.1 // indirect github.com/prometheus/client_golang v1.18.0 github.com/stretchr/testify v1.8.4 diff --git a/go.sum b/go.sum index 29230581e..82576ccbc 100644 --- a/go.sum +++ b/go.sum @@ -257,10 +257,10 @@ github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1y github.com/onsi/gomega v1.17.0/go.mod h1:HnhC7FXeEQY45zxNK3PPoIUhzk/80Xly9PcubAlGdZY= github.com/onsi/gomega v1.18.1 h1:M1GfJqGRrBrrGGsbxzV5dqM2U2ApXefZCQpkukxYRLE= github.com/onsi/gomega v1.18.1/go.mod h1:0q+aL8jAiMXy9hbwj2mr5GziHiwhAIQpFmmtT5hitRs= -github.com/openimsdk/protocol v0.0.59 h1:eINLjWs4pnBl33JccwHV5qUYCRm3UWk+1ED4HbDENC8= -github.com/openimsdk/protocol v0.0.59/go.mod h1:OZQA9FR55lseYoN2Ql1XAHYKHJGu7OMNkUbuekrKCM8= -github.com/openimsdk/tools v0.0.47-alpha.10 h1:bel44PB4xcC1uO+1y/LYhgsPmAGpxrlNd8JaFL4yc50= -github.com/openimsdk/tools v0.0.47-alpha.10/go.mod h1:mUsH+ANKbdmhUih43ijJHvuYcU8owm7X3kdFH7FsIec= +github.com/openimsdk/protocol v0.0.60 h1:iz5ORGpGupkaygLTdODogUb3E4jJzSixIXMJiRkc1qs= +github.com/openimsdk/protocol v0.0.60/go.mod h1:OZQA9FR55lseYoN2Ql1XAHYKHJGu7OMNkUbuekrKCM8= +github.com/openimsdk/tools v0.0.47-alpha.11 h1:VioqYz2zPjoOalvdy63Ryjqnp9cwpP1p2oOoej+pbdI= +github.com/openimsdk/tools v0.0.47-alpha.11/go.mod h1:aTh3aPbr4bibBGf0qnC3nTWC947xFOrvHsW6WZ1oV3E= github.com/pelletier/go-toml/v2 v2.0.8 h1:0ctb6s9mE31h0/lhu+J6OPmVeDxJn+kYnJc2jZR9tGQ= github.com/pelletier/go-toml/v2 v2.0.8/go.mod h1:vuYfssBdrU2XDZ9bYydBu6t+6a6PYNcZljzZR9VXg+4= github.com/pierrec/lz4/v4 v4.1.21 h1:yOVMLb6qSIDP67pl/5F7RepeKYu/VmTyEXvuMI5d9mQ= diff --git a/internal/api/third.go b/internal/api/third.go index 9489c4c07..6baa70ee5 100644 --- a/internal/api/third.go +++ b/internal/api/third.go @@ -15,9 +15,13 @@ package api import ( + "context" + "google.golang.org/grpc" "math/rand" "net/http" + "net/url" "strconv" + "strings" "github.com/gin-gonic/gin" "github.com/openimsdk/open-im-server/v3/pkg/rpcclient" @@ -43,6 +47,35 @@ func (o *ThirdApi) SetAppBadge(c *gin.Context) { // #################### s3 #################### +func setURLPrefixOption[A, B, C any](_ func(client C, ctx context.Context, req *A, options ...grpc.CallOption) (*B, error), fn func(*A) error) *a2r.Option[A, B] { + return &a2r.Option[A, B]{ + BindAfter: fn, + } +} + +func setURLPrefix(c *gin.Context, urlPrefix *string) error { + host := c.GetHeader("X-Request-Api") + if host != "" { + if strings.HasSuffix(host, "/") { + *urlPrefix = host + "object/" + return nil + } else { + *urlPrefix = host + "/object/" + return nil + } + } + u := url.URL{ + Scheme: "http", + Host: c.Request.Host, + Path: "/object/", + } + if c.Request.TLS != nil { + u.Scheme = "https" + } + *urlPrefix = u.String() + return nil +} + func (o *ThirdApi) PartLimit(c *gin.Context) { a2r.Call(third.ThirdClient.PartLimit, o.Client, c) } @@ -52,7 +85,10 @@ func (o *ThirdApi) PartSize(c *gin.Context) { } func (o *ThirdApi) InitiateMultipartUpload(c *gin.Context) { - a2r.Call(third.ThirdClient.InitiateMultipartUpload, o.Client, c) + opt := setURLPrefixOption(third.ThirdClient.InitiateMultipartUpload, func(req *third.InitiateMultipartUploadReq) error { + return setURLPrefix(c, &req.UrlPrefix) + }) + a2r.Call(third.ThirdClient.InitiateMultipartUpload, o.Client, c, opt) } func (o *ThirdApi) AuthSign(c *gin.Context) { @@ -60,7 +96,10 @@ func (o *ThirdApi) AuthSign(c *gin.Context) { } func (o *ThirdApi) CompleteMultipartUpload(c *gin.Context) { - a2r.Call(third.ThirdClient.CompleteMultipartUpload, o.Client, c) + opt := setURLPrefixOption(third.ThirdClient.CompleteMultipartUpload, func(req *third.CompleteMultipartUploadReq) error { + return setURLPrefix(c, &req.UrlPrefix) + }) + a2r.Call(third.ThirdClient.CompleteMultipartUpload, o.Client, c, opt) } func (o *ThirdApi) AccessURL(c *gin.Context) { @@ -72,7 +111,10 @@ func (o *ThirdApi) InitiateFormData(c *gin.Context) { } func (o *ThirdApi) CompleteFormData(c *gin.Context) { - a2r.Call(third.ThirdClient.CompleteFormData, o.Client, c) + opt := setURLPrefixOption(third.ThirdClient.CompleteFormData, func(req *third.CompleteFormDataReq) error { + return setURLPrefix(c, &req.UrlPrefix) + }) + a2r.Call(third.ThirdClient.CompleteFormData, o.Client, c, opt) } func (o *ThirdApi) ObjectRedirect(c *gin.Context) { diff --git a/internal/rpc/msg/verify.go b/internal/rpc/msg/verify.go index 6dbdb5b33..c9605d8b4 100644 --- a/internal/rpc/msg/verify.go +++ b/internal/rpc/msg/verify.go @@ -28,7 +28,6 @@ import ( "github.com/openimsdk/protocol/msg" "github.com/openimsdk/protocol/sdkws" "github.com/openimsdk/tools/errs" - "github.com/openimsdk/tools/log" ) var ExcludeContentType = []int{constant.HasReadReceipt} @@ -186,7 +185,6 @@ func GetMsgID(sendID string) string { } func (m *msgServer) modifyMessageByUserMessageReceiveOpt(ctx context.Context, userID, conversationID string, sessionType int, pb *msg.SendMsgReq) (bool, error) { - defer log.ZDebug(ctx, "modifyMessageByUserMessageReceiveOpt return") opt, err := m.UserLocalCache.GetUserGlobalMsgRecvOpt(ctx, userID) if err != nil { return false, err diff --git a/internal/rpc/third/s3.go b/internal/rpc/third/s3.go index 882d3576e..21d982268 100644 --- a/internal/rpc/third/s3.go +++ b/internal/rpc/third/s3.go @@ -53,7 +53,6 @@ func (t *thirdServer) PartSize(ctx context.Context, req *third.PartSizeReq) (*th } func (t *thirdServer) InitiateMultipartUpload(ctx context.Context, req *third.InitiateMultipartUploadReq) (*third.InitiateMultipartUploadResp, error) { - defer log.ZDebug(ctx, "return") if err := t.checkUploadName(ctx, req.Name); err != nil { return nil, err } @@ -75,7 +74,7 @@ func (t *thirdServer) InitiateMultipartUpload(ctx context.Context, req *third.In return nil, err } return &third.InitiateMultipartUploadResp{ - Url: t.apiAddress(obj.Name), + Url: t.apiAddress(req.UrlPrefix, obj.Name), }, nil } return nil, err @@ -108,7 +107,6 @@ func (t *thirdServer) InitiateMultipartUpload(ctx context.Context, req *third.In } func (t *thirdServer) AuthSign(ctx context.Context, req *third.AuthSignReq) (*third.AuthSignResp, error) { - defer log.ZDebug(ctx, "return") partNumbers := datautil.Slice(req.PartNumbers, func(partNumber int32) int { return int(partNumber) }) result, err := t.s3dataBase.AuthSign(ctx, req.UploadID, partNumbers) if err != nil { @@ -132,7 +130,6 @@ func (t *thirdServer) AuthSign(ctx context.Context, req *third.AuthSignReq) (*th } func (t *thirdServer) CompleteMultipartUpload(ctx context.Context, req *third.CompleteMultipartUploadReq) (*third.CompleteMultipartUploadResp, error) { - defer log.ZDebug(ctx, "return") if err := t.checkUploadName(ctx, req.Name); err != nil { return nil, err } @@ -154,7 +151,7 @@ func (t *thirdServer) CompleteMultipartUpload(ctx context.Context, req *third.Co return nil, err } return &third.CompleteMultipartUploadResp{ - Url: t.apiAddress(obj.Name), + Url: t.apiAddress(req.UrlPrefix, obj.Name), }, nil } @@ -279,11 +276,11 @@ func (t *thirdServer) CompleteFormData(ctx context.Context, req *third.CompleteF if err := t.s3dataBase.SetObject(ctx, obj); err != nil { return nil, err } - return &third.CompleteFormDataResp{Url: t.apiAddress(mate.Name)}, nil + return &third.CompleteFormDataResp{Url: t.apiAddress(req.UrlPrefix, mate.Name)}, nil } -func (t *thirdServer) apiAddress(name string) string { - return t.apiURL + name +func (t *thirdServer) apiAddress(prefix, name string) string { + return prefix + name } type FormDataMate struct { diff --git a/pkg/common/db/controller/friend.go b/pkg/common/db/controller/friend.go index 0dcd59052..49136f228 100644 --- a/pkg/common/db/controller/friend.go +++ b/pkg/common/db/controller/friend.go @@ -212,7 +212,6 @@ func (f *friendDatabase) RefuseFriendRequest(ctx context.Context, friendRequest // AgreeFriendRequest accepts a friend request. It first checks for an existing, unprocessed request. func (f *friendDatabase) AgreeFriendRequest(ctx context.Context, friendRequest *relation.FriendRequestModel) (err error) { return f.tx.Transaction(ctx, func(ctx context.Context) error { - defer log.ZDebug(ctx, "return line") now := time.Now() fr, err := f.friendRequest.Take(ctx, friendRequest.FromUserID, friendRequest.ToUserID) if err != nil { diff --git a/pkg/rpcclient/notification/group.go b/pkg/rpcclient/notification/group.go index 7900d0274..8cb3cf97c 100644 --- a/pkg/rpcclient/notification/group.go +++ b/pkg/rpcclient/notification/group.go @@ -288,7 +288,6 @@ func (g *GroupNotificationSender) fillOpUser(ctx context.Context, opUser **sdkws } func (g *GroupNotificationSender) GroupCreatedNotification(ctx context.Context, tips *sdkws.GroupCreatedTips) (err error) { - defer log.ZDebug(ctx, "return") defer func() { if err != nil { log.ZError(ctx, stringutil.GetFuncName(1)+" failed", err) @@ -301,7 +300,6 @@ func (g *GroupNotificationSender) GroupCreatedNotification(ctx context.Context, } func (g *GroupNotificationSender) GroupInfoSetNotification(ctx context.Context, tips *sdkws.GroupInfoSetTips) (err error) { - defer log.ZDebug(ctx, "return") defer func() { if err != nil { log.ZError(ctx, stringutil.GetFuncName(1)+" failed", err) @@ -314,7 +312,6 @@ func (g *GroupNotificationSender) GroupInfoSetNotification(ctx context.Context, } func (g *GroupNotificationSender) GroupInfoSetNameNotification(ctx context.Context, tips *sdkws.GroupInfoSetNameTips) (err error) { - defer log.ZDebug(ctx, "return") defer func() { if err != nil { log.ZError(ctx, stringutil.GetFuncName(1)+" failed", err) @@ -327,7 +324,6 @@ func (g *GroupNotificationSender) GroupInfoSetNameNotification(ctx context.Conte } func (g *GroupNotificationSender) GroupInfoSetAnnouncementNotification(ctx context.Context, tips *sdkws.GroupInfoSetAnnouncementTips) (err error) { - defer log.ZDebug(ctx, "return") defer func() { if err != nil { log.ZError(ctx, stringutil.GetFuncName(1)+" failed", err) @@ -340,7 +336,6 @@ func (g *GroupNotificationSender) GroupInfoSetAnnouncementNotification(ctx conte } func (g *GroupNotificationSender) JoinGroupApplicationNotification(ctx context.Context, req *pbgroup.JoinGroupReq) (err error) { - defer log.ZDebug(ctx, "return") defer func() { if err != nil { log.ZError(ctx, stringutil.GetFuncName(1)+" failed", err) @@ -370,7 +365,6 @@ func (g *GroupNotificationSender) JoinGroupApplicationNotification(ctx context.C } func (g *GroupNotificationSender) MemberQuitNotification(ctx context.Context, member *sdkws.GroupMemberFullInfo) (err error) { - defer log.ZDebug(ctx, "return") defer func() { if err != nil { log.ZError(ctx, stringutil.GetFuncName(1)+" failed", err) @@ -385,7 +379,6 @@ func (g *GroupNotificationSender) MemberQuitNotification(ctx context.Context, me } func (g *GroupNotificationSender) GroupApplicationAcceptedNotification(ctx context.Context, req *pbgroup.GroupApplicationResponseReq) (err error) { - defer log.ZDebug(ctx, "return") defer func() { if err != nil { log.ZError(ctx, stringutil.GetFuncName(1)+" failed", err) @@ -418,7 +411,6 @@ func (g *GroupNotificationSender) GroupApplicationAcceptedNotification(ctx conte } func (g *GroupNotificationSender) GroupApplicationRejectedNotification(ctx context.Context, req *pbgroup.GroupApplicationResponseReq) (err error) { - defer log.ZDebug(ctx, "return") defer func() { if err != nil { log.ZError(ctx, stringutil.GetFuncName(1)+" failed", err) @@ -451,7 +443,6 @@ func (g *GroupNotificationSender) GroupApplicationRejectedNotification(ctx conte } func (g *GroupNotificationSender) GroupOwnerTransferredNotification(ctx context.Context, req *pbgroup.TransferGroupOwnerReq) (err error) { - defer log.ZDebug(ctx, "return") defer func() { if err != nil { log.ZError(ctx, stringutil.GetFuncName(1)+" failed", err) @@ -474,7 +465,6 @@ func (g *GroupNotificationSender) GroupOwnerTransferredNotification(ctx context. } func (g *GroupNotificationSender) MemberKickedNotification(ctx context.Context, tips *sdkws.MemberKickedTips) (err error) { - defer log.ZDebug(ctx, "return") defer func() { if err != nil { log.ZError(ctx, stringutil.GetFuncName(1)+" failed", err) @@ -487,7 +477,6 @@ func (g *GroupNotificationSender) MemberKickedNotification(ctx context.Context, } func (g *GroupNotificationSender) MemberInvitedNotification(ctx context.Context, groupID, reason string, invitedUserIDList []string) (err error) { - defer log.ZDebug(ctx, "return") defer func() { if err != nil { log.ZError(ctx, stringutil.GetFuncName(1)+" failed", err) @@ -512,7 +501,6 @@ func (g *GroupNotificationSender) MemberInvitedNotification(ctx context.Context, } func (g *GroupNotificationSender) MemberEnterNotification(ctx context.Context, groupID string, entrantUserID string) (err error) { - defer log.ZDebug(ctx, "return") defer func() { if err != nil { log.ZError(ctx, stringutil.GetFuncName(1)+" failed", err) @@ -531,7 +519,6 @@ func (g *GroupNotificationSender) MemberEnterNotification(ctx context.Context, g } func (g *GroupNotificationSender) GroupDismissedNotification(ctx context.Context, tips *sdkws.GroupDismissedTips) (err error) { - defer log.ZDebug(ctx, "return") defer func() { if err != nil { log.ZError(ctx, stringutil.GetFuncName(1)+" failed", err) @@ -544,7 +531,6 @@ func (g *GroupNotificationSender) GroupDismissedNotification(ctx context.Context } func (g *GroupNotificationSender) GroupMemberMutedNotification(ctx context.Context, groupID, groupMemberUserID string, mutedSeconds uint32) (err error) { - defer log.ZDebug(ctx, "return") defer func() { if err != nil { log.ZError(ctx, stringutil.GetFuncName(1)+" failed", err) @@ -569,7 +555,6 @@ func (g *GroupNotificationSender) GroupMemberMutedNotification(ctx context.Conte } func (g *GroupNotificationSender) GroupMemberCancelMutedNotification(ctx context.Context, groupID, groupMemberUserID string) (err error) { - defer log.ZDebug(ctx, "return") defer func() { if err != nil { log.ZError(ctx, stringutil.GetFuncName(1)+" failed", err) @@ -591,7 +576,6 @@ func (g *GroupNotificationSender) GroupMemberCancelMutedNotification(ctx context } func (g *GroupNotificationSender) GroupMutedNotification(ctx context.Context, groupID string) (err error) { - defer log.ZDebug(ctx, "return") defer func() { if err != nil { log.ZError(ctx, stringutil.GetFuncName(1)+" failed", err) @@ -616,7 +600,6 @@ func (g *GroupNotificationSender) GroupMutedNotification(ctx context.Context, gr } func (g *GroupNotificationSender) GroupCancelMutedNotification(ctx context.Context, groupID string) (err error) { - defer log.ZDebug(ctx, "return") defer func() { if err != nil { log.ZError(ctx, stringutil.GetFuncName(1)+" failed", err) @@ -673,7 +656,6 @@ func (g *GroupNotificationSender) GroupMemberSetToAdminNotification(ctx context. } func (g *GroupNotificationSender) GroupMemberSetToOrdinaryUserNotification(ctx context.Context, groupID, groupMemberUserID string) (err error) { - defer log.ZDebug(ctx, "return") defer func() { if err != nil { log.ZError(ctx, stringutil.GetFuncName(1)+" failed", err) @@ -695,7 +677,6 @@ func (g *GroupNotificationSender) GroupMemberSetToOrdinaryUserNotification(ctx c } func (g *GroupNotificationSender) SuperGroupNotification(ctx context.Context, sendID, recvID string) (err error) { - defer log.ZDebug(ctx, "return") defer func() { if err != nil { log.ZError(ctx, stringutil.GetFuncName(1)+" failed", err)