julienschmidt / httprouter

A high performance HTTP request router that scales well

Home Page:https://pkg.go.dev/github.com/julienschmidt/httprouter

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Dump all registered routes

bentcoder opened this issue · comments

Hi,

Is there a way to dump list of all registered routes and methods?

Thanks

POST   /api/v1/users
GET    /api/v2/comments/:id
DELETE /api/v2/comments/:id
...

Consider wrapping the router, and then replacing it, but using RLock when serving.

type MyRouter struct {
	router *router.Router
	lock   sync.RWMutex
}

func (r *MyRouter) ReplaceRouter(router *router.Router) {
	r.lock.Lock()
	defer r.lock.Unlock()
	r.router = router
}

func (r *MyRouter) ServeHTTP(w http.ResponseWriter, req *http.Request) {
	r.lock.RLock()
	defer r.lock.RUnlock()
	r.router.ServeHTTP(w, req)
}

@bubbajoe I think he was asking for a way to output the list, not drop the routes.