Reputation: 1090
I've been using go-chi for a project and using an auth middleware for routes like this
r := chi.NewRouter()
r.Use(authService.AuthMiddleware)
r.Route("/platform", func(r chi.Router) {
r.Get("/version", RequestPlatformVersion)
})
This is applying to all the routes defined after this declaration which are fine. But now I need to add a route that is used for webhooks. I don't want to apply this middleware to that route since it'll fail. How can I do that?
Upvotes: 1
Views: 3225
Reputation: 1
For multiple routes that need to use the same middleware, a group is another option:
router := chi.NewRouter()
router.Get("/healthcheck", HandleHealthCheck)
router.Group(func(r chi.Router) {
r.Use(authService.AuthMiddleware)
r.Use(middleware.Recoverer)
r.Get("/route1", HandleRoute1)
r.Get("/route2", HandleRoute2)
r.Post("/route3", HandleRoute3)
})
Upvotes: 0
Reputation: 45220
You can set the middleware within the /platform
route:
r.Route("/platform", func(r chi.Router) {
r.Use(authService.AuthMiddleware)
r.Get("/version", RequestPlatformVersion)
})
r.Route("/webhooks", func(r chi.Router) {
r.Get("/", ...)
})
Upvotes: 2