Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

could i get a router param in middleware #74

Closed
williambao opened this issue Mar 8, 2015 · 4 comments
Closed

could i get a router param in middleware #74

williambao opened this issue Mar 8, 2015 · 4 comments

Comments

@williambao
Copy link

Hi,
i want to check some auth in middleware, should check url param is valid. but i use mux.Vars["dbId"], it return empty. like below code,

// my app url
router1 := mux.NewRouter()
router1.HandleFunc("/hrm/{dbId}/users", handler)
mainRouter.PathPrefix("/hrm").Handler(negroni.New(
    middleware2,
    negroni.Wrap(router1),
))

// in middleare
func (m *middleware2) ServeHTTP(w http.ResponseWriter, req *http.Request, next http.HandlerFunc) {
    vars := mux.Vars(req)

   // the dbId is empty, how to i get router param there?
    dbId := vars["dbId"]
}

how can i get router param in middleware?
thanks.

@codegangsta
Copy link
Contributor

At that point, the router has not even matched the route. You will have to have the middleware execute after the route has been matched but before your handler executes

@codegangsta
Copy link
Contributor

Closing, Hope this answered your question

@fiatjaf
Copy link

fiatjaf commented Jun 30, 2015

You will have to have the middleware execute after the route has been matched but before your handler executes

How do you do that?

@jszwedko
Copy link
Contributor

jszwedko commented Jul 1, 2015

@fiatjaf something like this:

// my app url
router1 := mux.NewRouter()
router1.HandleFunc("/hrm/{dbId}/users", negroni.New(
    middleware2,
    negroni.Wrap(router1),
))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants