Skip to content

Feature request: add the possibility to print registered routes for debug purposes #332

Description

@titpetric

When setting up drone.ci I noticed that they use a gin/debug.go feature to print all the registered routes, namely debugPrintRoute method. It would be nice to have an utility function that would produce somewhat standard output;

Here's my stab at that function:

var printRoutes func(chi.Routes, string, string)
printRoutes = func(r chi.Routes, indent string, prefix string) {
        routes := r.Routes()
        for _, route := range routes {
                if route.SubRoutes != nil && len(route.SubRoutes.Routes()) > 0 {
                        fmt.Printf(indent+"%s - with %d handlers, %d subroutes\n", route.Pattern, len(route.Handlers), len(route.SubRoutes.Routes()))
                        printRoutes(route.SubRoutes, indent+"\t", prefix+route.Pattern[:len(route.Pattern)-2])
                } else {
                        for key, fn := range route.Handlers {
                                fmt.Printf("%s%s\t%s -> %s\n", indent, key, prefix+route.Pattern, runtime.FuncForPC(reflect.ValueOf(fn).Pointer()).Name())
                        }
                }
        }
}
printRoutes(r, "", "")

The function, like gin, uses reflection to print the name of the individual handlers.

And an example output:

/module/* - with 10 handlers, 5 subroutes
        DELETE  /module/content/delete -> crm.(*ModuleHandlers).ContentDelete-fm
        POST    /module/content/edit -> crm.(*ModuleHandlers).ContentEdit-fm
        GET     /module/content/list -> crm.(*ModuleHandlers).ContentList-fm
        POST    /module/edit -> crm.(*ModuleHandlers).Edit-fm
        GET     /module/list -> crm.(*ModuleHandlers).List-fm
/types/* - with 10 handlers, 2 subroutes
        GET     /types/list -> crm.(*TypesHandlers).List-fm
        GET     /types/type/{id} -> crm.(*TypesHandlers).Type-fm

What's missing:

  • info about middleware (not sure it should even be included)
  • a better way to output this stuff (io.Writer, colors, standard structured logger?)

I'd love to improve it, and submit a PR for something like chi.Debug(chi.Router). Comments for improvements are welcome, and I hope I didn't have a blind spot somewhere and missed that something like this already exists in chi :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions