-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcontext.go
137 lines (114 loc) · 3.87 KB
/
context.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package httplog
import (
"context"
"github.com/pkg/errors"
"github.com/rs/xid"
)
type contextKey string
func (c contextKey) String() string {
return "context key " + string(c)
}
// RequestID is a unique identifier for each inbound request
var (
requestID = contextKey("RequestID")
requestHost = contextKey("RequestHost")
requestPort = contextKey("RequestPort")
requestPath = contextKey("RequestPath")
requestRawQuery = contextKey("RequestRawQuery")
requestFragment = contextKey("RequestFragment")
)
func setRequest2Context(ctx context.Context, aud *tracker) context.Context {
ctx = setRequestHost(ctx, aud)
ctx = setRequestPort(ctx, aud)
ctx = setRequestPath(ctx, aud)
ctx = setRequestRawQuery(ctx, aud)
ctx = setRequestFragment(ctx, aud)
return ctx
}
// SetRequestID adds a unique ID as RequestID to the context
func setRequestID(ctx context.Context) context.Context {
// get byte Array representation of guid from xid package (12 bytes)
guid := xid.New()
// use the String method of the guid object to convert byte array to string (20 bytes)
rID := guid.String()
ctx = context.WithValue(ctx, requestID, rID)
return ctx
}
// RequestID gets the Request ID from the context.
func RequestID(ctx context.Context) (string, error) {
requestIDstr, ok := ctx.Value(requestID).(string)
if ok {
return requestIDstr, nil
}
return requestIDstr, errors.New("RequestID is not set properly to context")
}
// SetRequestHost adds the request host to the context
func setRequestHost(ctx context.Context, audit *tracker) context.Context {
host := audit.request.host
ctx = context.WithValue(ctx, requestHost, host)
return ctx
}
// RequestHost gets the request host from the context
func RequestHost(ctx context.Context) (string, error) {
url, ok := ctx.Value(requestHost).(string)
if ok {
return url, nil
}
return url, errors.New("RequestHost is not set properly to context")
}
// SetRequestPort adds the request port to the context
func setRequestPort(ctx context.Context, audit *tracker) context.Context {
port := audit.request.port
ctx = context.WithValue(ctx, requestPort, port)
return ctx
}
// RequestPort gets the request port from the context
func RequestPort(ctx context.Context) (string, error) {
url, ok := ctx.Value(requestPort).(string)
if ok {
return url, nil
}
return url, errors.New("RequestPort is not set properly to context")
}
// SetRequestPath adds the request URL to the context
func setRequestPath(ctx context.Context, audit *tracker) context.Context {
path := audit.request.path
ctx = context.WithValue(ctx, requestPath, path)
return ctx
}
// RequestPath gets the request URL from the context
func RequestPath(ctx context.Context) (string, error) {
url, ok := ctx.Value(requestPath).(string)
if ok {
return url, nil
}
return url, errors.New("RequestPath is not set properly to context")
}
// SetRequestRawQuery adds the request Query string details to the context
func setRequestRawQuery(ctx context.Context, audit *tracker) context.Context {
query := audit.request.rawQuery
ctx = context.WithValue(ctx, requestRawQuery, query)
return ctx
}
// RequestRawQuery gets the request Query string details from the context
func RequestRawQuery(ctx context.Context) (string, error) {
url, ok := ctx.Value(requestRawQuery).(string)
if ok {
return url, nil
}
return url, errors.New("RequestRawQuery is not set properly to context")
}
// SetRequestFragment adds the request Fragment details to the context
func setRequestFragment(ctx context.Context, audit *tracker) context.Context {
frag := audit.request.fragment
ctx = context.WithValue(ctx, requestFragment, frag)
return ctx
}
// RequestFragment gets the request Fragment details from the context
func RequestFragment(ctx context.Context) (string, error) {
url, ok := ctx.Value(requestFragment).(string)
if ok {
return url, nil
}
return url, errors.New("RequestFragment is not set properly to context")
}