-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathmiddleware.go
58 lines (48 loc) · 1.84 KB
/
middleware.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// Copyright © 2021 by PACE Telematics GmbH. All rights reserved.
package grpc
import (
"context"
"encoding/gob"
"strings"
"github.com/pace/bricks/http/middleware"
"github.com/pace/bricks/pkg/tracking/utm"
"google.golang.org/grpc/metadata"
)
const utmMetadataKey = "utm-bin" // IMPORTANT -bin post-fix allows us to send binary data via grpc metadata, otherwise it will break the protocol
func ContextWithUTMFromMetadata(parentCtx context.Context, md metadata.MD) context.Context {
dataSlice := md.Get(utmMetadataKey)
if len(dataSlice) == 0 {
return parentCtx
}
var utmData utm.UTMData
if err := gob.NewDecoder(strings.NewReader(dataSlice[0])).Decode(&utmData); err != nil {
return parentCtx
}
return utm.ContextWithUTMData(parentCtx, utmData)
}
func EncodeContextWithUTMData(parentCtx context.Context) context.Context {
utmData, exists := utm.FromContext(parentCtx)
if !exists {
return parentCtx
}
w := strings.Builder{}
if err := gob.NewEncoder(&w).Encode(utmData); err != nil {
return parentCtx
}
return metadata.AppendToOutgoingContext(parentCtx, utmMetadataKey, w.String())
}
// AddExternalDependencyMetadataToContext adds external dependencies to the context if they are present in the metadata.
func AddExternalDependencyMetadataToContext(ctx context.Context, md metadata.MD) context.Context {
// If there are no external dependencies in the metadata, we can return the context as is.
externalDependencies := md.Get(MetadataKeyExternalDependencies)
if len(externalDependencies) == 0 {
return ctx
}
// If there are external dependencies in the metadata, we need to parse them and add them to the context.
edc := middleware.ExternalDependencyContextFromContext(ctx)
if edc == nil {
edc = &middleware.ExternalDependencyContext{}
}
edc.Parse(externalDependencies[0])
return middleware.ContextWithExternalDependency(ctx, edc)
}