Skip to content

Commit

Permalink
Merge pull request #1 from e-conomic/custom-ocr-cache-header
Browse files Browse the repository at this point in the history
Added custom OCR cache header
  • Loading branch information
brunsgaard authored Jun 7, 2019
2 parents 264e47e + b11b2dd commit b208e73
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions ctxvml.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ctxvml

import (
"context"
"strings"

grpc_ctxtags "github.com/grpc-ecosystem/go-grpc-middleware/tags"
"google.golang.org/grpc"
Expand All @@ -13,6 +14,11 @@ type ctxMarker struct{}
// VmlHeaders contains vml http headers.
type VmlHeaders struct {
Username string
OcrCache string
}

func (h VmlHeaders) OcrCacheAllow() bool {
return strings.EqualFold(h.OcrCache, "allow")
}

var (
Expand Down Expand Up @@ -53,11 +59,16 @@ func (ss serverStreamWithContext) Context() context.Context {
func extractMetadataToContext(ctx context.Context) context.Context {
md, _ := metadata.FromIncomingContext(ctx)
headers := VmlHeaders{}
if mdValue, ok := md["vml-username"]; ok {
if mdValue, ok := md["vml-username"]; ok && len(mdValue) != 0 {
headers.Username = mdValue[0]
grpc_ctxtags.Extract(ctx).Set("username", mdValue[0])
ctx = context.WithValue(ctx, ctxMarkerKey, headers)
}
if mdValue, ok := md["vml-ocr-cache"]; ok && len(mdValue) != 0 {
headers.OcrCache = mdValue[0]
}
if headers.Username != "" {
grpc_ctxtags.Extract(ctx).Set("username", headers.Username)
}
ctx = context.WithValue(ctx, ctxMarkerKey, headers)
return ctx
}

Expand Down Expand Up @@ -92,6 +103,7 @@ func packCallerMetadata(ctx context.Context) map[string]string {
var md = map[string]string{}
headers := Extract(ctx)
md["vml-username"] = headers.Username
md["vml-ocr-cache"] = headers.OcrCache
return md
}

Expand Down

0 comments on commit b208e73

Please sign in to comment.