-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmiddlewares.go
More file actions
42 lines (37 loc) · 968 Bytes
/
middlewares.go
File metadata and controls
42 lines (37 loc) · 968 Bytes
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
package processing
import (
"net/http"
"net/http/httputil"
"os"
"path/filepath"
"strings"
"time"
)
func RequestToDiskMw(outPath string, checkDebugEnabledFn func() bool) func(next http.Handler) http.Handler {
noopMw := func(next http.Handler) http.Handler {
return next
}
if _, err := os.Stat(outPath); err != nil {
return noopMw
}
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if !checkDebugEnabledFn() {
next.ServeHTTP(w, r)
return
}
fullPath := filepath.Join(outPath, r.Host+strings.ReplaceAll(r.RequestURI, "/", "-")+"-"+time.Now().UTC().Format(time.RFC3339)+".req")
ff, err := os.OpenFile(fullPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
next.ServeHTTP(w, r)
return
}
defer r.Body.Close()
raw, err := httputil.DumpRequest(r, true)
if err == nil {
_, _ = ff.Write(raw)
}
next.ServeHTTP(w, r)
})
}
}