-
Notifications
You must be signed in to change notification settings - Fork 0
/
stdout.go
57 lines (46 loc) · 1.03 KB
/
stdout.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
package httpdebug
import (
"bytes"
"encoding/json"
"fmt"
"log"
"net/http"
"net/http/httptest"
"net/http/httputil"
)
func RawStdout(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
resp := httptest.NewRecorder()
reqDump, err := httputil.DumpRequest(req, true)
if err != nil {
log.Printf("ERR: dumping request body for logs: %s", err)
}
next(resp, req)
var respHeaders bytes.Buffer
resp.Header().WriteSubset(&respHeaders, nil)
log.Printf(`
-------------------
~ REQUEST:
%s ~ RESPONSE:
Status: %d
Headers: %s
%s
-------------------
`, string(reqDump), resp.Code, respHeaders.String(), resp.Body)
forwardResponse(resp, w)
}
}
func JSONStdout(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
t, err := generateTransaction(next, w, req)
if err != nil {
log.Printf("ERR: %s", err)
return
}
out, err := json.Marshal(t)
if err != nil {
out = []byte(fmt.Sprintf("%+v", t))
}
log.Println(string(out))
}
}