-
Notifications
You must be signed in to change notification settings - Fork 218
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
Adds support for grpc-web #206
Conversation
This is awesome |
Coming from an embedded perspective, being able to use this saves a lot of cpu cycles compared to my previous proxies. Since the client now can be a web browser the library need to handle CORS. To get this operational I needed to do quite a lot of coors catchup resulting in the following changes from this grpc/lib/grpc/transport/http2.ex Lines 11 to 17 in 5b4c66c
to: def server_headers(%{codec: GRPC.Codec.WebText = codec}) do
%{"content-type" => "application/grpc-web-#{codec.name}",
"Access-Control-Allow-Origin" => "*",
"Access-Control-Allow-Headers" => "Content-Type, x-grpc-web, x-user-agent",
}
end
def server_headers(%{codec: codec}) do
%{"content-type" => "application/grpc+#{codec.name}",
"Access-Control-Allow-Origin" => "*",
"Access-Control-Allow-Headers" => "Content-Type, x-grpc-web, x-user-agent",
}
end I'm assuming there is already a mechanism in place on this but I can't wrap my around it so please advice me. Some conclusions.
@drowzy Possibly you have the answer up your sleeve :) |
I think perhaps the best place to implement this is directly in Cowboy Handler https://github.com/elixir-grpc/grpc/blob/master/lib/grpc/adapter/cowboy/handler.ex |
Any predictions for this to be approved? I think this library seems to have been abandoned by its creators |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks great overall! I've left some comments so we can iterate a bit :)
wonderful to see some activity here! Let's try and get some nice solution the the cors issue described above, possibly in a separate issue. Short recap, We need to be able to provide headers in a configurable way. I've played with Interceptors (for other purposes), however I don't see a clean match since it will affect all services - regardless if they use grpc-web or not. |
@AleksandarFilipov I think we could use the interceptors in a similar way to how Tesla modifies the current response given the result of https://github.com/elixir-tesla/tesla/blob/master/lib/tesla/middleware/compression.ex In this example, they decide if the resp body needs to be decompressed based on the result tuple tag. We can provide a custom interceptor that injects response headers based on a predicate MFA tuple (that receives the stream and returns true if the headers should be injected). Something like: defmodule MyServer do
use GRPC.Endpoint
intercept GRPC.Interceptors.ResponseHeaders, should_inject: {__MODULE__, :"add_cors?", 1}, headers: [{"Access-Control-Allow-Origin", "*"}, {"Access-Control-Allow-Headers", "Content-Type, x-grpc-web, x-user-agent"}]
def add_cors?(...), do: ...
end The one thing that might need to change (I'm not that familiar with the code base yet) is that this only works if I'm almost sure that Shall we move this into a new issue? |
Co-authored-by: Paulo Valente <16843419+polvalente@users.noreply.github.com>
Co-authored-by: Paulo Valente <16843419+polvalente@users.noreply.github.com>
Co-authored-by: Paulo Valente <16843419+polvalente@users.noreply.github.com>
lib/grpc/codec/web_text.ex
Outdated
data | ||
|> IO.iodata_to_binary() | ||
|> pack_for_channel() | ||
|> List.wrap() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
|> List.wrap() |
We don't need this because a binary is also a valid iodata instance :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome work guys! |
Adds support for grpc-web.
GRPC.Codec
behaviour in order to base64 decode / encode the payload.There's an example repo to see it in action.
closes #193