-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rs
61 lines (51 loc) · 1.59 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
use hello_world::greeter_server::{Greeter, GreeterServer};
use hello_world::{HelloReply, HelloRequest};
use hyper::server::conn::Http;
use std::net::SocketAddr;
use tokio::net::TcpListener;
use tonic::{Request, Response, Status};
use tower::Service;
pub mod hello_world {
tonic::include_proto!("helloworld");
}
tokio::task_local! {
static ADDR: SocketAddr;
}
#[tokio::main]
async fn main() {
let http = Http::new().http2_only(true).clone();
let mut listener = TcpListener::bind("[::1]:50051").await.unwrap();
let greeter = MyGreeter::default();
let svc = GreeterServer::new(greeter);
while let Ok((conn, addr)) = listener.accept().await {
let http = http.clone();
let svc = svc.clone();
tokio::spawn(async move {
let svc = svc.clone();
http.serve_connection(
conn,
hyper::service::service_fn(|req: hyper::Request<hyper::Body>| {
let mut svc = svc.clone();
async move { ADDR.scope(addr, async move { svc.call(req).await }).await }
}),
)
.await
.unwrap();
});
}
}
#[derive(Default)]
pub struct MyGreeter {}
#[tonic::async_trait]
impl Greeter for MyGreeter {
async fn say_hello(
&self,
request: Request<HelloRequest>,
) -> Result<Response<HelloReply>, Status> {
println!("Got a request from {:?}", ADDR.get());
let reply = hello_world::HelloReply {
message: format!("Hello {}!", request.into_inner().name),
};
Ok(Response::new(reply))
}
}