-
Notifications
You must be signed in to change notification settings - Fork 1
/
access.go
50 lines (40 loc) · 980 Bytes
/
access.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
package main
import (
"bufio"
"net"
"net/http"
log "gopkg.in/inconshreveable/log15.v2"
)
type responseWriterWrapper struct {
rw http.ResponseWriter
status int
}
func (r *responseWriterWrapper) Write(p []byte) (int, error) {
return r.rw.Write(p)
}
func (r *responseWriterWrapper) Header() http.Header {
return r.rw.Header()
}
func (r *responseWriterWrapper) WriteHeader(status int) {
r.status = status
r.rw.WriteHeader(status)
}
func (r *responseWriterWrapper) Hijack() (net.Conn, *bufio.ReadWriter, error) {
h := r.rw.(http.Hijacker)
return h.Hijack()
}
func accessLog(handler http.Handler) http.Handler {
return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
log.Info("received request",
"method", req.Method,
"path", req.URL.Path,
)
rww := responseWriterWrapper{rw, 200}
handler.ServeHTTP(&rww, req)
log.Info("processed request",
"method", req.Method,
"path", req.URL.Path,
"status", rww.status,
)
})
}