1 // Copyright 2018 The Bazel Authors. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 extern crate grpc;
16 extern crate helloworld_proto;
17 extern crate tls_api_stub;
18
19 use std::env;
20 use std::str::FromStr;
21 use std::thread;
22
23 use helloworld_proto::*;
24
25 struct GreeterImpl;
26
27 impl Greeter for GreeterImpl {
say_hello( &self, _m: grpc::RequestOptions, req: HelloRequest, ) -> grpc::SingleResponse<HelloReply>28 fn say_hello(
29 &self,
30 _m: grpc::RequestOptions,
31 req: HelloRequest,
32 ) -> grpc::SingleResponse<HelloReply> {
33 let mut r = HelloReply::new();
34 let name = if req.get_name().is_empty() {
35 "world"
36 } else {
37 req.get_name()
38 };
39 println!("greeting request from {name}");
40 r.set_message(format!("Hello {name}"));
41 grpc::SingleResponse::completed(r)
42 }
43 }
44
main()45 fn main() {
46 let mut server = grpc::ServerBuilder::<tls_api_stub::TlsAcceptor>::new();
47 let port = u16::from_str(&env::args().nth(1).unwrap_or_else(|| "50051".to_owned())).unwrap();
48 server.http.set_port(port);
49 server.add_service(GreeterServer::new_service_def(GreeterImpl));
50 server.http.set_cpu_pool_threads(4);
51 let server = server.build().expect("server");
52 let port = server.local_addr().port().unwrap();
53 println!("greeter server started on port {port}");
54
55 loop {
56 thread::park();
57 }
58 }
59