Print all routes in Chi router

Author

Gurleen Sethi on 16 December 2022

Print all routes in Chi router

Getting Started #

If you have been writing Go you probably have heard of the popular http router chi. In this article you are going to learn how to print all the routes registered in a router, this is very helpful for debugging purposes.

If you want to learn about chi, please read Restful routing with Chi.

Setting up a router #

I assume you have a project with chi setup (if not please install chi).

Let's assume an application that has a router with 3 routes registered.

package main

import (
	"fmt"
	"github.com/go-chi/chi/v5"
	"net/http"
)

func main() {
	router := chi.NewRouter()

	router.Get("/articles", sayHello)
	router.Get("/articles/{articleID}", sayHello)

	router.With(func(next http.Handler) http.Handler {
		return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			next.ServeHTTP(w, r)
		})
	}).Post("/articles", sayHello) // πŸ‘ˆ route with middleware
}

func sayHello(w http.ResponseWriter, r *http.Request) {
	w.Write([]byte("Hello World"))
}
main.go

Look closely one of these routes (POST route) has a middleware setup as well. So now we want to print out all the routes and the number of middlewares setup on each routes.

Print routes with chi.Walk #

We want to print each route in the format: [<method>]: '<route>' has <x> middlewares.

Chi provides a built in function called chi.Walk that traverses the route tree and calls a calback function for each route.

It has a long function signature so scroll the code below to the right to see it all.

chi.Walk(router, func(method string, route string, handler http.Handler, middlewares ...func(http.Handler) http.Handler) error {
	fmt.Printf("[%s]: '%s' has %d middlewares\n", method, route, len(middlewares))
	return nil
})

chi will call the passed function for each route and our fmt statement inside it will print the required information.

Putting it together the entire example looks like this.

package main

import (
	"fmt"
	"github.com/go-chi/chi/v5"
	"net/http"
)

func main() {
	router := chi.NewRouter()

	router.Get("/articles", sayHello)
	router.Get("/articles/{articleID}", sayHello)

	router.With(func(next http.Handler) http.Handler {
		return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			next.ServeHTTP(w, r)
		})
	}).Post("/articles", sayHello)

	// πŸ‘‡ the walking function πŸšΆβ€β™‚οΈ
	chi.Walk(router, func(method string, route string, handler http.Handler, middlewares ...func(http.Handler) http.Handler) error {
		fmt.Printf("[%s]: '%s' has %d middlewares\n", method, route, len(middlewares))
		return nil
	})
}

func sayHello(w http.ResponseWriter, r *http.Request) {
	w.Write([]byte("Hello World"))
}
main.go

And will print the following output.

[GET]: '/articles' has 0 middlewares
[POST]: '/articles' has 1 middlewares
[GET]: '/articles/{articleID}' has 0 middlewares

Thank you for reading this article πŸ™πŸ» hope it was helpful.

Table of Contents
Subscribe via email

Get notified once/twice per month when new articles are published.

Byte Byte Go Affiliate
TheDeveloperCafe
Copyright Β© 2022 - 2024 TheDeveloperCafe.
The Go gopher was designed by Renee French.