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 :)
When setting up drone.ci I noticed that they use a gin/debug.go feature to print all the registered routes, namely
debugPrintRoutemethod. It would be nice to have an utility function that would produce somewhat standard output;Here's my stab at that function:
The function, like gin, uses reflection to print the name of the individual handlers.
And an example output:
What's missing:
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 :)