The following example behaves not as I would expect from the the description for r.Group, specifically the "fresh" attributes:
// Group adds a new inline-Router along the current routing
// path, with a fresh middleware stack for the inline-Router.
Group(fn func(r Router)) Router
Is that a bug or am I just not fully understanding the idea?
$ curl http://localhost:3000/m1/m3
m1
m2
m3
/m1/m3
package main
import (
"fmt"
"net/http"
"github.com/go-chi/chi"
)
func main() {
r := chi.NewRouter()
r.Route("/m1", func(r chi.Router) {
r.Use(m1)
r.Group(func(r chi.Router) {
r.Use(m2)
r.Get("/m2", func(w http.ResponseWriter, r *http.Request) {
fmt.Println("/m1/m2")
})
r.Group(func(r chi.Router) {
r.Use(m3)
r.Get("/m3", func(w http.ResponseWriter, r *http.Request) {
fmt.Println("/m1/m3")
})
})
})
})
http.ListenAndServe(":3000", r)
}
func m1(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Println("m1")
next.ServeHTTP(w, r)
})
}
func m2(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Println("m2")
next.ServeHTTP(w, r)
})
}
func m3(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Println("m3")
next.ServeHTTP(w, r)
})
}
Hey @djui - I checked your example and everything works as expected by design. I agree the description isn't great, but its the best ive come up with, this is correct though.
@pkieltyka Would you say then that the trailing senentrnce part adds anything or could be removed?
// Group adds a new inline-Router along the current routing
// path, with a fresh middleware stack for the inline-Router.
Group(fn func(r Router)) Router
vs
// Group adds a new inline-Router along the current routing
// path.
Group(fn func(r Router)) Router
Otherwise I wonder: what is “fresh” about it? I guess the symbolic impression of “this one is not inheriting anything, but usually it does.”
Btw: is there a style of writing that would actually drop all inherited middlewares?
Most helpful comment
@pkieltyka Would you say then that the trailing senentrnce part adds anything or could be removed?
vs
Otherwise I wonder: what is “fresh” about it? I guess the symbolic impression of “this one is not inheriting anything, but usually it does.”
Btw: is there a style of writing that would actually drop all inherited middlewares?