MCPcopy Create free account
hub / github.com/goadesign/goa / Debug

Function Debug

http/middleware/debug.go:29–106  ·  view source on GitHub ↗

Debug returns a debug middleware which prints detailed information about incoming requests and outgoing responses including all headers, parameters and bodies.

(mux goahttp.Muxer, w io.Writer)

Source from the content-addressed store, hash-verified

27// incoming requests and outgoing responses including all headers, parameters
28// and bodies.
29func Debug(mux goahttp.Muxer, w io.Writer) func(http.Handler) http.Handler {
30 return func(h http.Handler) http.Handler {
31 return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
32 buf := &bytes.Buffer{}
33 // Request ID
34 reqID := r.Context().Value(middleware.RequestIDKey)
35 if reqID == nil {
36 reqID = shortID()
37 }
38
39 // Request URL
40 fmt.Fprintf(buf, "> [%s] %s %s", reqID, r.Method, r.URL.String())
41
42 // Request Headers
43 keys := make([]string, len(r.Header))
44 i := 0
45 for k := range r.Header {
46 keys[i] = k
47 i++
48 }
49 sort.Strings(keys)
50 for _, k := range keys {
51 fmt.Fprintf(buf, "\n> [%s] %s: %s", reqID, k, strings.Join(r.Header[k], ", "))
52 }
53
54 // Request parameters
55 params := mux.Vars(r)
56 keys = make([]string, len(params))
57 i = 0
58 for k := range params {
59 keys[i] = k
60 i++
61 }
62 sort.Strings(keys)
63 for _, k := range keys {
64 fmt.Fprintf(buf, "\n> [%s] %s: %s", reqID, k, params[k])
65 }
66
67 // Request body
68 b, err := io.ReadAll(r.Body)
69 if err != nil {
70 b = []byte("failed to read body: " + err.Error())
71 }
72 if len(b) > 0 {
73 buf.WriteByte('\n')
74 lines := strings.Split(string(b), "\n")
75 for _, line := range lines {
76 fmt.Fprintf(buf, "[%s] %s\n", reqID, line)
77 }
78 }
79 r.Body = io.NopCloser(bytes.NewBuffer(b))
80
81 dupper := &responseDupper{ResponseWriter: rw, Buffer: &bytes.Buffer{}}
82 h.ServeHTTP(dupper, r)
83
84 fmt.Fprintf(buf, "\n< [%s] %s", reqID, http.StatusText(dupper.Status))
85 keys = make([]string, len(dupper.Header()))
86 i = 0

Callers

nothing calls this directly

Calls 8

ContextMethod · 0.80
shortIDFunction · 0.70
StringMethod · 0.65
VarsMethod · 0.65
ServeHTTPMethod · 0.65
ErrorMethod · 0.45
HeaderMethod · 0.45
WriteMethod · 0.45

Tested by

no test coverage detected