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

Pass something from http middleware to rpc middleware #1370

Open
dpc opened this issue May 18, 2024 · 4 comments
Open

Pass something from http middleware to rpc middleware #1370

dpc opened this issue May 18, 2024 · 4 comments

Comments

@dpc
Copy link

dpc commented May 18, 2024

I might have missed something, but I don't see a way to pass anything from http middlware to rpc middleware/handler.

I'd like to do authorization over http request, and then depending on the result possibly each rpc would enable/disable certain things etc.

Conceptually it's easy, but there seem to be nothing the two layers together, so it can't be done.

@niklasad1
Copy link
Member

Hey hey,

Have you looked at this example which does passes the HTTP headers to the RPC middleware stuff.

But you are right that it won't work with server builder API instead you have to utilize the "low-level API" and use jsonrpsee as as tower service and start the hyper server yourself.

Another example how we are using it in substrate to disable/enable rpc rate limiting middleware...

I hope that helps...

@dpc
Copy link
Author

dpc commented May 18, 2024

Thanks!

This is a bit mindbending. Do I understand correctly that the approach here is to create an rpc service for every http connection, kinda?

@niklasad1
Copy link
Member

This is a bit mindbending. Do I understand correctly that the approach here is to create an rpc service for every http connection, kinda?

Yes, if you want some state from HTTP request itself such as headers then it's only possible to utilize it by creating a rpc service per connection.

So I get that it's quite tricky to use/understand but that's the trade-off right because we didn't want to introduce any additional custom middleware stuff in jsonrpsee and clone stuff that may not used by users....

@dinhani
Copy link
Contributor

dinhani commented Aug 2, 2024

You can pass data from HTTP to RPC with request extensions.

In this example we parse client identification in the HTTP layer and use it in the RPC layer.

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

3 participants