// Copyright 2018 The Bazel Authors. All rights reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. extern crate grpc; extern crate helloworld_proto; extern crate tls_api_stub; use std::env; use std::str::FromStr; use std::thread; use helloworld_proto::*; struct GreeterImpl; impl Greeter for GreeterImpl { fn say_hello( &self, _m: grpc::RequestOptions, req: HelloRequest, ) -> grpc::SingleResponse { let mut r = HelloReply::new(); let name = if req.get_name().is_empty() { "world" } else { req.get_name() }; println!("greeting request from {name}"); r.set_message(format!("Hello {name}")); grpc::SingleResponse::completed(r) } } fn main() { let mut server = grpc::ServerBuilder::::new(); let port = u16::from_str(&env::args().nth(1).unwrap_or_else(|| "50051".to_owned())).unwrap(); server.http.set_port(port); server.add_service(GreeterServer::new_service_def(GreeterImpl)); server.http.set_cpu_pool_threads(4); let server = server.build().expect("server"); let port = server.local_addr().port().unwrap(); println!("greeter server started on port {port}"); loop { thread::park(); } }