Reputation: 32264
How can I apply middleware to a Go Gorilla Toolkit mux subrouter?
I have the following code:
router := mux.NewRouter().StrictSlash(true)
apiRouter := router.PathPrefix("/api/").Subrouter()
apiRouter.Methods(http.MethodGet).
Path("/api/path/to/handler").Handler(handleAPICall)
I want to apply a middleware handler that checks a security token, but only on those paths that start with /api
.
Upvotes: 2
Views: 7344
Reputation: 31
use these code ,should work fine 😄
r := mux.NewRouter()
api := r.PathPrefix("/api").Subrouter()
routes.InitRouteV1(api)
// handle 404
r.NotFoundHandler = http.HandlerFunc(func (w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-type", "application/json")
json.NewEncoder(w).Encode(&response.Rsp{Code: http.StatusNotFound,Message: http.StatusText(http.StatusNotFound) })
})
// handle method
r.MethodNotAllowedHandler = http.HandlerFunc(func (w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-type", "application/json")
json.NewEncoder(w).Encode(&response.Rsp{Code: http.StatusMethodNotAllowed,Message: http.StatusText(http.StatusMethodNotAllowed) })
})
package routes
func InitRouteV1(r *mux.Router){
r.Use(middleware.ResponseJsonMiddleware)
r.HandleFunc("/user", controller.User).Methods("GET")
}
Upvotes: 1
Reputation: 816
You can use this:
router := mux.NewRouter().StrictSlash(true)
apiRoutes := router.PathPrefix("/api").Subrouter()
apiRoutes.Use(authMiddleware)
apiRoutes.HandleFunc("/postes", getPostes).Methods("GET")
apiRoutes.HandleFunc("/postes/new", newPost).Methods("POST")
this is my authMidelware:
func authMiddleware(next http.Handler) http.Handler {
if len(APP_KEY) == 0 {
log.Fatal("HTTP server unable to start, expected an APP_KEY for JWT auth")
}
jwtMiddleware := jwtmiddleware.New(jwtmiddleware.Options{
ValidationKeyGetter: func(token *jwt.Token) (interface{}, error) {
return []byte(APP_KEY), nil
},
SigningMethod: jwt.SigningMethodHS256,
})
return jwtMiddleware.Handler(next)
}
Upvotes: 0
Reputation: 32264
The following seems to work:
apiRouter := mux.NewRouter()
router.PathPrefix("/api/").Handler(http.StripPrefix("/api",
adapt(apiRouter, checkTokenHandler)))
apiRouter.Methods(http.MethodGet).
Path("/path/to/handler").Handler(handleAPICall)
// Note that `/api` has been removed from the path.
where
func adapt(h http.Handler, adapters ...func(http.Handler) http.Handler)
http.Handler {
for _, adapter := range adapters {
h = adapter(h)
}
return h
}
and
func checkTokenHandler(h http.Handler) http.Handler {
return http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
// Check the security cookie.
h.ServeHTTP(res, req)
})
}
Upvotes: 2