middleware with status codes
This commit is contained in:
parent
025596162c
commit
c98fc938c9
@ -3,13 +3,29 @@ package middleware
|
|||||||
import (
|
import (
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func LoggingMiddleware(next http.Handler) http.Handler {
|
type wrappedWriter struct {
|
||||||
return http.HandlerFunc(
|
http.ResponseWriter
|
||||||
func(w http.ResponseWriter, r *http.Request) {
|
statusCode int
|
||||||
log.Printf("[%s] %s", r.Method, r.URL.EscapedPath())
|
}
|
||||||
next.ServeHTTP(w, r)
|
|
||||||
},
|
func (w * wrappedWriter) WriteHeader(statusCode int) {
|
||||||
)
|
w.ResponseWriter.WriteHeader(statusCode)
|
||||||
|
w.statusCode = statusCode
|
||||||
|
}
|
||||||
|
|
||||||
|
func LoggingMiddleware(next http.Handler) http.Handler {
|
||||||
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
start := time.Now()
|
||||||
|
|
||||||
|
wrapped := &wrappedWriter{
|
||||||
|
ResponseWriter: w,
|
||||||
|
statusCode: http.StatusOK,
|
||||||
|
}
|
||||||
|
|
||||||
|
next.ServeHTTP(wrapped, r)
|
||||||
|
log.Println(wrapped.statusCode, r.Method, r.URL.Path, time.Since(start))
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user