Twirp services in Ruby
Перейти к файлу
Mario Izquierdo 851adf93bc fix error test 2018-02-21 17:23:57 -08:00
example Add before hooks to the service. Refactor some more code. Add simple test for one hook 2018-02-21 16:13:19 -08:00
lib Add before hooks to the service. Refactor some more code. Add simple test for one hook 2018-02-21 16:13:19 -08:00
protoc-gen-twirp_ruby Add before hooks to the service. Refactor some more code. Add simple test for one hook 2018-02-21 16:13:19 -08:00
test fix error test 2018-02-21 17:23:57 -08:00
.gitignore rename Twirp::Server to Twirp::Service; v0.0.2 2018-02-05 21:29:32 -08:00
Gemfile Edit gemspec and add initial files 2018-02-04 18:26:55 -08:00
Gemfile.lock run bundle install and make sure it works 2018-02-05 00:30:53 -08:00
LICENSE add LICENSE 2018-02-06 14:49:06 -08:00
README.md pr comments v2 2018-02-16 12:55:36 -08:00
twirp.gemspec run bundle install and make sure it works 2018-02-05 00:30:53 -08:00

README.md

Ruby Twirp

Twirp services and clients in Ruby.

Installation

Install the twirp gem:

➜ gem install twirp

Use go get to install the ruby_twirp protoc plugin:

➜ go get github.com/cyrusaf/ruby-twirp/protoc-gen-twirp_ruby

You will also need:

  • protoc, the protobuf compiler. You need version 3+.

Haberdasher Example

See the example/ folder for the final product.

First create a basic .proto file:

// haberdasher.proto
syntax = "proto3";
package example;

service Haberdasher {
    rpc HelloWorld(HelloWorldRequest) returns (HelloWorldResponse);
}

message HelloWorldRequest {
    string name = 1;
}

message HelloWorldResponse {
    string message = 1;
}

Run the protoc binary to generate gen/haberdasher_pb.rb and gen/haberdasher_twirp.rb.

➜ protoc --proto_path=. ./haberdasher.proto --ruby_out=gen --twirp_ruby_out=gen

Write an implementation of our haberdasher service and attach to a rack server:

# main.rb
require 'rack'
require_relative 'gen/haberdasher_pb.rb'
require_relative 'gen/haberdasher_twirp.rb'

class HaberdasherHandler
    def hello_world(req)
        return {message: "Hello #{req.name}"}
    end
end

handler = HaberdasherHandler.new()
service = Example::HaberdasherService.new(handler)
Rack::Handler::WEBrick.run service

You can also mount onto a rails service:

App::Application.routes.draw do
  handler = HaberdasherHandler.new()
  service = Example::HaberdasherService.new(handler)
  mount service, at: HaberdasherService::PATH_PREFIX
end

Run ruby main.rb to start the server on port 8080:

➜ ruby main.rb

curl your server to get a response:

➜ curl --request POST \
  --url http://localhost:8080/twirp/examples.Haberdasher/HelloWorld \
  --header 'content-type: application/json' \
  --data '{
	"name": "World"
  }'