I am using gorilla mux for manage routing. What I am missing is to integrate a middleware between every request.
For example
package main
import (
func Middleware(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// do stuff
h.ServeHTTP(w, r)
})
}
func Middleware2(s string) mux.MiddlewareFunc {
return func(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// do stuff
fmt.Println(s)
h.ServeHTTP(w, r)
})
}
}
func main() {
router := mux.NewRouter()
router.Use(Middleware)
//you can apply it to a sub-router too
subRouter := router.PathPrefix("/sub_router/").Subrouter()
subRouter.Use(Middleware2("somePrams"))
// Add more middleware if you need call router.Use Again
// router.Use(Middleware3, Middleware4, Middleware5)
_ = http.ListenAndServe(":80", router)
}
the official doc on the mux website