How to access fly routes in go?
I have a multiplexer and 4 different routes.
a.Router = mux.NewRouter()
a.Router.HandleFunc("/1/query/{query}", a.sigQuery).Methods("GET")
a.Router.HandleFunc("/1/sis", a.rGet).Methods("GET")
a.Router.HandleFunc("/1/sigs", a.sigHandler).Methods("GET", "POST", "DELETE")
a.Router.HandleFunc("/1/nfeeds", a.nfeedGet).Methods("GET", "DELETE", "POST")
Is there a method where we can list specific routes and get methods defined on them. I tried like this: it routes := a.getRoutes()
will return me a piece with all the routes, and it methods := routes[1].Methods()
will return the methods listed on this route. Is there a way we can achieve this?
+3
user5906464
source
to share
1 answer
Use the method Walk
:
router.Walk(func(route *mux.Route, router *mux.Router, ancestors []*mux.Route) error {
tpl, err1 := route.GetPathTemplate()
met, err2 := route.GetMethods()
fmt.Println(tpl, err1, met, err2)
return nil
})
Alternatively, you can just put all your routes in a structure chunk and just do
for _, r := range routes {
router.HandleFunc(r.tpl, r.func).Methods(r.methods...)
}
at the initialization stage.
+2
source to share