Closed jaysonsantos closed 1 year ago
A way I've managed to make it compile was by building a wrapper, maybe is it worth it to generate on the codegen crate?
diff --git a/tonic-web/tests/integration/tests/grpc_web.rs b/tonic-web/tests/integration/tests/grpc_web.rs
index e062a83..dd2645d 100644
--- a/tonic-web/tests/integration/tests/grpc_web.rs
+++ b/tonic-web/tests/integration/tests/grpc_web.rs
@@ -1,9 +1,11 @@
+use std::convert::Infallible;
use std::net::SocketAddr;
+use std::task::{Context, Poll};
use base64::Engine as _;
use bytes::{Buf, BufMut, Bytes, BytesMut};
use hyper::http::{header, StatusCode};
-use hyper::{Body, Client, Method, Request, Uri};
+use hyper::{Body, Client, Method, Request, Response, Uri};
use prost::Message;
use tokio::net::TcpListener;
use tokio_stream::wrappers::TcpListenerStream;
@@ -11,6 +13,9 @@ use tonic::transport::Server;
use integration::pb::{test_server::TestServer, Input, Output};
use integration::Svc;
+use tonic::body::BoxBody;
+use tonic::codegen::Service;
+use tonic::server::NamedService;
use tonic_web::GrpcWebLayer;
#[tokio::test]
@@ -59,6 +64,38 @@ async fn text_request() {
assert_eq!(&trailers[..], b"grpc-status:0\r\n");
}
+#[derive(Clone)]
+pub struct NamedTestServerWrapper<S>(S);
+impl<S> NamedTestServerWrapper<S> {
+ pub fn new(service: S) -> Self {
+ Self(service)
+ }
+}
+impl<S> Service<Request<Body>> for NamedTestServerWrapper<S>
+where
+ S: Service<Request<Body>, Response = Response<BoxBody>, Error = Infallible>
+ + Clone
+ + Send
+ + 'static,
+ S::Future: Send + 'static,
+{
+ type Response = S::Response;
+ type Error = S::Error;
+ type Future = S::Future;
+
+ fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
+ self.0.poll_ready(cx)
+ }
+
+ fn call(&mut self, req: Request<Body>) -> Self::Future {
+ self.0.call(req)
+ }
+}
+
+impl<S> NamedService for NamedTestServerWrapper<S> {
+ const NAME: &'static str = "test.Test";
+}
+
async fn spawn() -> String {
let addr = SocketAddr::from(([127, 0, 0, 1], 0));
let listener = TcpListener::bind(addr).await.expect("listener");
@@ -69,7 +106,9 @@ async fn spawn() -> String {
Server::builder()
.accept_http1(true)
.layer(GrpcWebLayer::new())
- .add_service(TestServer::new(Svc))
+ .add_service(NamedTestServerWrapper::new(tonic_web::enable(
+ TestServer::new(Svc),
+ )))
.serve_with_incoming(listener_stream)
.await
.unwrap()
This PR might be related. https://github.com/hyperium/tonic/pull/1286
Kinda a duplicate https://github.com/hyperium/tonic/issues/270 will be fixed in the next release.
@LucioFranco thanks for the fix! When will the next release be out?
Yes, will come in the next release of tonic, I just have some tests to write before we make the release since it will contain some important/cross cutting changes.
Bug Report
Hi there, first thank you very much for this lib, it has been a pleasure working with it!
Version
Platform
Crates
Description
While trying to follow the docs to enable cors in my service, I found that it actually does not work because Server::add_service requires NamedService to work.
A simple way to test it is to patch tonic_web's own test with:
and the build output is