Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to pass a value to middleware? #74

Open
marcelloh opened this issue Jul 6, 2022 · 2 comments
Open

How to pass a value to middleware? #74

marcelloh opened this issue Jul 6, 2022 · 2 comments

Comments

@marcelloh
Copy link

Normally I would use middleware like this:

router.Use(middleware.MyMiddleware).GET("/demo", myDemoHandler)

But let's say, I want to check in the middleware if the user has permissions to use the handler.
Something like this:

router.Use(middleware.Permissions("RW")).POST("/demo", myDemoHandler)

How can I achieve something like this?

@vmihailenco
Copy link
Member

You can pass the user or perms in context.Context, for example - https://github.com/uptrace/uptrace/blob/master/pkg/tracing/grafana_handler.go#L27-L50

@marcelloh
Copy link
Author

marcelloh commented Jul 6, 2022

So before calling the middleware.Permissions, I have to set the wanted "RW" inside a context that I can read back inside that middleware?

I want to achieve something like:

	router.Use(middleware.Permissions("R")).GET("/demo", myDemoHandler)
	router.Use(middleware.Permissions("R")).POST("/demo", myDemoHandler)
	router.Use(middleware.Permissions("RW")).GET("/secret", mySecretHandler)

In your example, I see that In the middleware, to context is created and passed to the underlying handler method.
(In my desired solution, it needs to happen before that.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants