Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 336
batazor Avatar asked Sep 10 '25 18:09

batazor


1 Answers

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)
like image 95
Flimzy Avatar answered Sep 13 '25 09:09

Flimzy