Modern applications with web UI or REST API usually use the middleware mechanism to log the activity, or guard the security of the given interface. In this recipe, the implementation of such a middleware layer will be presented.
Creating HTTP middleware layer
How to do it...
- Open the console and create the folder chapter09/recipe06.
- Navigate to the directory.
- Create the middleware.go file with the following content:
package main
import (
"io"
"net/http"
)
func main() {
// Secured API
mux := http.NewServeMux()
mux.HandleFunc("/api/users", Secure(func(w http.ResponseWriter,
r *http.Request) {
...