-
Notifications
You must be signed in to change notification settings - Fork 7
/
native.go
163 lines (138 loc) · 4.43 KB
/
native.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package apitoolkit
import (
"bytes"
"context"
"io"
"log"
"net/http"
"net/http/httptest"
"time"
"github.com/go-chi/chi/v5"
"github.com/google/uuid"
"github.com/gorilla/mux"
)
// Middleware collects request, response parameters and publishes the payload
func (c *Client) Middleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
msgID := uuid.Must(uuid.NewRandom())
newCtx := context.WithValue(req.Context(), CurrentRequestMessageID, msgID)
errorList := []ATError{}
newCtx = context.WithValue(newCtx, ErrorListCtxKey, &errorList)
newCtx = context.WithValue(newCtx, CurrentClient, c)
req = req.WithContext(newCtx)
reqBuf, _ := io.ReadAll(req.Body)
req.Body.Close()
req.Body = io.NopCloser(bytes.NewBuffer(reqBuf))
rec := httptest.NewRecorder()
start := time.Now()
next.ServeHTTP(rec, req)
recRes := rec.Result()
// io.Copy(res, recRes.Body)
for k, v := range recRes.Header {
for _, vv := range v {
res.Header().Add(k, vv)
}
}
resBody, _ := io.ReadAll(recRes.Body)
res.WriteHeader(recRes.StatusCode)
res.Write(resBody)
payload := c.buildPayload(GoDefaultSDKType, start,
req, recRes.StatusCode,
reqBuf, resBody, recRes.Header, nil, req.URL.Path,
c.config.RedactHeaders, c.config.RedactRequestBody, c.config.RedactResponseBody,
errorList,
msgID,
nil,
)
c.PublishMessage(req.Context(), payload)
})
}
// GorillaMuxMiddleware is for the gorilla mux routing library and collects request, response parameters and publishes the payload
func (c *Client) GorillaMuxMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
msgID := uuid.Must(uuid.NewRandom())
newCtx := context.WithValue(req.Context(), CurrentRequestMessageID, msgID)
errorList := []ATError{}
newCtx = context.WithValue(newCtx, ErrorListCtxKey, &errorList)
newCtx = context.WithValue(newCtx, CurrentClient, c)
req = req.WithContext(newCtx)
reqBuf, _ := io.ReadAll(req.Body)
req.Body.Close()
req.Body = io.NopCloser(bytes.NewBuffer(reqBuf))
rec := httptest.NewRecorder()
start := time.Now()
next.ServeHTTP(rec, req)
recRes := rec.Result()
for k, v := range recRes.Header {
for _, vv := range v {
res.Header().Add(k, vv)
}
}
resBody, _ := io.ReadAll(recRes.Body)
res.WriteHeader(recRes.StatusCode)
res.Write(resBody)
route := mux.CurrentRoute(req)
pathTmpl, _ := route.GetPathTemplate()
vars := mux.Vars(req)
payload := c.buildPayload(GoGorillaMux, start,
req, recRes.StatusCode,
reqBuf, resBody, recRes.Header, vars, pathTmpl,
c.config.RedactHeaders, c.config.RedactRequestBody, c.config.RedactResponseBody,
errorList,
msgID,
nil,
)
err := c.PublishMessage(req.Context(), payload)
if err != nil {
if c.config.Debug {
log.Println("APIToolkit: unable to publish request payload to pubsub.")
}
}
})
}
// ChiMiddleware is for the Golang Chi router and collects request, response parameters and publishes the payload
func (c *Client) ChiMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
msgID := uuid.Must(uuid.NewRandom())
newCtx := context.WithValue(req.Context(), CurrentRequestMessageID, msgID)
errorList := []ATError{}
newCtx = context.WithValue(newCtx, ErrorListCtxKey, &errorList)
req = req.WithContext(newCtx)
reqBuf, _ := io.ReadAll(req.Body)
req.Body.Close()
req.Body = io.NopCloser(bytes.NewBuffer(reqBuf))
rec := httptest.NewRecorder()
start := time.Now()
next.ServeHTTP(rec, req)
recRes := rec.Result()
for k, v := range recRes.Header {
for _, vv := range v {
res.Header().Add(k, vv)
}
}
resBody, _ := io.ReadAll(recRes.Body)
res.WriteHeader(recRes.StatusCode)
res.Write(resBody)
chiCtx := chi.RouteContext(req.Context())
vars := map[string]string{}
for i, key := range chiCtx.URLParams.Keys {
if len(chiCtx.URLParams.Values) > i {
vars[key] = chiCtx.URLParams.Values[i]
}
}
payload := c.buildPayload(GoGorillaMux, start,
req, recRes.StatusCode,
reqBuf, resBody, recRes.Header, vars, chiCtx.RoutePattern(),
c.config.RedactHeaders, c.config.RedactRequestBody, c.config.RedactResponseBody,
errorList,
msgID,
nil,
)
err := c.PublishMessage(req.Context(), payload)
if err != nil {
if c.config.Debug {
log.Println("APIToolkit: unable to publish request payload to pubsub.")
}
}
})
}