dcsunny 4 жил өмнө
parent
commit
f651d8565d

+ 14 - 0
http/context/context.go

@@ -0,0 +1,14 @@
+package context
+
+import (
+	"context"
+	"fmt"
+
+	"google.golang.org/grpc/metadata"
+)
+
+func AppendToContext(ctx context.Context, key string, value interface{}) context.Context {
+	ctx = context.WithValue(ctx, key, value)
+	ctx = metadata.AppendToOutgoingContext(ctx, key, fmt.Sprint(value))
+	return ctx
+}

+ 8 - 6
http/middleware/rpc_value.go

@@ -6,6 +6,8 @@ import (
 	"net/url"
 	"strconv"
 
+	context2 "git.ikuban.com/server/kratos-utils/http/context"
+
 	"google.golang.org/grpc/metadata"
 
 	"github.com/go-kratos/kratos/v2/middleware"
@@ -17,35 +19,35 @@ func GrpcValue(handler middleware.Handler) middleware.Handler {
 			userID := md.Get("user_id")
 			if len(userID) > 0 {
 				_userID, _ := strconv.ParseInt(userID[0], 10, 64)
-				ctx = context.WithValue(ctx, "user_id", _userID)
+				ctx = context2.AppendToContext(ctx, "user_id", _userID)
 			}
 
 			claimMap := md.Get("jwt_claims")
 			if len(claimMap) > 0 {
 				_claimMap := json.RawMessage(claimMap[0])
-				ctx = context.WithValue(ctx, "jwt_claims", _claimMap)
+				ctx = context2.AppendToContext(ctx, "jwt_claims", _claimMap)
 			}
 			token := md.Get("auth_token")
 			if len(token) > 0 {
-				ctx = context.WithValue(ctx, "auth_token", token[0])
+				ctx = context2.AppendToContext(ctx, "auth_token", token[0])
 			}
 
 			page := md.Get("page")
 			if len(page) > 0 {
 				_page, _ := strconv.ParseInt(page[0], 10, 64)
-				ctx = context.WithValue(ctx, "page", _page)
+				ctx = context2.AppendToContext(ctx, "page", _page)
 			}
 
 			pageSize := md.Get("page_size")
 			if len(pageSize) > 0 {
 				_pageSize, _ := strconv.ParseInt(pageSize[0], 10, 64)
-				ctx = context.WithValue(ctx, "page_size", _pageSize)
+				ctx = context2.AppendToContext(ctx, "page_size", _pageSize)
 			}
 
 			query := md.Get("query")
 			if len(query) > 0 {
 				_query, _ := url.ParseQuery(query[0])
-				ctx = context.WithValue(ctx, "query", _query)
+				ctx = context2.AppendToContext(ctx, "query", _query)
 			}
 		}
 		return handler(ctx, req)