This repository has been archived by the owner on Apr 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 73
/
server.rs
59 lines (42 loc) · 1.5 KB
/
server.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
#![deny(warnings, rust_2018_idioms)]
use crate::hello_world::{server, HelloReply, HelloRequest};
use futures::{future, Future, Stream};
use log::error;
use tokio::net::TcpListener;
use tower_grpc::{Request, Response};
use tower_hyper::server::{Http, Server};
pub mod hello_world {
include!(concat!(env!("OUT_DIR"), "/helloworld.rs"));
}
#[derive(Clone, Debug)]
struct Greet;
impl server::Greeter for Greet {
type SayHelloFuture = future::FutureResult<Response<HelloReply>, tower_grpc::Status>;
fn say_hello(&mut self, request: Request<HelloRequest>) -> Self::SayHelloFuture {
println!("REQUEST = {:?}", request);
let response = Response::new(HelloReply {
message: "Zomg, it works!".to_string(),
});
future::ok(response)
}
}
pub fn main() {
let _ = ::env_logger::init();
let new_service = server::GreeterServer::new(Greet);
let mut server = Server::new(new_service);
let http = Http::new().http2_only(true).clone();
let addr = "[::1]:50051".parse().unwrap();
let bind = TcpListener::bind(&addr).expect("bind");
let serve = bind
.incoming()
.for_each(move |sock| {
if let Err(e) = sock.set_nodelay(true) {
return Err(e);
}
let serve = server.serve_with(sock, http.clone());
tokio::spawn(serve.map_err(|e| error!("hyper error: {:?}", e)));
Ok(())
})
.map_err(|e| eprintln!("accept error: {}", e));
tokio::run(serve)
}