I'm trying to add a middleware only on some routes. I wrote this code:
func main() {
router := mux.NewRouter().StrictSlash(false)
admin_subrouter := router.PathPrefix("/admin").Subrouter()
//handlers.CombinedLoggingHandler comes from gorilla/handlers
router.PathPrefix("/admin").Handler(negroni.New(
negroni.Wrap(handlers.CombinedLoggingHandler(os.Stdout, admin_subrouter)),
))
admin_subrouter.HandleFunc("/articles/new", articles_new).Methods("GET")
admin_subrouter.HandleFunc("/articles", articles_index).Methods("GET")
admin_subrouter.HandleFunc("/articles", articles_create).Methods("POST")
n := negroni.New()
n.UseHandler(router)
http.ListenAndServe(":3000", n)
}
I expect to see request logs only for paths with prefix /admin. I do see a log line when I do "GET /admin", but not when I do "GET /admin/articles/new". I tried by brute force other combinations but I can't get it. What's wrong with my code?
I saw other ways, like wrapping the HandlerFunc on each route definition, but I wanted to do it once for a prefix or a subrouter.
The logging middleware I'm using there is for testing, maybe an Auth middleware makes more sense, but I just wanted to make it work.
Thanks!