Last active
September 14, 2018 08:11
-
-
Save imrenagi/efaa05c9125536650fa7e3aa731b4bc2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ( | |
mt "github.com/micro/go-micro/metadata" | |
) | |
// get user info from context passed by auth middleware | |
func getUserInfo(incomingContext context.Context) map[string]string { | |
userMap := map[string]string{} | |
value := incomingContext.Value(constant.UserMetaKey) | |
if value != nil { | |
userInfo := value.(map[string]interface{}) | |
for k, v := range userInfo { | |
userMap[k] = v.(string) | |
} | |
} | |
return userMap | |
} | |
// To inject the user info to GRPC context | |
func (r *pastPaymentResolver) Payments(ctx context.Context, obj *models.PastPayment, last int, currency string, after *string) (models.PaymentConnection, error) { | |
.... | |
.... | |
// create new context by augmenting user info into it | |
// need to use metadata in go-micro instead of golang grpc | |
ctx = mt.NewContext(ctx, getUserInfo(ctx)) | |
... | |
} | |
//===================================EOF======================================= | |
import "github.com/micro/go-micro/metadata" | |
// read the context in payment services | |
func (p *PaymentService) GetLastPaymentMethods(c context.Context, req *payment.RequestWithCurrency, res *payment.Response) error { | |
.... | |
var clientId string | |
md, ok := metadata.FromContext(c) | |
if ok { | |
clientId = md["Clientid"] | |
} else { | |
localLogger.WithError(ErrNotContainClientId).Errorf("Can't get context from grpc request context") | |
return ErrNotContainClientId | |
} | |
.... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment