-
I have a project with a large number of routes. Many of them have URL params. Some of them are grouped into subrouters. When I have an issue at a particular URL, sometimes it is difficult to determine what handler is responsible. For example, given the README.md example routes: r.Route("/articles", func(r chi.Router) {
r.With(paginate).Get("/", listArticles) // GET /articles
r.With(paginate).Get("/{month}-{day}-{year}", listArticlesByDate) // GET /articles/01-16-2017
r.Post("/", createArticle) // POST /articles
r.Get("/search", searchArticles) // GET /articles/search
// Regexp url parameters:
r.Get("/{articleSlug:[a-z-]+}", getArticleBySlug) // GET /articles/home-is-toronto
// Subrouters:
r.Route("/{articleID}", func(r chi.Router) {
r.Use(ArticleCtx)
r.Get("/", getArticle) // GET /articles/123
r.Put("/", updateArticle) // PUT /articles/123
r.Delete("/", deleteArticle) // DELETE /articles/123
})
}) How would I easily find that I'm somewhat spoiled by the Ruby on Rails logs where it logs the exact method that handled each request. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
@jackc chi.RouteContext(r.Context()).RoutePatterns Hope it helps :) |
Beta Was this translation helpful? Give feedback.
-
@jackc handlerName := runtime.FuncForPC(reflect.ValueOf(your_handler).Pointer()).Name() However, we can't directly access the original handler function since it's already wrapped by Chi's routing system. |
Beta Was this translation helpful? Give feedback.
@jackc
The only solution I could prototype to get the name of the handler is with this:
However, we can't directly access the original handler function since it's already wrapped by Chi's routing system.