tommyd456
tommyd456

Reputation: 10693

Negroni Route Specific Middleware

I'm struggling to get my route specific middleware working with httprouter and Negroni. The login route requires Middleware2 and all the other routes require Middleware1.

So far I have:

func Main() {
    router := httprouter.New()
    protectedRoutes := httprouter.New()

    DefineRoutes(router)
    DefineProtectedRoutes(protectedRoutes)

    //This is the block that I'm unsure about
    //How can I introduce Middleware2 for a specific route?
    n := negroni.New()
    n.Use(negroni.HandlerFunc(Middleware1))
    n.UseHandler(router)


    n.Run(":5000")
}

func DefineRoutes(router *httprouter.Router) {
    router.POST("/v1/users/authenticate", UserLogin)
    router.POST("/v1/users/authorize", UserLogin)
    router.POST("/v1/users/logout", UserLogout)
}

func DefineProtectedRoutes(router *httprouter.Router) {
    router.POST("/v1/users/login", UserLogin)
}

but now I'm a bit stuck as the examples on the site (https://github.com/codegangsta/negroni) use the standard handlers.

Upvotes: 2

Views: 1097

Answers (1)

Unni
Unni

Reputation: 1

Not sure if anyone is still looking for an answer. I think params can be retrieved by calling the router.Lookup function. Here is what I did:

_, params, _ :=  router.Lookup("GET", req.URL.Path)

where router is an instance of httprouter.Router

Upvotes: 0

Related Questions