4

As in go-chi, set middleware at the level of individual routes, and not just globally for all routes

// Routes creates a REST router
func Routes() chi.Router {
    r := chi.NewRouter()
    r.Use(middleware.Captcha)

    r.Post("/", Login)

    return r
}

How for Login to specify a unique middleware or to exclude from the general middleware?

Zoyd
  • 3,449
  • 1
  • 18
  • 27
batazor
  • 852
  • 2
  • 16
  • 36

1 Answers1

11

You have two options. The natural way, supported by any router:

r.Post("/", middlewareFunc(Login))

Or if you want to use a Chi-specific way, create a new Group for the one specific endpoint:

loginGroup := r.Group(nil)
loginGroup.Use(middleware)
loginGroup.Post("/", Login)
Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189