greeter_server.rb 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #!/usr/bin/env ruby
  2. # Copyright 2015 gRPC authors.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. # Sample gRPC server that implements the Greeter::Helloworld service.
  16. #
  17. # Usage: $ path/to/greeter_server.rb
  18. this_dir = File.expand_path(File.dirname(__FILE__))
  19. lib_dir = File.join(this_dir, 'lib')
  20. $LOAD_PATH.unshift(lib_dir) unless $LOAD_PATH.include?(lib_dir)
  21. require 'grpc'
  22. require 'helloworld_services_pb'
  23. # GreeterServer is simple server that implements the Helloworld Greeter server.
  24. class GreeterServer < Helloworld::Greeter::Service
  25. # say_hello implements the SayHello rpc method.
  26. def say_hello(hello_req, _unused_call)
  27. Helloworld::HelloReply.new(message: "Hello #{hello_req.name}")
  28. end
  29. end
  30. # main starts an RpcServer that receives requests to GreeterServer at the sample
  31. # server port.
  32. def main
  33. s = GRPC::RpcServer.new
  34. s.add_http2_port('0.0.0.0:50051', :this_port_is_insecure)
  35. s.handle(GreeterServer)
  36. # Runs the server with SIGHUP, SIGINT and SIGQUIT signal handlers to
  37. # gracefully shutdown.
  38. # User could also choose to run server via call to run_till_terminated
  39. s.run_till_terminated_or_interrupted([1, 'int', 'SIGQUIT'])
  40. end
  41. main