Adds a helloworld sample in ruby.
This commit is contained in:
parent
a16a4d551f
commit
49f3ccdd01
|
|
@ -0,0 +1,15 @@
|
|||
/.bundle/
|
||||
/.yardoc
|
||||
/Gemfile.lock
|
||||
/_yardoc/
|
||||
/coverage/
|
||||
/doc/
|
||||
/pkg/
|
||||
/spec/reports/
|
||||
/tmp/
|
||||
*.bundle
|
||||
*.so
|
||||
*.o
|
||||
*.a
|
||||
mkmf.log
|
||||
vendor
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
# -*- ruby -*-
|
||||
# encoding: utf-8
|
||||
|
||||
source 'https://rubygems.org/'
|
||||
|
||||
# Update this to reflect the local installation of gRPC.
|
||||
gem 'grpc', path: '/usr/local/google/repos/grpc/src/ruby'
|
||||
|
||||
# TODO: fix this if/when the gRPC repo structure changes.
|
||||
#
|
||||
# At the moment it's not possible to use grpc Ruby via git/github reference
|
||||
#
|
||||
# git 'git@github.com:grpc/grpc.git' do
|
||||
# gem 'grpc'
|
||||
# end
|
||||
|
|
@ -0,0 +1,35 @@
|
|||
gRPC Ruby Helloworld
|
||||
====================
|
||||
|
||||
INSTALLATION PREREQUISITES
|
||||
--------------------------
|
||||
|
||||
This requires Ruby 2.x, as the gRPC API surface uses keyword args.
|
||||
|
||||
INSTALL
|
||||
-------
|
||||
|
||||
- Clone this repository.
|
||||
- Follow the instructions in [INSTALL](https://github.com/grpc/grpc/blob/master/INSTALL) to install the gRPC C core.
|
||||
- *Temporary* Install gRPC for Ruby from source on your local machine and update path: to refer to it [Gemfile].
|
||||
- this is needed until the gRPC ruby gem is published
|
||||
- Use bundler to install
|
||||
```sh
|
||||
$ # from this directory
|
||||
$ gem install bundler && bundle install
|
||||
```
|
||||
|
||||
USAGE
|
||||
-----
|
||||
|
||||
- Run the server
|
||||
```sh
|
||||
$ # from this directory
|
||||
$ bundle exec ./greeter_server.rb &
|
||||
```
|
||||
|
||||
- Run the client
|
||||
```sh
|
||||
$ # from this directory
|
||||
$ bundle exec ./greeter_client.rb
|
||||
```
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
# -*- ruby -*-
|
||||
# encoding: utf-8
|
||||
|
||||
Gem::Specification.new do |s|
|
||||
s.name = 'grpc'
|
||||
s.version = '0.0.0'
|
||||
s.authors = ['gRPC Authors']
|
||||
s.email = 'temiola@google.com'
|
||||
s.homepage = 'https://github.com/google/grpc-common'
|
||||
s.summary = 'gRPC Ruby overview sample'
|
||||
s.description = 'Demonstrates how'
|
||||
|
||||
s.files = `git ls-files -- ruby/*`.split("\n")
|
||||
s.executables = `git ls-files -- ruby/greeter*.rb`.split("\n").map do |f|
|
||||
File.basename(f)
|
||||
end
|
||||
s.require_paths = ['lib']
|
||||
s.platform = Gem::Platform::RUBY
|
||||
|
||||
s.add_dependency 'grpc', '~> 0.0.1'
|
||||
|
||||
s.add_development_dependency 'bundler', '~> 1.7'
|
||||
end
|
||||
|
|
@ -0,0 +1,50 @@
|
|||
#!/usr/bin/env ruby
|
||||
|
||||
# Copyright 2015, Google Inc.
|
||||
# All rights reserved.
|
||||
#
|
||||
# Redistribution and use in source and binary forms, with or without
|
||||
# modification, are permitted provided that the following conditions are
|
||||
# met:
|
||||
#
|
||||
# * Redistributions of source code must retain the above copyright
|
||||
# notice, this list of conditions and the following disclaimer.
|
||||
# * Redistributions in binary form must reproduce the above
|
||||
# copyright notice, this list of conditions and the following disclaimer
|
||||
# in the documentation and/or other materials provided with the
|
||||
# distribution.
|
||||
# * Neither the name of Google Inc. nor the names of its
|
||||
# contributors may be used to endorse or promote products derived from
|
||||
# this software without specific prior written permission.
|
||||
#
|
||||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
# Sample app that connects to a Greeter service.
|
||||
#
|
||||
# Usage: $ path/to/greeter_client.rb
|
||||
|
||||
this_dir = File.expand_path(File.dirname(__FILE__))
|
||||
lib_dir = File.join(this_dir, 'lib')
|
||||
$LOAD_PATH.unshift(lib_dir) unless $LOAD_PATH.include?(lib_dir)
|
||||
|
||||
require 'grpc'
|
||||
require 'helloworld_services'
|
||||
|
||||
def main
|
||||
stub = Helloworld::Greeter::Stub.new('localhost:50051')
|
||||
user = ARGV.size > 0 ? ARGV[0] : 'world'
|
||||
message = stub.say_hello(Helloworld::HelloRequest.new(name: user)).message
|
||||
p "Greeting: #{message}"
|
||||
end
|
||||
|
||||
main
|
||||
|
|
@ -0,0 +1,60 @@
|
|||
#!/usr/bin/env ruby
|
||||
|
||||
# Copyright 2015, Google Inc.
|
||||
# All rights reserved.
|
||||
#
|
||||
# Redistribution and use in source and binary forms, with or without
|
||||
# modification, are permitted provided that the following conditions are
|
||||
# met:
|
||||
#
|
||||
# * Redistributions of source code must retain the above copyright
|
||||
# notice, this list of conditions and the following disclaimer.
|
||||
# * Redistributions in binary form must reproduce the above
|
||||
# copyright notice, this list of conditions and the following disclaimer
|
||||
# in the documentation and/or other materials provided with the
|
||||
# distribution.
|
||||
# * Neither the name of Google Inc. nor the names of its
|
||||
# contributors may be used to endorse or promote products derived from
|
||||
# this software without specific prior written permission.
|
||||
#
|
||||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
# Sample gRPC server that implements the Greeter::Helloworld service.
|
||||
#
|
||||
# Usage: $ path/to/greeter_server.rb
|
||||
|
||||
this_dir = File.expand_path(File.dirname(__FILE__))
|
||||
lib_dir = File.join(this_dir, 'lib')
|
||||
$LOAD_PATH.unshift(lib_dir) unless $LOAD_PATH.include?(lib_dir)
|
||||
|
||||
require 'grpc'
|
||||
require 'helloworld_services'
|
||||
|
||||
# GreeterServer is simple server that implements the Helloworld Greeter server.
|
||||
class GreeterServer < Helloworld::Greeter::Service
|
||||
# say_hello implements the sayHello rpc method.
|
||||
def say_hello(hello_req, _unused_call)
|
||||
Helloworld::HelloReply.new(message: "Hello #{hello_req.name}")
|
||||
end
|
||||
end
|
||||
|
||||
# main starts an RpcServer that receives requests to GreeterServer at the sample
|
||||
# server port.
|
||||
def main
|
||||
s = GRPC::RpcServer.new
|
||||
s.add_http2_port('0.0.0.0:50051')
|
||||
s.handle(GreeterServer)
|
||||
s.run
|
||||
end
|
||||
|
||||
main
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
# Generated by the protocol buffer compiler. DO NOT EDIT!
|
||||
# source: helloworld.proto
|
||||
|
||||
require 'google/protobuf'
|
||||
|
||||
Google::Protobuf::DescriptorPool.generated_pool.build do
|
||||
add_message "helloworld.HelloRequest" do
|
||||
optional :name, :string, 1
|
||||
end
|
||||
add_message "helloworld.HelloReply" do
|
||||
optional :message, :string, 1
|
||||
end
|
||||
end
|
||||
|
||||
module Helloworld
|
||||
HelloRequest = Google::Protobuf::DescriptorPool.generated_pool.lookup("helloworld.HelloRequest").msgclass
|
||||
HelloReply = Google::Protobuf::DescriptorPool.generated_pool.lookup("helloworld.HelloReply").msgclass
|
||||
end
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
# Generated by the protocol buffer compiler. DO NOT EDIT!
|
||||
# Source: helloworld.proto for package 'helloworld'
|
||||
|
||||
require 'grpc'
|
||||
require 'helloworld'
|
||||
|
||||
module Helloworld
|
||||
module Greeter
|
||||
|
||||
# TODO: add proto service documentation here
|
||||
class Service
|
||||
|
||||
include GRPC::GenericService
|
||||
|
||||
self.marshal_class_method = :encode
|
||||
self.unmarshal_class_method = :decode
|
||||
self.service_name = 'helloworld.Greeter'
|
||||
|
||||
rpc :sayHello, HelloRequest, HelloReply
|
||||
end
|
||||
|
||||
Stub = Service.rpc_stub_class
|
||||
end
|
||||
end
|
||||
Loading…
Reference in New Issue