2

I have the following grouped routes, that use different middlewares

func userRoutes(handlers *Handler) *mux.Router {
    router := mux.NewRouter()

    // Routes here
    router.HandleFunc("/accounts", handlers.ListAccounts).Name("list-account")
    router.HandleFunc("/accounts/{id}", handlers.SingleAccount).Name("single-account")
    
    // Middleware registration
    router.Use(
        middleware.Logger,)
    return router
}

func staffRoutes(handlers *Handler) *mux.Router {
    router := mux.NewRouter()

    // Routes here

    // Middleware registration
    router.Use()
    return router
}

func adminRoutes(handlers *Handler) *mux.Router {
    router := mux.NewRouter()

    // Routes here

    // Middleware registration
    router.Use()
    return router
}

How do I bind these to a single router and still have the middleware isolated in each group?

DeeStarks
  • 320
  • 5
  • 16
  • 2
    Pass the main mux to each group. Create a [subrouter](https://pkg.go.dev/github.com/gorilla/mux#Route.Subrouter) in the group and attach middlewares to that subrouter. –  Mar 24 '22 at 21:35

0 Answers0