I have a situation when i need to use authorization middleware for all paths, except ones that start with /internal-api prefix. But currently, Use method doesn't implement such functionality.
What i propose is if Use is called with prefix and prefix is starting with ^ symbol -- consider that middleware should be applied to all paths except ones that start with prefix. Another option would be to handle prefix as regexp, but that probably would be overkill.
Thanks for opening your first issue here! 馃帀 Be sure to follow the issue template! If you need help or want to chat with us, join us on Discord https://gofiber.io/discord
Hi @GrigoriyMikhalkin, thanks for opening your first issue here!
Since we want to replicate expressjs behavior regarding routing, your suggestion will probably not be an ideal solution.
Here are some examples that could solve your issue using order or some small middleware checks.
Using order
func main() {
app := fiber.New()
app.Get("/internal-api/get", handler)
app.Put("/internal-api/put", handler)
app.Post("/internal-api/post", handler)
app.Use(func(c *fiber.Ctx) {
// authorization logic
c.Next()
})
app.Get("/secret", handler)
app.Get("/admin", handler)
app.Listen(3000)
}
Verify the prefix
func main() {
app := fiber.New()
app.Use(func(c *fiber.Ctx) {
if !strings.HasPrefix(c.Path(), "/internal-api") {
// authorization logic
c.Next()
}
})
app.Get("/internal-api/get", handler)
app.Put("/internal-api/put", handler)
app.Post("/internal-api/post", handler)
app.Get("/secret", handler)
app.Get("/admin", handler)
app.Listen(3000)
}
I hope this answered your question. A quick side note, we are working on regex support within routes 馃憤
@Fenny Thanks for answer! I currently use second approach. First approach also looks nice, would be good to document it in docs(at least i didn't saw it anywhere before)
Yeah, it would be a good idea to document that order matters like in expressjs. I'm closing this issue, for now, feel free to re-open if you have any further questions 馃憤
Most helpful comment
Hi @GrigoriyMikhalkin, thanks for opening your first issue here!
Since we want to replicate expressjs behavior regarding routing, your suggestion will probably not be an ideal solution.
Here are some examples that could solve your issue using order or some small middleware checks.
Using order
Verify the prefix
I hope this answered your question. A quick side note, we are working on regex support within routes 馃憤