chore(docs): Add hyper example (#267)
Signed-off-by: Lucio Franco <[email protected]>
This commit is contained in:
@@ -94,6 +94,14 @@ path = "src/interceptor/client.rs"
|
|||||||
name = "interceptor-server"
|
name = "interceptor-server"
|
||||||
path = "src/interceptor/server.rs"
|
path = "src/interceptor/server.rs"
|
||||||
|
|
||||||
|
[[bin]]
|
||||||
|
name = "hyper-client"
|
||||||
|
path = "src/hyper/client.rs"
|
||||||
|
|
||||||
|
[[bin]]
|
||||||
|
name = "hyper-server"
|
||||||
|
path = "src/hyper/server.rs"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
tonic = { path = "../tonic", features = ["tls"] }
|
tonic = { path = "../tonic", features = ["tls"] }
|
||||||
prost = "0.6"
|
prost = "0.6"
|
||||||
@@ -112,6 +120,8 @@ tracing-attributes = "0.1"
|
|||||||
tracing-futures = "0.2"
|
tracing-futures = "0.2"
|
||||||
# Required for wellknown types
|
# Required for wellknown types
|
||||||
prost-types = "0.6"
|
prost-types = "0.6"
|
||||||
|
# Hyper example
|
||||||
|
hyper = "0.13"
|
||||||
|
|
||||||
[build-dependencies]
|
[build-dependencies]
|
||||||
tonic-build = { path = "../tonic-build" }
|
tonic-build = { path = "../tonic-build" }
|
||||||
|
|||||||
@@ -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(())
|
||||||
|
}
|
||||||
@@ -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(())
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user