-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext_helpers.go
More file actions
95 lines (83 loc) · 2.31 KB
/
Copy pathcontext_helpers.go
File metadata and controls
95 lines (83 loc) · 2.31 KB
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
package servex
import (
"crypto/rand"
"math"
mr "math/rand"
"net/http"
"strconv"
"strings"
"time"
)
// RequestID returns the request ID for the request.
func (ctx *Context) RequestID() string {
return getOrSetRequestID(ctx.r)
}
// TraceID returns the W3C trace ID for this request, or an empty string if
// trace propagation is not enabled.
func (ctx *Context) TraceID() string {
return getValueFromContext[string](ctx.r, traceIDKey{})
}
// SpanID returns the W3C span ID generated for this request, or an empty string if
// trace propagation is not enabled.
func (ctx *Context) SpanID() string {
return getValueFromContext[string](ctx.r, spanIDKey{})
}
// APIVersion returns the API version of the handler from the path.
// It returns an empty string if not found.
// Example:
//
// // Route definition: server.GET("/api/v1/users", handler)
// // Request: GET /api/v1/users
//
// func handler(w http.ResponseWriter, r *http.Request) {
// ctx := servex.C(w, r)
// version := ctx.APIVersion() // "v1"
// }
func (ctx *Context) APIVersion() string {
for s := range strings.SplitSeq(ctx.r.URL.Path, "/") {
if len(s) > 1 && s[0] == 'v' {
if _, err := strconv.Atoi(s[1:]); err == nil {
return s
}
}
}
return ""
}
// NoLog marks to not log the request after returning from the handler.
func (ctx *Context) NoLog() {
ctx.setNoLog()
}
// APIKeyScopes returns the API key scopes stored in the request context.
// Returns nil if the request was not authenticated via an API key.
func (ctx *Context) APIKeyScopes() []string {
return getValueFromContext[[]string](ctx.r, APIKeyScopesContextKey{})
}
func getOrSetRequestID(r *http.Request) string {
rIDHeader := r.Header.Get("X-Request-ID")
if rIDHeader != "" {
return rIDHeader
}
requestID := getValueFromContext[string](r, requestIDKey{})
if requestID == "" {
requestID = string(getRandomBytes(12))
r.Header.Set("X-Request-ID", requestID)
}
return requestID
}
var (
defaultAlphabet = []byte("0123456789abcdef")
alphabetLen = uint8(len(defaultAlphabet))
)
func getRandomBytes(n int) []byte {
out := make([]byte, n)
if _, err := rand.Read(out); err != nil {
r := mr.New(mr.NewSource(time.Now().UnixNano()))
for i := range out {
out[i] = byte(r.Intn(math.MaxUint8))
}
}
for i := range out {
out[i] = defaultAlphabet[out[i]&(alphabetLen-1)]
}
return out
}