gosimplenpm/middlewares/logRequest.go

34 lines
679 B
Go

package middlewares
import (
"fmt"
"log"
"net/http"
"net/http/httputil"
)
func LogMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Printf("%s - %s - %s", r.Method, r.URL, r.Host)
hasBody := false
if r.Method == "PUT" {
hasBody = true
}
requestDump, err := httputil.DumpRequest(r, hasBody)
if err != nil {
fmt.Println(err)
}
fmt.Println("RequestDump: ", string(requestDump))
// fmt.Println("Printing headers")
// for name, values := range r.Header {
// for _, value := range values {
// fmt.Printf("%s:%s\n", name, value)
// }
// }
next.ServeHTTP(w, r)
})
}