Router
NewRouter, the method helpers, groups, middleware, and the Route table.
Router is the chi-backed request router. Handlers register against it and read path data through the params helpers, so chi never leaks into handler code.
NewRouter
func NewRouter() *RouterBuilds an empty router. Register handlers on it, then wrap it in a Server to serve.
Method helpers
Each helper registers a handler for one HTTP method and a chi pattern. Handle takes an explicit method for verbs the helpers do not cover.
func (r *Router) Get(p string, h http.HandlerFunc)
func (r *Router) Post(p string, h http.HandlerFunc)
func (r *Router) Put(p string, h http.HandlerFunc)
func (r *Router) Delete(p string, h http.HandlerFunc)
func (r *Router) Patch(p string, h http.HandlerFunc)
func (r *Router) Handle(method string, pattern string, h http.Handler)Groups and middleware
func (r *Router) Group(prefix string, fn func(*Router))Mounts a sub-router at prefix. Middleware added inside fn applies only to routes registered on the sub-router.
func (r *Router) Use(mw ...func(http.Handler) http.Handler)Appends middleware to this router's scope. chi panics if called after a route is registered on the same scope, so add middleware first.
func (r *Router) With(mw ...func(http.Handler) http.Handler) *RouterReturns a sub-router carrying extra inline middleware, without mutating the parent.
LogRoutes
func (r *Router) LogRoutes(logger Logger)Walks every registered route and logs its method and pattern. Server.Start calls it for you on startup.
Route table
When routes are described as data, collect them as a []Route and register them in one call.
type Route struct {
Method string
Pattern string
Handler http.Handler
}func Register(r HandleRouter, routes []Route)Register walks the table and registers each entry through HandleRouter, the minimal method-pattern-handler interface that *Router satisfies.