3
0

upd(sam): print route debug in main

This commit is contained in:
Tit Petric
2018-07-19 20:21:36 +02:00
parent 73c27f6bdd
commit 5ad2b6af51

View File

@@ -1,13 +1,17 @@
package main
import (
"fmt"
"reflect"
"runtime"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
"github.com/go-chi/cors"
)
// MountRoutes will register API routes
func MountRoutes(r chi.Router, opts *RouteOptions, mountRoutes func(r chi.Router)) {
func MountRoutes(r chi.Router, opts *RouteOptions, mountRoutes ...func(r chi.Router)) {
// CORS for local development...
cors := cors.New(cors.Options{
AllowedOrigins: []string{"*"},
@@ -22,5 +26,23 @@ func MountRoutes(r chi.Router, opts *RouteOptions, mountRoutes func(r chi.Router
r.Use(middleware.Logger)
}
mountRoutes(r)
for _, mount := range mountRoutes {
mount(r)
}
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, "", "")
}