Skip to content

Commit

Permalink
grpc: Use constants for metadata keys
Browse files Browse the repository at this point in the history
  • Loading branch information
monstermunchkin committed Aug 29, 2024
1 parent f59bc40 commit 22a9f06
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
6 changes: 3 additions & 3 deletions grpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,13 @@ func dialCtx(ctx context.Context, addr string) (*grpc.ClientConn, error) {

func prepareClientContext(ctx context.Context) context.Context {
if loc, ok := locale.FromCtx(ctx); ok {
ctx = metadata.AppendToOutgoingContext(ctx, "locale", loc.Serialize())
ctx = metadata.AppendToOutgoingContext(ctx, MetadataKeyLocale, loc.Serialize())
}
if token, ok := security.GetTokenFromContext(ctx); ok {
ctx = metadata.AppendToOutgoingContext(ctx, "bearer_token", token.GetValue())
ctx = metadata.AppendToOutgoingContext(ctx, MetadataKeyBearerToken, token.GetValue())
}
if reqID := log.RequestIDFromContext(ctx); reqID != "" {
ctx = metadata.AppendToOutgoingContext(ctx, "req_id", reqID)
ctx = metadata.AppendToOutgoingContext(ctx, MetadataKeyRequestID, reqID)
}
ctx = EncodeContextWithUTMData(ctx)

Expand Down
4 changes: 4 additions & 0 deletions grpc/consts.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package grpc

const (
MetadataKeyBearerToken = "bearer_token"
MetadataKeyContentType = "content-type"
MetadataKeyExternalDependencies = "external_dependencies"
MetadataKeyLocale = "locale"
MetadataKeyRequestID = "req_id"
)
14 changes: 7 additions & 7 deletions grpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func prepareContext(ctx context.Context) (context.Context, metadata.MD) {

// add request context if req_id is given
var reqID xid.ID
if ri := md.Get("req_id"); len(ri) > 0 {
if ri := md.Get(MetadataKeyRequestID); len(ri) > 0 {
var err error
reqID, err = xid.FromString(ri[0])
if err != nil {
Expand All @@ -175,7 +175,7 @@ func prepareContext(ctx context.Context) (context.Context, metadata.MD) {
})

// handle locale
if l := md.Get("locale"); len(l) > 0 {
if l := md.Get(MetadataKeyLocale); len(l) > 0 {
loc, err := locale.ParseLocale(l[0])
if err != nil {
log.Ctx(ctx).Debug().Err(err).Msgf("unable to parse locale: %v", err)
Expand All @@ -187,7 +187,7 @@ func prepareContext(ctx context.Context) (context.Context, metadata.MD) {
ctx = ContextWithUTMFromMetadata(ctx, md)

// add security context if bearer token is given
if bt := md.Get("bearer_token"); len(bt) > 0 {
if bt := md.Get(MetadataKeyBearerToken); len(bt) > 0 {
ctx = security.ContextWithToken(ctx, security.TokenString(bt[0]))
}

Expand All @@ -198,10 +198,10 @@ func prepareContext(ctx context.Context) (context.Context, metadata.MD) {
ctx = middleware.ContextWithExternalDependency(ctx, &externalDependencyContext)
}

delete(md, "content-type")
delete(md, "locale")
delete(md, "bearer_token")
delete(md, "req_id")
delete(md, MetadataKeyContentType)
delete(md, MetadataKeyLocale)
delete(md, MetadataKeyBearerToken)
delete(md, MetadataKeyRequestID)
delete(md, MetadataKeyExternalDependencies)

return ctx, md
Expand Down

0 comments on commit 22a9f06

Please sign in to comment.