batazor
batazor

Reputation: 1012

Middleware on a specific route

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?

Upvotes: 4

Views: 5437

Answers (1)

Jonathan Hall
Jonathan Hall

Reputation: 79794

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)

Upvotes: 13

Related Questions