| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- package middleware
- import (
- "context"
- "encoding/json"
- "net/url"
- "strconv"
- context2 "git.ikuban.com/server/kratos-utils/v2/transport/http/context"
- "google.golang.org/grpc/metadata"
- "github.com/go-kratos/kratos/v2/middleware"
- )
- func GrpcValue(handler middleware.Handler) middleware.Handler {
- return func(ctx context.Context, req interface{}) (reply interface{}, err error) {
- if md, ok := metadata.FromIncomingContext(ctx); ok {
- userID := md.Get("user_id")
- if len(userID) > 0 {
- _userID, _ := strconv.ParseInt(userID[0], 10, 64)
- ctx = context2.AppendToContext(ctx, "user_id", _userID)
- }
- accountID := md.Get("account_id")
- if len(accountID) > 0 {
- _accountID, _ := strconv.ParseInt(accountID[0], 10, 64)
- ctx = context2.AppendToContext(ctx, "account_id", _accountID)
- }
- memID := md.Get("mem_id")
- if len(memID) > 0 {
- _memID, _ := strconv.ParseInt(memID[0], 10, 64)
- ctx = context2.AppendToContext(ctx, "mem_id", _memID)
- }
- claimMap := md.Get("jwt_claims")
- if len(claimMap) > 0 {
- _claimMap := json.RawMessage(claimMap[0])
- ctx = context2.AppendToContext(ctx, "jwt_claims", _claimMap)
- }
- token := md.Get("auth_token")
- if len(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 = context2.AppendToContext(ctx, "page", _page)
- }
- pageSize := md.Get("page_size")
- if len(pageSize) > 0 {
- _pageSize, _ := strconv.ParseInt(pageSize[0], 10, 64)
- ctx = context2.AppendToContext(ctx, "page_size", _pageSize)
- }
- query := md.Get("query")
- if len(query) > 0 {
- _query, _ := url.ParseQuery(query[0])
- ctx = context2.AppendToContext(ctx, "query", _query)
- }
- referer := md.Get("referer")
- if len(referer) > 0 {
- ctx = context2.AppendToContext(ctx, "referer", referer[0])
- }
- uri := md.Get("uri")
- if len(uri) > 0 {
- ctx = context2.AppendToContext(ctx, "uri", uri[0])
- }
- }
- return handler(ctx, req)
- }
- }
|