Skip to content
This repository has been archived by the owner on Feb 24, 2024. It is now read-only.

improved task middleware to show skipped path #1380

Merged
merged 4 commits into from
Oct 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 59 additions & 4 deletions grifts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 3 additions & 1 deletion middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down