diff --git a/examples/Cargo.toml b/examples/Cargo.toml index 2239336e1..57d05d3e3 100644 --- a/examples/Cargo.toml +++ b/examples/Cargo.toml @@ -149,16 +149,6 @@ path = "src/interceptor/client.rs" name = "interceptor-server" path = "src/interceptor/server.rs" -[[bin]] -name = "hyper-warp-client" -path = "src/hyper_warp/client.rs" -required-features = ["hyper-warp"] - -[[bin]] -name = "hyper-warp-server" -path = "src/hyper_warp/server.rs" -required-features = ["hyper-warp"] - [[bin]] name = "health-server" path = "src/health/server.rs" @@ -178,16 +168,6 @@ required-features = ["autoreload"] name = "optional-server" path = "src/optional/server.rs" -[[bin]] -name = "hyper-warp-multiplex-client" -path = "src/hyper_warp_multiplex/client.rs" -required-features = ["hyper-warp-multiplex"] - -[[bin]] -name = "hyper-warp-multiplex-server" -path = "src/hyper_warp_multiplex/server.rs" -required-features = ["hyper-warp-multiplex"] - [[bin]] name = "compression-server" path = "src/compression/server.rs" @@ -293,8 +273,6 @@ autoreload = ["tokio-stream/net", "dep:listenfd"] health = ["dep:tonic-health"] grpc-web = ["dep:tonic-web", "dep:bytes", "dep:http", "dep:hyper", "dep:tracing-subscriber", "dep:tower"] tracing = ["dep:tracing", "dep:tracing-subscriber"] -hyper-warp = ["dep:either", "dep:tower", "dep:hyper", "dep:http", "dep:http-body", "dep:warp"] -hyper-warp-multiplex = ["hyper-warp"] uds = ["tokio-stream/net", "dep:tower", "dep:hyper"] streaming = ["tokio-stream", "dep:h2"] mock = ["tokio-stream", "dep:tower"] @@ -310,7 +288,7 @@ types = ["dep:tonic-types"] h2c = ["dep:hyper", "dep:tower", "dep:http"] cancellation = ["dep:tokio-util"] -full = ["gcp", "routeguide", "reflection", "autoreload", "health", "grpc-web", "tracing", "hyper-warp", "hyper-warp-multiplex", "uds", "streaming", "mock", "tower", "json-codec", "compression", "tls", "tls-rustls", "dynamic-load-balance", "timeout", "tls-client-auth", "types", "cancellation", "h2c"] +full = ["gcp", "routeguide", "reflection", "autoreload", "health", "grpc-web", "tracing", "uds", "streaming", "mock", "tower", "json-codec", "compression", "tls", "tls-rustls", "dynamic-load-balance", "timeout", "tls-client-auth", "types", "cancellation", "h2c"] default = ["full"] [dependencies] @@ -323,7 +301,6 @@ tonic-web = { path = "../tonic-web", optional = true } tonic-health = { path = "../tonic-health", optional = true } tonic-reflection = { path = "../tonic-reflection", optional = true } tonic-types = { path = "../tonic-types", optional = true } -either = { version = "1.9", optional = true } async-stream = { version = "0.3", optional = true } tokio-stream = { version = "0.1", optional = true } tokio-util = { version = "0.7.8", optional = true } @@ -337,7 +314,6 @@ prost-types = { version = "0.12", optional = true } http = { version = "0.2", optional = true } http-body = { version = "0.4.2", optional = true } hyper = { version = "0.14", optional = true } -warp = { version = "0.3.4", default-features = false, optional = true } listenfd = { version = "1.0", optional = true } bytes = { version = "1", optional = true } h2 = { version = "0.3", optional = true } diff --git a/examples/src/hyper_warp/client.rs b/examples/src/hyper_warp/client.rs deleted file mode 100644 index 49091639e..000000000 --- a/examples/src/hyper_warp/client.rs +++ /dev/null @@ -1,47 +0,0 @@ -//! To hit the gRPC endpoint you must run this client via: -//! `cargo run --bin hyper-warp-client -//! To hit the warp server you can run this command: -//! `curl localhost:50051/hello` - -use hello_world::greeter_client::GreeterClient; -use hello_world::HelloRequest; -use hyper::{Client, Uri}; - -pub mod hello_world { - tonic::include_proto!("helloworld"); -} - -#[tokio::main] -async fn main() -> Result<(), Box> { - let client = Client::builder().http2_only(true).build_http(); - - let uri = Uri::from_static("http://[::1]:50051"); - - // Hyper's client requires that requests contain full Uris include a scheme and - // an authority. Tonic's transport will handle this for you but when using the client - // manually you need ensure the uri's are set correctly. - let add_origin = tower::service_fn(|mut req: hyper::Request| { - let uri = Uri::builder() - .scheme(uri.scheme().unwrap().clone()) - .authority(uri.authority().unwrap().clone()) - .path_and_query(req.uri().path_and_query().unwrap().clone()) - .build() - .unwrap(); - - *req.uri_mut() = uri; - - client.request(req) - }); - - let mut client = GreeterClient::new(add_origin); - - let request = tonic::Request::new(HelloRequest { - name: "Tonic".into(), - }); - - let response = client.say_hello(request).await?; - - println!("RESPONSE={:?}", response); - - Ok(()) -} diff --git a/examples/src/hyper_warp/server.rs b/examples/src/hyper_warp/server.rs deleted file mode 100644 index a79caf401..000000000 --- a/examples/src/hyper_warp/server.rs +++ /dev/null @@ -1,126 +0,0 @@ -//! To hit the gRPC endpoint you must run this client via: -//! `cargo run --bin hyper-warp-client -//! To hit the warp server you can run this command: -//! `curl localhost:50051/hello` - -use either::Either; -use http::version::Version; -use hyper::{service::make_service_fn, Server}; -use std::convert::Infallible; -use std::{ - pin::Pin, - task::{Context, Poll}, -}; -use tonic::{Request, Response, Status}; -use tower::Service; -use warp::Filter; - -use hello_world::greeter_server::{Greeter, GreeterServer}; -use hello_world::{HelloReply, HelloRequest}; - -type Error = Box; - -pub mod hello_world { - tonic::include_proto!("helloworld"); -} - -#[derive(Default)] -pub struct MyGreeter {} - -#[tonic::async_trait] -impl Greeter for MyGreeter { - async fn say_hello( - &self, - request: Request, - ) -> Result, Status> { - let reply = hello_world::HelloReply { - message: format!("Hello {}!", request.into_inner().name), - }; - Ok(Response::new(reply)) - } -} - -#[tokio::main] -async fn main() -> Result<(), Box> { - let addr = "[::1]:50051".parse().unwrap(); - let greeter = MyGreeter::default(); - - println!("GreeterServer listening on {}", addr); - - let tonic = GreeterServer::new(greeter); - let mut warp = warp::service(warp::path("hello").map(|| "hello, world!")); - - Server::bind(&addr) - .serve(make_service_fn(move |_| { - let mut tonic = tonic.clone(); - std::future::ready(Ok::<_, Infallible>(tower::service_fn( - move |req: hyper::Request| match req.version() { - Version::HTTP_11 | Version::HTTP_10 => Either::Left({ - let res = warp.call(req); - Box::pin(async move { - let res = res.await.map(|res| res.map(EitherBody::Left))?; - Ok::<_, Error>(res) - }) - }), - Version::HTTP_2 => Either::Right({ - let res = tonic.call(req); - Box::pin(async move { - let res = res.await.map(|res| res.map(EitherBody::Right))?; - Ok::<_, Error>(res) - }) - }), - _ => unimplemented!(), - }, - ))) - })) - .await?; - - Ok(()) -} - -enum EitherBody { - Left(A), - Right(B), -} - -impl http_body::Body for EitherBody -where - A: http_body::Body + Send + Unpin, - B: http_body::Body + Send + Unpin, - A::Error: Into, - B::Error: Into, -{ - type Data = A::Data; - type Error = Box; - - fn is_end_stream(&self) -> bool { - match self { - EitherBody::Left(b) => b.is_end_stream(), - EitherBody::Right(b) => b.is_end_stream(), - } - } - - fn poll_data( - self: Pin<&mut Self>, - cx: &mut Context<'_>, - ) -> Poll>> { - match self.get_mut() { - EitherBody::Left(b) => Pin::new(b).poll_data(cx).map(map_option_err), - EitherBody::Right(b) => Pin::new(b).poll_data(cx).map(map_option_err), - } - } - - fn poll_trailers( - self: Pin<&mut Self>, - cx: &mut Context<'_>, - ) -> Poll, Self::Error>> { - match self.get_mut() { - EitherBody::Left(b) => Pin::new(b).poll_trailers(cx).map_err(Into::into), - EitherBody::Right(b) => Pin::new(b).poll_trailers(cx).map_err(Into::into), - } - } -} - -fn map_option_err>(err: Option>) -> Option> { - err.map(|e| e.map_err(Into::into)) -} diff --git a/examples/src/hyper_warp_multiplex/client.rs b/examples/src/hyper_warp_multiplex/client.rs deleted file mode 100644 index a2b94a1d9..000000000 --- a/examples/src/hyper_warp_multiplex/client.rs +++ /dev/null @@ -1,61 +0,0 @@ -//! To hit the gRPC endpoint you must run this client via: -//! `cargo run --bin hyper-warp-multiplex-client -//! To hit the warp server you can run this command: -//! `curl localhost:50051/hello` - -pub mod hello_world { - tonic::include_proto!("helloworld"); -} - -pub mod echo { - tonic::include_proto!("grpc.examples.unaryecho"); -} - -use echo::{echo_client::EchoClient, EchoRequest}; -use hello_world::greeter_client::GreeterClient; -use hello_world::HelloRequest; -use hyper::{Client, Uri}; - -#[tokio::main] -async fn main() -> Result<(), Box> { - let client = Client::builder().http2_only(true).build_http(); - - let uri = Uri::from_static("http://[::1]:50051"); - - // Hyper's client requires that requests contain full Uris include a scheme and - // an authority. Tonic's transport will handle this for you but when using the client - // manually you need ensure the uri's are set correctly. - let add_origin = tower::service_fn(|mut req: hyper::Request| { - let uri = Uri::builder() - .scheme(uri.scheme().unwrap().clone()) - .authority(uri.authority().unwrap().clone()) - .path_and_query(req.uri().path_and_query().unwrap().clone()) - .build() - .unwrap(); - - *req.uri_mut() = uri; - - client.request(req) - }); - - let mut greeter_client = GreeterClient::new(add_origin); - let mut echo_client = EchoClient::new(add_origin); - - let request = tonic::Request::new(HelloRequest { - name: "Tonic".into(), - }); - - let response = greeter_client.say_hello(request).await?; - - println!("GREETER RESPONSE={:?}", response); - - let request = tonic::Request::new(EchoRequest { - message: "hello".into(), - }); - - let response = echo_client.unary_echo(request).await?; - - println!("ECHO RESPONSE={:?}", response); - - Ok(()) -} diff --git a/examples/src/hyper_warp_multiplex/server.rs b/examples/src/hyper_warp_multiplex/server.rs deleted file mode 100644 index deea8bea5..000000000 --- a/examples/src/hyper_warp_multiplex/server.rs +++ /dev/null @@ -1,153 +0,0 @@ -//! To hit the gRPC endpoint you must run this client via: -//! `cargo run --bin hyper-warp-multiplex-client -//! To hit the warp server you can run this command: -//! `curl localhost:50051/hello` - -use either::Either; -use http::version::Version; -use hyper::{service::make_service_fn, Server}; -use std::convert::Infallible; -use std::{ - pin::Pin, - task::{Context, Poll}, -}; -use tonic::{transport::Server as TonicServer, Request, Response, Status}; -use tower::Service; -use warp::Filter; - -type Error = Box; - -pub mod hello_world { - tonic::include_proto!("helloworld"); -} - -pub mod echo { - tonic::include_proto!("grpc.examples.unaryecho"); -} -use hello_world::{ - greeter_server::{Greeter, GreeterServer}, - HelloReply, HelloRequest, -}; - -use echo::{ - echo_server::{Echo, EchoServer}, - EchoRequest, EchoResponse, -}; - -#[derive(Default)] -pub struct MyGreeter {} - -#[tonic::async_trait] -impl Greeter for MyGreeter { - async fn say_hello( - &self, - request: Request, - ) -> Result, Status> { - let reply = hello_world::HelloReply { - message: format!("Hello {}!", request.into_inner().name), - }; - Ok(Response::new(reply)) - } -} - -#[derive(Default)] -pub struct MyEcho; - -#[tonic::async_trait] -impl Echo for MyEcho { - async fn unary_echo( - &self, - request: Request, - ) -> Result, Status> { - let message = request.into_inner().message; - Ok(Response::new(EchoResponse { message })) - } -} - -#[tokio::main] -async fn main() -> Result<(), Box> { - let addr = "[::1]:50051".parse().unwrap(); - - let mut warp = warp::service(warp::path("hello").map(|| "hello, world!")); - - Server::bind(&addr) - .serve(make_service_fn(move |_| { - let greeter = GreeterServer::new(MyGreeter::default()); - let echo = EchoServer::new(MyEcho::default()); - - let mut tonic = TonicServer::builder() - .add_service(greeter) - .add_service(echo) - .into_service(); - - std::future::ready(Ok::<_, Infallible>(tower::service_fn( - move |req: hyper::Request| match req.version() { - Version::HTTP_11 | Version::HTTP_10 => Either::Left({ - let res = warp.call(req); - Box::pin(async move { - let res = res.await.map(|res| res.map(EitherBody::Left))?; - Ok::<_, Error>(res) - }) - }), - Version::HTTP_2 => Either::Right({ - let res = tonic.call(req); - Box::pin(async move { - let res = res.await.map(|res| res.map(EitherBody::Right))?; - Ok::<_, Error>(res) - }) - }), - _ => unimplemented!(), - }, - ))) - })) - .await?; - - Ok(()) -} - -enum EitherBody { - Left(A), - Right(B), -} - -impl http_body::Body for EitherBody -where - A: http_body::Body + Send + Unpin, - B: http_body::Body + Send + Unpin, - A::Error: Into, - B::Error: Into, -{ - type Data = A::Data; - type Error = Box; - - fn is_end_stream(&self) -> bool { - match self { - EitherBody::Left(b) => b.is_end_stream(), - EitherBody::Right(b) => b.is_end_stream(), - } - } - - fn poll_data( - self: Pin<&mut Self>, - cx: &mut Context<'_>, - ) -> Poll>> { - match self.get_mut() { - EitherBody::Left(b) => Pin::new(b).poll_data(cx).map(map_option_err), - EitherBody::Right(b) => Pin::new(b).poll_data(cx).map(map_option_err), - } - } - - fn poll_trailers( - self: Pin<&mut Self>, - cx: &mut Context<'_>, - ) -> Poll, Self::Error>> { - match self.get_mut() { - EitherBody::Left(b) => Pin::new(b).poll_trailers(cx).map_err(Into::into), - EitherBody::Right(b) => Pin::new(b).poll_trailers(cx).map_err(Into::into), - } - } -} - -fn map_option_err>(err: Option>) -> Option> { - err.map(|e| e.map_err(Into::into)) -}