noproto_server.rb 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 app that helps validate RpcServer without protobuf serialization.
  16. #
  17. # Usage: $ path/to/noproto_server.rb
  18. this_dir = File.expand_path(File.dirname(__FILE__))
  19. lib_dir = File.join(File.dirname(this_dir), 'lib')
  20. $LOAD_PATH.unshift(lib_dir) unless $LOAD_PATH.include?(lib_dir)
  21. require 'grpc'
  22. require 'optparse'
  23. # a simple non-protobuf message class.
  24. class NoProtoMsg
  25. def self.marshal(_o)
  26. ''
  27. end
  28. def self.unmarshal(_o)
  29. NoProtoMsg.new
  30. end
  31. end
  32. # service the uses the non-protobuf message class.
  33. class NoProtoService
  34. include GRPC::GenericService
  35. rpc :AnRPC, NoProtoMsg, NoProtoMsg
  36. end
  37. # an implementation of the non-protobuf service.
  38. class NoProto < NoProtoService
  39. def initialize(_default_var = 'ignored')
  40. end
  41. def an_rpc(req, _call)
  42. GRPC.logger.info('echo service received a request')
  43. req
  44. end
  45. end
  46. def load_test_certs
  47. this_dir = File.expand_path(File.dirname(__FILE__))
  48. data_dir = File.join(File.dirname(this_dir), 'spec/testdata')
  49. files = ['ca.pem', 'server1.key', 'server1.pem']
  50. files.map { |f| File.open(File.join(data_dir, f)).read }
  51. end
  52. def test_server_creds
  53. certs = load_test_certs
  54. GRPC::Core::ServerCredentials.new(
  55. nil, [{ private_key: certs[1], cert_chain: certs[2] }], false)
  56. end
  57. def main
  58. options = {
  59. 'host' => 'localhost:9090',
  60. 'secure' => false
  61. }
  62. OptionParser.new do |opts|
  63. opts.banner = 'Usage: [--host <hostname>:<port>] [--secure|-s]'
  64. opts.on('--host HOST', '<hostname>:<port>') do |v|
  65. options['host'] = v
  66. end
  67. opts.on('-s', '--secure', 'access using test creds') do |v|
  68. options['secure'] = v
  69. end
  70. end.parse!
  71. s = GRPC::RpcServer.new
  72. if options['secure']
  73. s.add_http2_port(options['host'], test_server_creds)
  74. GRPC.logger.info("... running securely on #{options['host']}")
  75. else
  76. s.add_http2_port(options['host'], :this_port_is_insecure)
  77. GRPC.logger.info("... running insecurely on #{options['host']}")
  78. end
  79. s.handle(NoProto)
  80. s.run_till_terminated
  81. end
  82. main