Tim Emiola | 3acf05a | 2015-01-13 07:55:34 -0800 | [diff] [blame] | 1 | #!/usr/bin/env ruby |
| 2 | |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 3 | # Copyright 2015 gRPC authors. |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 4 | # |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 8 | # |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 10 | # |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 16 | |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 17 | # Sample app that helps validate RpcServer without protobuf serialization. |
| 18 | # |
| 19 | # Usage: $ path/to/noproto_server.rb |
| 20 | |
| 21 | this_dir = File.expand_path(File.dirname(__FILE__)) |
| 22 | lib_dir = File.join(File.dirname(this_dir), 'lib') |
| 23 | $LOAD_PATH.unshift(lib_dir) unless $LOAD_PATH.include?(lib_dir) |
| 24 | |
| 25 | require 'grpc' |
temiola | 0f0a6bc | 2015-01-07 18:43:40 -0800 | [diff] [blame] | 26 | require 'optparse' |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 27 | |
Tim Emiola | e2860c5 | 2015-01-16 02:58:41 -0800 | [diff] [blame] | 28 | # a simple non-protobuf message class. |
temiola | 0f0a6bc | 2015-01-07 18:43:40 -0800 | [diff] [blame] | 29 | class NoProtoMsg |
Tim Emiola | e2860c5 | 2015-01-16 02:58:41 -0800 | [diff] [blame] | 30 | def self.marshal(_o) |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 31 | '' |
| 32 | end |
| 33 | |
Tim Emiola | e2860c5 | 2015-01-16 02:58:41 -0800 | [diff] [blame] | 34 | def self.unmarshal(_o) |
temiola | 0f0a6bc | 2015-01-07 18:43:40 -0800 | [diff] [blame] | 35 | NoProtoMsg.new |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 36 | end |
| 37 | end |
| 38 | |
Tim Emiola | e2860c5 | 2015-01-16 02:58:41 -0800 | [diff] [blame] | 39 | # service the uses the non-protobuf message class. |
temiola | 0f0a6bc | 2015-01-07 18:43:40 -0800 | [diff] [blame] | 40 | class NoProtoService |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 41 | include GRPC::GenericService |
temiola | 0f0a6bc | 2015-01-07 18:43:40 -0800 | [diff] [blame] | 42 | rpc :AnRPC, NoProtoMsg, NoProtoMsg |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 43 | end |
| 44 | |
Tim Emiola | e2860c5 | 2015-01-16 02:58:41 -0800 | [diff] [blame] | 45 | # an implementation of the non-protobuf service. |
temiola | 0f0a6bc | 2015-01-07 18:43:40 -0800 | [diff] [blame] | 46 | class NoProto < NoProtoService |
Tim Emiola | e2860c5 | 2015-01-16 02:58:41 -0800 | [diff] [blame] | 47 | def initialize(_default_var = 'ignored') |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 48 | end |
| 49 | |
Tim Emiola | e2860c5 | 2015-01-16 02:58:41 -0800 | [diff] [blame] | 50 | def an_rpc(req, _call) |
Nick Gauthier | f233d96 | 2015-05-20 14:02:50 -0400 | [diff] [blame] | 51 | GRPC.logger.info('echo service received a request') |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 52 | req |
| 53 | end |
| 54 | end |
| 55 | |
temiola | 0f0a6bc | 2015-01-07 18:43:40 -0800 | [diff] [blame] | 56 | def load_test_certs |
| 57 | this_dir = File.expand_path(File.dirname(__FILE__)) |
| 58 | data_dir = File.join(File.dirname(this_dir), 'spec/testdata') |
| 59 | files = ['ca.pem', 'server1.key', 'server1.pem'] |
| 60 | files.map { |f| File.open(File.join(data_dir, f)).read } |
| 61 | end |
| 62 | |
| 63 | def test_server_creds |
| 64 | certs = load_test_certs |
Tim Emiola | 73a540a | 2015-08-28 18:56:17 -0700 | [diff] [blame] | 65 | GRPC::Core::ServerCredentials.new( |
| 66 | nil, [{ private_key: certs[1], cert_chain: certs[2] }], false) |
temiola | 0f0a6bc | 2015-01-07 18:43:40 -0800 | [diff] [blame] | 67 | end |
| 68 | |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 69 | def main |
temiola | 0f0a6bc | 2015-01-07 18:43:40 -0800 | [diff] [blame] | 70 | options = { |
| 71 | 'host' => 'localhost:9090', |
| 72 | 'secure' => false |
| 73 | } |
| 74 | OptionParser.new do |opts| |
| 75 | opts.banner = 'Usage: [--host <hostname>:<port>] [--secure|-s]' |
| 76 | opts.on('--host HOST', '<hostname>:<port>') do |v| |
| 77 | options['host'] = v |
| 78 | end |
| 79 | opts.on('-s', '--secure', 'access using test creds') do |v| |
Tim Emiola | e2860c5 | 2015-01-16 02:58:41 -0800 | [diff] [blame] | 80 | options['secure'] = v |
temiola | 0f0a6bc | 2015-01-07 18:43:40 -0800 | [diff] [blame] | 81 | end |
| 82 | end.parse! |
| 83 | |
Tim Emiola | 0ce8edc | 2015-03-05 15:17:30 -0800 | [diff] [blame] | 84 | s = GRPC::RpcServer.new |
temiola | 0f0a6bc | 2015-01-07 18:43:40 -0800 | [diff] [blame] | 85 | if options['secure'] |
Tim Emiola | 0ce8edc | 2015-03-05 15:17:30 -0800 | [diff] [blame] | 86 | s.add_http2_port(options['host'], test_server_creds) |
Nick Gauthier | f233d96 | 2015-05-20 14:02:50 -0400 | [diff] [blame] | 87 | GRPC.logger.info("... running securely on #{options['host']}") |
temiola | 0f0a6bc | 2015-01-07 18:43:40 -0800 | [diff] [blame] | 88 | else |
Tim Emiola | c03138a | 2015-09-24 13:11:03 -0700 | [diff] [blame] | 89 | s.add_http2_port(options['host'], :this_port_is_insecure) |
Nick Gauthier | f233d96 | 2015-05-20 14:02:50 -0400 | [diff] [blame] | 90 | GRPC.logger.info("... running insecurely on #{options['host']}") |
temiola | 0f0a6bc | 2015-01-07 18:43:40 -0800 | [diff] [blame] | 91 | end |
| 92 | |
| 93 | s.handle(NoProto) |
Tim Emiola | 321871e | 2015-04-16 12:56:11 -0700 | [diff] [blame] | 94 | s.run_till_terminated |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 95 | end |
| 96 | |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 97 | main |