Skip to content

🚀 [Feature]: Get Route Name/Path inside Middlewares #2195

@mirusky

Description

@mirusky

Feature Description

Expose the route name inside middleware before reach the end handler / last handler in the stack.

Additional Context (optional)

Currently I'm trying to implement a RBAC system, each route has a name defined like "resource.action" eg: user.create, user.read...

And the idea is provide an API where user can change the necessary permissions for each route. Like: the route called user.create is necessary to be have admin or user:create or user:* permission.

So the Middleware will check the current route name and validate if the logged user has the appropriate permissions.

Some time ago I had seen a similar issue, but I remember that it was mentioned that the way the stack is built would have to be modified. I didn't found the Issue to put it here...

EDIT:
Related Issues #1334 #923

Code Snippet (optional)

package main

import (
	"fmt"

	"github.com/gofiber/fiber/v2"
)

func main() {
	app := fiber.New()
	// app.Use(logger.New())
	// app.Use(cors.New())

	app.Use(func(c *fiber.Ctx) error {

		fmt.Printf("%+v\n", c.Route().Name) // Prints ""

		err := c.Next()

		fmt.Printf("%+v\n", c.Route().Name) // Prints "Ping"
		return err
	})

	app.Get("/ping", Ping).Name("Ping")

	app.Listen(":3000")
}

func Ping(c *fiber.Ctx) error {
	return c.SendString("Pong!")
}

Checklist:

  • I agree to follow Fiber's Code of Conduct.
  • I have checked for existing issues that describe my suggestion prior to opening this one.
  • I understand that improperly formatted feature requests may be closed without explanation.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions