xref: /tonic/examples/src/grpc-web/client.rs (revision 9c1f2f94)
1 use hello_world::{greeter_client::GreeterClient, HelloRequest};
2 use hyper_util::rt::TokioExecutor;
3 use tonic_web::GrpcWebClientLayer;
4 
5 pub mod hello_world {
6     tonic::include_proto!("helloworld");
7 }
8 
9 #[tokio::main]
main() -> Result<(), Box<dyn std::error::Error>>10 async fn main() -> Result<(), Box<dyn std::error::Error>> {
11     // Must use hyper directly...
12     let client = hyper_util::client::legacy::Client::builder(TokioExecutor::new()).build_http();
13 
14     let svc = tower::ServiceBuilder::new()
15         .layer(GrpcWebClientLayer::new())
16         .service(client);
17 
18     let mut client = GreeterClient::with_origin(svc, "http://127.0.0.1:3000".try_into()?);
19 
20     let request = tonic::Request::new(HelloRequest {
21         name: "Tonic".into(),
22     });
23 
24     let response = client.say_hello(request).await?;
25 
26     println!("RESPONSE={:?}", response);
27 
28     Ok(())
29 }
30