-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmeta.go
103 lines (78 loc) · 2.44 KB
/
meta.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
package meta
import (
"context"
"net/http"
"github.com/alexfalkowski/go-service/meta"
"github.com/alexfalkowski/go-service/security/header"
m "github.com/alexfalkowski/go-service/transport/meta"
"github.com/google/uuid"
"github.com/ulule/limiter/v3"
)
// Handler for meta.
type Handler struct {
userAgent string
}
// NewHandler for meta.
func NewHandler(userAgent string) *Handler {
return &Handler{userAgent: userAgent}
}
func (h *Handler) ServeHTTP(resp http.ResponseWriter, req *http.Request, next http.HandlerFunc) {
ctx := req.Context()
ctx = m.WithUserAgent(ctx, extractUserAgent(ctx, req, h.userAgent))
ctx = m.WithRequestID(ctx, extractRequestID(ctx, req))
ctx = m.WithIPAddr(ctx, extractIP(req))
ctx = m.WithAuthorization(ctx, extractAuthorization(ctx, req))
next(resp, req.WithContext(ctx))
}
// NewRoundTripper for meta.
func NewRoundTripper(userAgent string, hrt http.RoundTripper) *RoundTripper {
return &RoundTripper{userAgent: userAgent, RoundTripper: hrt}
}
// RoundTripper for meta.
type RoundTripper struct {
http.RoundTripper
userAgent string
}
func (r *RoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
ctx := req.Context()
userAgent := extractUserAgent(ctx, req, r.userAgent)
req.Header.Set("User-Agent", userAgent.Value())
ctx = m.WithUserAgent(ctx, userAgent)
requestID := extractRequestID(ctx, req)
req.Header.Set("Request-ID", requestID.Value())
ctx = m.WithRequestID(ctx, requestID)
return r.RoundTripper.RoundTrip(req.WithContext(ctx))
}
func extractUserAgent(ctx context.Context, req *http.Request, userAgent string) meta.Valuer {
if ua := m.UserAgent(ctx); ua != nil {
return ua
}
if ua := req.Header.Get("User-Agent"); ua != "" {
return meta.String(ua)
}
return meta.String(userAgent)
}
func extractRequestID(ctx context.Context, req *http.Request) meta.Valuer {
if id := m.RequestID(ctx); id != nil {
return id
}
if id := req.Header.Get("Request-ID"); id != "" {
return meta.String(id)
}
return meta.ToString(uuid.New())
}
func extractIP(req *http.Request) meta.Valuer {
return meta.ToIgnored(limiter.GetIP(req, limiter.Options{TrustForwardHeader: true}))
}
func extractAuthorization(ctx context.Context, req *http.Request) meta.Valuer {
a := req.Header.Get("Authorization")
if a == "" {
return meta.Blank()
}
_, t, err := header.ParseAuthorization(a)
if err != nil {
meta.WithAttribute(ctx, "authError", meta.Error(err))
return meta.Blank()
}
return meta.Ignored(t)
}