Skip to content

Instantly share code, notes, and snippets.

@wemgl
Created March 10, 2019 14:00
Show Gist options
  • Save wemgl/bba36fc977f30020ebed54634ddb0d24 to your computer and use it in GitHub Desktop.
Save wemgl/bba36fc977f30020ebed54634ddb0d24 to your computer and use it in GitHub Desktop.
Example of creating a simple middleware function
// 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