Comments (17)
Hey @aubm
When you access your endpoints it is handled by router
that redirects to booksRouter
or librariesRouter
. The problem is that when it's redirected there is no mappings for the called endpoint. The routers for books and libraries are not subrouters for router
. So you must specify the complete mapping in order to be able to handle the requests. Use it instead...
router := mux.NewRouter()
booksRouter := mux.NewRouter()
booksRouter.HandleFunc("/api/libraries/{libraryId}/books", getBooks).Methods("GET")
booksRouter.HandleFunc("/api/libraries/{libraryId}/books", createBook).Methods("GET")
booksRouter.HandleFunc("/api/libraries/{libraryId}/books/{bookId}", updateBook).Methods("PUT")
booksRouter.HandleFunc("/api/libraries/{libraryId}/books/{bookId}", getOneBook).Methods("GET")
booksRouter.HandleFunc("/api/libraries/{libraryId}/books/{bookId}", deleteOneBook).Methods("DELETE")
router.PathPrefix("/api/libraries/{libraryId}/books").Handler(negroni.New(negroni.Wrap(booksRouter)))
librariesRouter := mux.NewRouter()
librariesRouter.HandleFunc("/api/libraries", getLibraries).Methods("GET")
librariesRouter.HandleFunc("/api/libraries", createLibrary).Methods("POST")
librariesRouter.HandleFunc("/api/libraries/{libraryId}", updateLibrary).Methods("PUT")
librariesRouter.HandleFunc("/api/libraries/{libraryId}", getOneLibrary).Methods("GET")
librariesRouter.HandleFunc("/api/libraries/{libraryId}", deleteLibrary).Methods("DELETE")
router.PathPrefix("/api/libraries").Handler(negroni.New(negroni.Wrap(librariesRouter)))
n := negroni.New()
n.UseHandler(router)
n.Run(":8080")
from negroni.
I already found how it should be done. Instead of using r.Handle
I've changed it to r.PathPrefix("/accounts").Handler(.....
That did the trick.
from negroni.
+1
from negroni.
+1
from negroni.
This is where Negroni's modularity excels. If your router supports http.Handler
or http.HandlerFunc
for your route handlers (which it should), you can just create a new instance of negroni and use that as your middleware stack for the specific route, here is an example in gorilla mux:
n := negroni.Classic()
r := mux.NewRouter()
r.Handle("/admin", negroni.New(Middleware1, Middleware2, AdminHandler))
n.UseHandler(r)
n.Run(":3000")
With AdminHandler
being your route handler.
Hope that helps!
from negroni.
Thank you for the reply.
Has anyone tried this with https://github.com/julienschmidt/httprouter ?
from negroni.
Httprouter supports http.Handler with the Handler method. So it should work great. Only limitation is that you won't be getting httprouter params for the top level route in the nesting, but that is a problem with nesting in general.
You could possibly make use of closures, but in all likeliness you wouldn't have this restriction work against you in most situations
Sent from my iPhone
On Jun 27, 2014, at 8:12 AM, Jamie Foster [email protected] wrote:
Thank you for the reply.
Has anyone tried this with https://github.com/julienschmidt/httprouter ?—
Reply to this email directly or view it on GitHub.
from negroni.
Yup, you can create as many negroni instances as you want, they are just http.Handlers and pretty lightweight.
On Jun 27, 2014, at 9:00 AM, Jamie Foster [email protected] wrote:
In case i need two routes using a middleware and a third route using another middleware, would I write something like this?
n := negroni.Classic()
r := mux.NewRouter()
r.Handle("/route1", negroni.New(Middleware1,UnrestrictedHandlerRoute1))
r.Handle("/admin", negroni.New(Middleware1, Middleware2, AdminHandler))
n.UseHandler(r)n.Run(":3000")
—
Reply to this email directly or view it on GitHub.
from negroni.
closing as it looks like this issue is resolved
from negroni.
It would be great if this info was in readme file.
from negroni.
It would be great if this info was in readme file.
+1
from negroni.
Hi, I'm trying to make this work using gorilla and negroni but I can't seem to find the correct solution. This is the way I'm trying to do this:
n := negroni.Classic()
r := mux.NewRouter()
ac := mux.NewRouter()
ac.HandleFunc("/accounts", createHandler)
ac.HandleFunc("/accounts/{id}/password_reset_tokens", sendPasswordResetTokenHandler)
r.Handle("/accounts", negroni.New(
&middleware.BasicAuth{},
negroni.Wrap(ac),
))
n.UseHandler(r)
n.Run(":3000")
After negroni starts, if I call the /accounts URI it works and calls the createHandler with the specified middlewares. However, whenever I try to call the /accounts/{id}/password_reset_tokens I get a 404 response from negorni / gorilla.
Can you please point me the correct way of applying a middleware for several routes?
Thanks!.
from negroni.
Thanks @marcosnils. Put me in the right direction.
from negroni.
@dimroc glad it helped :)
from negroni.
Hey there :)
I might need a bit of help for this. For what I understand, the following should work:
router := mux.NewRouter()
booksRouter := mux.NewRouter()
booksRouter.HandleFunc("/", getBooks).Methods("GET")
booksRouter.HandleFunc("/", createBook).Methods("GET")
booksRouter.HandleFunc("/{bookId}", updateBook).Methods("PUT")
booksRouter.HandleFunc("/{bookId}", getOneBook).Methods("GET")
booksRouter.HandleFunc("/{bookId}", deleteOneBook).Methods("DELETE")
router.PathPrefix("/api/libraries/{libraryId}/books").Handler(negroni.New(negroni.Wrap(booksRouter)))
librariesRouter := mux.NewRouter()
librariesRouter.HandleFunc("/", getLibraries).Methods("GET")
librariesRouter.HandleFunc("/", createLibrary).Methods("POST")
librariesRouter.HandleFunc("/{libraryId}", updateLibrary).Methods("PUT")
librariesRouter.HandleFunc("/{libraryId}", getOneLibrary).Methods("GET")
librariesRouter.HandleFunc("/{libraryId}", deleteLibrary).Methods("DELETE")
router.PathPrefix("/api/libraries").Handler(negroni.New(negroni.Wrap(librariesRouter)))
n := negroni.New()
n.UseHandler(router)
n.Run(":8080")
Sadly, I fail to see why any request ends up with a 404 status code. The idea behind that is to be able to execute a middleware only for /api/libraries/{libraryId}/books
routes.
Any ideas?
from negroni.
Hey @brstos :)
Thank you for your answer, it makes sens to me.
I'll give it a try,
Cheers,
from negroni.
I am trying to apply a middleware to the routes with a subrouter, as shown https://github.com/dgraph-io/gru/blob/feature/invite-candidate/gruadmin/main.go#L124. Though the jwt middleware isn't hit and the route is hit directly?
from negroni.
Related Issues (20)
- New version tag HOT 2
- PANIC: interface conversion: *negroni.responseWriter is not negroni.ResponseWriter: missing method Before
- more example on PanicHandlerFunc ? HOT 4
- Paths containing percent (%) character not being logged correctly HOT 1
- negroni not working with google appengine HOT 2
- Gocritic 3.3 warning ifElseChain
- http.CloseNotifier is deprecated HOT 1
- http.CloseNotifier has deprecated
- Multiple With() + many middleware handlers problem HOT 3
- Can't set response headers when implementing custom Recovery Formatter HOT 3
- Can't prevent panic text from being sent on the wire
- PANIC: runtime error: invalid memory address or nil pointer dereference HOT 2
- Recovery middleware not setting stack in PanicInformation HOT 2
- The master branch is not 1.0.0 HOT 1
- Negroni and new relic HOT 2
- Change Logger Output
- negroni.ResponseWriter implements http.Flusher even if underlying http.ResponseWriter doesn't HOT 2
- bug: recovery middleware has a bug
- v3 `ResponseWriter.Before()` functions can't overwrite HTTP response code
- Release the latest tag HOT 17
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from negroni.