chore(docs): Add hyper example (#267)

Signed-off-by: Lucio Franco <luciofranco14@gmail.com>
This commit is contained in:
Lucio Franco
2020-02-16 12:46:55 -05:00
committed by GitHub
parent 4f343eb980
commit b49ad9bb47
3 changed files with 98 additions and 0 deletions
+10
View File
@@ -94,6 +94,14 @@ path = "src/interceptor/client.rs"
name = "interceptor-server"
path = "src/interceptor/server.rs"
[[bin]]
name = "hyper-client"
path = "src/hyper/client.rs"
[[bin]]
name = "hyper-server"
path = "src/hyper/server.rs"
[dependencies]
tonic = { path = "../tonic", features = ["tls"] }
prost = "0.6"
@@ -112,6 +120,8 @@ tracing-attributes = "0.1"
tracing-futures = "0.2"
# Required for wellknown types
prost-types = "0.6"
# Hyper example
hyper = "0.13"
[build-dependencies]
tonic-build = { path = "../tonic-build" }
+42
View File
@@ -0,0 +1,42 @@
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<dyn std::error::Error>> {
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<tonic::body::BoxBody>| {
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(())
}
+46
View File
@@ -0,0 +1,46 @@
use futures::future;
use hyper::{service::make_service_fn, Server};
use std::convert::Infallible;
use tonic::{Request, Response, Status};
use hello_world::greeter_server::{Greeter, GreeterServer};
use hello_world::{HelloReply, HelloRequest};
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<HelloRequest>,
) -> Result<Response<HelloReply>, Status> {
let reply = hello_world::HelloReply {
message: format!("Hello {}!", request.into_inner().name),
};
Ok(Response::new(reply))
}
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let addr = "[::1]:50051".parse().unwrap();
let greeter = MyGreeter::default();
println!("GreeterServer listening on {}", addr);
let svc = GreeterServer::new(greeter);
Server::bind(&addr)
.http2_only(true)
.serve(make_service_fn(|_| {
future::ok::<_, Infallible>(svc.clone())
}))
.await?;
Ok(())
}