Skip to content
Permalink
Browse files

improved task middleware to show skipped path (#1380)

* improved task middleware to show skipped path

* use tab instead of spaces for indenting middlewares

* fixed duplicated and invalid output for two or more skipping
  • Loading branch information...
sio4 authored and markbates committed Oct 18, 2018
1 parent beb041c commit 1cecfc0679ebe3f19beadf1c50eaa482aebd59c6
Showing with 62 additions and 5 deletions.
  1. +59 −4 grifts.go
  2. +3 −1 middleware.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) {
@@ -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 {

0 comments on commit 1cecfc0

Please sign in to comment.
You can’t perform that action at this time.