I'm encountering an issue with route setup in Go using Fiber.
app := fiber.New()
app.Get("/", func(c *fiber.Ctx) error {
return c.SendString("Hello, World!")
})
basicAuth1 := basicauth.New(basicauth.Config{
Users: map[string]string{
"go": "123",
},
Unauthorized: func(ctx *fiber.Ctx) error {
return fiber.NewError(fiber.StatusForbidden, "Error BasicAuth V1")
},
})
basicAuth2 := basicauth.New(basicauth.Config{
Users: map[string]string{
"go": "321",
},
Unauthorized: func(ctx *fiber.Ctx) error {
return fiber.NewError(fiber.StatusForbidden, "Error BasicAuth V2")
},
})
base := app.Group("/api")
v1 := base.Group("/", basicAuth1)
v1.Get("/v1", func(ctx *fiber.Ctx) error {
return ctx.SendString("a from v1")
})
v2 := base.Group("/", basicAuth2)
v2.Get("/v2", func(ctx *fiber.Ctx) error {
return ctx.SendString("a from v2")
})
app.Listen(":3000")
When I attempt to access the api/v2 endpoint, I receive an error from the basicAuth1 middleware.
This middleware should only be applied to a different group and not affect api/v2.
I didn't experience this issue with similar middleware usage and group configuration in Echo. and I just want grouping handler by middleware.
Grouping endpoint by its Middleware
In your code, you're experiencing this issue because you're attaching both
basicAuth1andbasicAuth2to the same base group (base). When you definev1andv2groups, you're attaching the respective basic auth middleware to them, but you're not removing or overriding the middleware from the parent group (base).To resolve this issue you can structure code like this:
I have attached the
basicauthmiddleware directly to each sub-group (v1andv2) rather than attaching it to the base group (base). This way, each group has its own middleware configuration, and endpoints within those groups will only be affected by the middleware attached to their specific group.Testing /api/v1 endpoint:
Testing /api/v2 endpoint: