use middlewares of parents
This adds all middlewares from all parents. This should make it easier to group endpoints to middlewares.
This commit is contained in:
parent
4c6d6f3976
commit
5fdfdbf5e1
|
@ -50,6 +50,13 @@ func (r *Router) fullpath(path string) string {
|
|||
return r.path + path
|
||||
}
|
||||
|
||||
func (r *Router) fullFuncList() []ContextFunc {
|
||||
if r.parent != nil {
|
||||
return append(r.parent.fullFuncList(), r.funcList...)
|
||||
}
|
||||
return r.funcList
|
||||
}
|
||||
|
||||
func (r *Router) Get(path string, target ContextFunc) {
|
||||
r.addRoute("GET", path, target)
|
||||
}
|
||||
|
@ -81,7 +88,7 @@ func (r *Router) createHandleFunction(target ContextFunc) httprouter.Handle {
|
|||
params httprouter.Params) {
|
||||
|
||||
ctx := &Context{
|
||||
request, response, params, append(r.funcList, target), 0,
|
||||
request, response, params, append(r.fullFuncList(), target), 0,
|
||||
}
|
||||
for i := 0; i < len(ctx.funcList); i++ {
|
||||
ctx.funcList[i](ctx)
|
||||
|
|
9
main.go
9
main.go
|
@ -9,15 +9,6 @@ import (
|
|||
func main() {
|
||||
router := lib.NewRouter()
|
||||
authentication := router.NewGroup("/admin")
|
||||
authentication.Use(func(c *lib.Context) error {
|
||||
c.Response.Header().Add("Content-Type", "text/html")
|
||||
fmt.Fprint(c.Response, "Hello says the middleware!<br />")
|
||||
return nil
|
||||
})
|
||||
authentication.Get("/", func(c *lib.Context) error {
|
||||
fmt.Fprint(c.Response, "Admin panel, yay!")
|
||||
return nil
|
||||
})
|
||||
controller.DefineRoutes(router)
|
||||
router.Start()
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue