blob: 609a0dbcc7c0e6516e18360163235f01896346fe [file] [log] [blame]
Tim Emiola3acf05a2015-01-13 07:55:34 -08001#!/usr/bin/env ruby
2
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003# Copyright 2015 gRPC authors.
nnoble097ef9b2014-12-01 17:06:10 -08004#
Jan Tattermusch7897ae92017-06-07 22:57:36 +02005# 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
nnoble097ef9b2014-12-01 17:06:10 -08008#
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009# http://www.apache.org/licenses/LICENSE-2.0
nnoble097ef9b2014-12-01 17:06:10 -080010#
Jan Tattermusch7897ae92017-06-07 22:57:36 +020011# 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.
nnoble097ef9b2014-12-01 17:06:10 -080016
nnoble097ef9b2014-12-01 17:06:10 -080017# Sample app that helps validate RpcServer without protobuf serialization.
18#
19# Usage: $ path/to/noproto_server.rb
20
21this_dir = File.expand_path(File.dirname(__FILE__))
22lib_dir = File.join(File.dirname(this_dir), 'lib')
23$LOAD_PATH.unshift(lib_dir) unless $LOAD_PATH.include?(lib_dir)
24
25require 'grpc'
temiola0f0a6bc2015-01-07 18:43:40 -080026require 'optparse'
nnoble097ef9b2014-12-01 17:06:10 -080027
Tim Emiolae2860c52015-01-16 02:58:41 -080028# a simple non-protobuf message class.
temiola0f0a6bc2015-01-07 18:43:40 -080029class NoProtoMsg
Tim Emiolae2860c52015-01-16 02:58:41 -080030 def self.marshal(_o)
nnoble097ef9b2014-12-01 17:06:10 -080031 ''
32 end
33
Tim Emiolae2860c52015-01-16 02:58:41 -080034 def self.unmarshal(_o)
temiola0f0a6bc2015-01-07 18:43:40 -080035 NoProtoMsg.new
nnoble097ef9b2014-12-01 17:06:10 -080036 end
37end
38
Tim Emiolae2860c52015-01-16 02:58:41 -080039# service the uses the non-protobuf message class.
temiola0f0a6bc2015-01-07 18:43:40 -080040class NoProtoService
nnoble097ef9b2014-12-01 17:06:10 -080041 include GRPC::GenericService
temiola0f0a6bc2015-01-07 18:43:40 -080042 rpc :AnRPC, NoProtoMsg, NoProtoMsg
nnoble097ef9b2014-12-01 17:06:10 -080043end
44
Tim Emiolae2860c52015-01-16 02:58:41 -080045# an implementation of the non-protobuf service.
temiola0f0a6bc2015-01-07 18:43:40 -080046class NoProto < NoProtoService
Tim Emiolae2860c52015-01-16 02:58:41 -080047 def initialize(_default_var = 'ignored')
nnoble097ef9b2014-12-01 17:06:10 -080048 end
49
Tim Emiolae2860c52015-01-16 02:58:41 -080050 def an_rpc(req, _call)
Nick Gauthierf233d962015-05-20 14:02:50 -040051 GRPC.logger.info('echo service received a request')
nnoble097ef9b2014-12-01 17:06:10 -080052 req
53 end
54end
55
temiola0f0a6bc2015-01-07 18:43:40 -080056def 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 }
61end
62
63def test_server_creds
64 certs = load_test_certs
Tim Emiola73a540a2015-08-28 18:56:17 -070065 GRPC::Core::ServerCredentials.new(
66 nil, [{ private_key: certs[1], cert_chain: certs[2] }], false)
temiola0f0a6bc2015-01-07 18:43:40 -080067end
68
nnoble097ef9b2014-12-01 17:06:10 -080069def main
temiola0f0a6bc2015-01-07 18:43:40 -080070 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 Emiolae2860c52015-01-16 02:58:41 -080080 options['secure'] = v
temiola0f0a6bc2015-01-07 18:43:40 -080081 end
82 end.parse!
83
Tim Emiola0ce8edc2015-03-05 15:17:30 -080084 s = GRPC::RpcServer.new
temiola0f0a6bc2015-01-07 18:43:40 -080085 if options['secure']
Tim Emiola0ce8edc2015-03-05 15:17:30 -080086 s.add_http2_port(options['host'], test_server_creds)
Nick Gauthierf233d962015-05-20 14:02:50 -040087 GRPC.logger.info("... running securely on #{options['host']}")
temiola0f0a6bc2015-01-07 18:43:40 -080088 else
Tim Emiolac03138a2015-09-24 13:11:03 -070089 s.add_http2_port(options['host'], :this_port_is_insecure)
Nick Gauthierf233d962015-05-20 14:02:50 -040090 GRPC.logger.info("... running insecurely on #{options['host']}")
temiola0f0a6bc2015-01-07 18:43:40 -080091 end
92
93 s.handle(NoProto)
Tim Emiola321871e2015-04-16 12:56:11 -070094 s.run_till_terminated
nnoble097ef9b2014-12-01 17:06:10 -080095end
96
Craig Tiller190d3602015-02-18 09:23:38 -080097main