Coder Social home page Coder Social logo

Comments (17)

brstos avatar brstos commented on May 22, 2024 2

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.

marcosnils avatar marcosnils commented on May 22, 2024 1

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.

alehano avatar alehano commented on May 22, 2024

+1

from negroni.

muei avatar muei commented on May 22, 2024

+1

from negroni.

codegangsta avatar codegangsta commented on May 22, 2024

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.

jamiefoster avatar jamiefoster commented on May 22, 2024

Thank you for the reply.
Has anyone tried this with https://github.com/julienschmidt/httprouter ?

from negroni.

codegangsta avatar codegangsta commented on May 22, 2024

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.

codegangsta avatar codegangsta commented on May 22, 2024

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.

codegangsta avatar codegangsta commented on May 22, 2024

closing as it looks like this issue is resolved

from negroni.

alehano avatar alehano commented on May 22, 2024

It would be great if this info was in readme file.

from negroni.

romanodesouza avatar romanodesouza commented on May 22, 2024

It would be great if this info was in readme file.

+1

from negroni.

marcosnils avatar marcosnils commented on May 22, 2024

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.

dimroc avatar dimroc commented on May 22, 2024

Thanks @marcosnils. Put me in the right direction.

from negroni.

marcosnils avatar marcosnils commented on May 22, 2024

@dimroc glad it helped :)

from negroni.

aubm avatar aubm commented on May 22, 2024

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.

aubm avatar aubm commented on May 22, 2024

Hey @brstos :)

Thank you for your answer, it makes sens to me.
I'll give it a try,

Cheers,

from negroni.

pawanrawal avatar pawanrawal commented on May 22, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.