diff --git a/grifts.go b/grifts.go index d1eb6d5b5..2b635f9d7 100644 --- a/grifts.go +++ b/grifts.go @@ -49,11 +49,66 @@ func middlewareGrift(a *App) { } func printMiddleware(a *App) { - fmt.Printf("-> %s\n", a.Name) - fmt.Printf("%v\n", a.Middleware.String()) - for _, x := range a.children { - printMiddleware(x) + printMiddlewareByRoute(a) +} + +func printMiddlewareByRoute(a *App) { + mws := map[string]string{} + for _, r := range a.Routes() { + if mws[r.App.Name] == "" { + pname := "" + if parent := getParentApp(r.App.root, r.App.Name); parent != nil { + pname = parent.Name + } + + mws[r.App.Name] = r.App.Middleware.String() + if mws[pname] != mws[r.App.Name] { + fmt.Printf("-> %s\n", r.App.Name) + printMiddlewareStackWithIndent(mws[r.App.Name]) + } else { + fmt.Printf("-> %s (see: %v)\n", r.App.Name, pname) + } + } + s := "\n" + mws[r.App.Name] + for k := range r.App.Middleware.skips { + mw := strings.Split(k, funcKeyDelimeter)[0] + h := strings.Split(k, funcKeyDelimeter)[1] + if h == r.HandlerName { + s = strings.Replace(s, "\n"+mw, "", 1) + } + } + if "\n"+mws[r.App.Name] != s { + ahn := strings.Split(r.HandlerName, "/") + hn := ahn[len(ahn)-1] + fmt.Printf("-> %s %s (by %s)\n", r.Method, r.Path, hn) + printMiddlewareStackWithIndent(s) + } + } +} + +func getParentApp(r *App, name string) *App { + if r == nil { + return nil + } + for _, x := range r.children { + if x.Name == name { + return r + } + if len(x.children) > 0 { + if ret := getParentApp(x, name); ret != nil { + return ret + } + } + } + return nil +} + +func printMiddlewareStackWithIndent(s string) { + if s == "" { + s = "[none]" } + s = strings.Replace(s, "\n", "\n\t", -1) + fmt.Printf("\t%v\n", strings.TrimSpace(s)) } func routesGrift(a *App) { diff --git a/middleware.go b/middleware.go index 60bde531f..a93e7719e 100644 --- a/middleware.go +++ b/middleware.go @@ -21,6 +21,8 @@ import ( */ type MiddlewareFunc func(Handler) Handler +const funcKeyDelimeter = ":" + // Use the specified Middleware for the App. // When defined on an `*App` the specified middleware will be // inherited by any `Group` calls that are made on that on @@ -153,7 +155,7 @@ func funcKey(funcs ...interface{}) string { keyMapMutex.Unlock() names = append(names, n) } - return strings.Join(names, "/") + return strings.Join(names, funcKeyDelimeter) } func ptrName(ptr uintptr) string {