Created
March 10, 2019 14:00
-
-
Save wemgl/bba36fc977f30020ebed54634ddb0d24 to your computer and use it in GitHub Desktop.
Example of creating a simple middleware function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// logging is middleware for wrapping any handler we want to track response | |
// times for and to see what resources are requested. | |
func logging(next http.Handler) http.Handler { | |
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | |
start := time.Now() | |
req := fmt.Sprintf("%s %s", r.Method, r.URL) | |
log.Println(req) | |
next.ServeHTTP(w, r) | |
log.Println(req, "completed in", time.Now().Sub(start)) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment