Giter Site home page Giter Site logo

Subrouter hides parent routes about chi HOT 3 CLOSED

go-chi avatar go-chi commented on May 6, 2024
Subrouter hides parent routes

from chi.

Comments (3)

pkieltyka avatar pkieltyka commented on May 6, 2024

What should it do?

Cuz the r.Mount() route is the same as the get request, and overwrites it. You should define the / route in the subrouter if it's using the same name, since that is its domain

On Jan 25, 2016, at 9:11 PM, Vojtech Vitek [email protected] wrote:

package main

import (
"log"
"net/http"

"github.com/pressly/chi"

)

func main() {
r := chi.NewRouter()

// Parent route -- /foo
r.Get("/foo", func(w http.ResponseWriter, r *http.Request) {
    log.Println("/foo")
})

// Subrouter -- /foo/bar
r.Mount("/foo", subrouter())

http.ListenAndServe(":3333", r)

}

func subrouter() http.Handler {
r := chi.NewRouter()
r.Get("/bar", func(w http.ResponseWriter, r *http.Request) {
log.Println("/foo/bar")
})
return r
}
$ curl -v localhost:3333/foo

GET /foo HTTP/1.1
< HTTP/1.1 404 Not Found
Not Found
** Not found? I defined this route -- and none path should rewrite it. **

$ curl -v localhost:3333/foo/

GET /foo/ HTTP/1.1
< HTTP/1.1 404 Not Found
404 page not found
* btw: Why's the body different compared to the previous request? Different NotFound handler?*

$ curl -v localhost:3333/foo/bar

GET /foo/bar HTTP/1.1
< HTTP/1.1 200 OK
/foo/bar
** OK **


Reply to this email directly or view it on GitHub.

from chi.

VojtechVitek avatar VojtechVitek commented on May 6, 2024

Since I'm not overwriting the parent /foo path in the subrouter, I'd expect it to be matched and served anyways. Consider applying different middlewares for anonymous and auth'd routes from the parent router:

func main() {
    r := chi.NewRouter()

    r.Group(func(r chi.Router) {
        r.Use(InjectAnonymousToken)

        r.Get("/", PublicRoutes())
    })

    r.Group(func(r chi.Router) {
        r.Use(TokenRefresh)
        r.Use(Authenticator)

        r.Mount("/", AuthRoutes())
    })

    http.ListenAndServe(":3333", r)
}

It would be nice to be able to "merge" subrouters having a common root path.

from chi.

VojtechVitek avatar VojtechVitek commented on May 6, 2024

Another example:

  1. THIS WORKS (subrouter first, GetHandler route merges into the routes):
func main() {
    r := chi.NewRouter()

    // Auth'd routes
    r.Group(func(r chi.Router) {
        r.Use(TokenRefresh)
        r.Use(Authenticator)

        r.Mount("/", AuthRoutes())
    })

    // Public routes
    r.Group(func(r chi.Router) {
        r.Use(InjectAnonymousToken)

        r.Get("/", GetHandler)
    })

    http.ListenAndServe(":3333", r)
}
  1. THIS DOESN'T WORK (subrouter second, it overrides the GetHandler route w/o merge):
func main() {
    r := chi.NewRouter()

    // Public routes
    r.Group(func(r chi.Router) {
        r.Use(InjectAnonymousToken)

        r.Get("/", GetHandler)
    })

    // Auth'd routes
    r.Group(func(r chi.Router) {
        r.Use(TokenRefresh)
        r.Use(Authenticator)

        r.Mount("/", AuthRoutes())
    })

    http.ListenAndServe(":3333", r)
}

from chi.

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.