Coder Social home page Coder Social logo

twirp.cr's Introduction

twirp.cr

An implementation of Twirp in Crystal.

Installation

  1. Add the dependency to your shard.yml:

    dependencies:
      twirp:
        github: mloughran/twirp.cr
  2. Run shards install

Usage

To see an example proto definition, generator, server, and client, see the included example.

Protobuf generator

Includes a protoc plugin for generating Twirp server and client implementations from a protobuf service definition.

protoc --twirp_crystal_out=src --plugin=bin/protoc-gen-twirp_crystal example.proto

Typically you will also want to generate protobuf message definitions:

protoc --crystal_out=src --plugin=bin/protoc-gen-crystal example.proto

Twirp client

For example:

require "./example.twirp.cr"
require "./example.pb.cr"

client = ExampleService::Client.new("localhost", 8080)

req = HelloWorldRequest.new(name: "twirp")
resp = client.hello_world(req)
puts resp.greeting

I've elected to raise an error (which will be a subclass of Twirp::Error) if a Twirp error response is received, rather that take the ruby approach of returning a result object. Exceptions will also be raised by the HTTP::Client in case of transport errors.

Twirp server

Implement a handler (subclassing the generated abstract class):

class ExampleHandler < ExampleService
  def hello_world(req : HelloWorldRequest) : HelloWorldResponse
    HelloWorldResponse.new(greeting: "Hello #{req.name}")
  end
end

Pass this to a Twirp server (which is itself a HTTP::Handler):

require "twirp/server"

twirp_server = Twirp::Server.new(ExampleHandler.new)

Expose this via a HTTP::Server:

server = HTTP::Server.new(twirp_server)
address = server.bind_tcp 8080
puts "Listening on http://#{address}"
server.listen

To return a Twirp error, raise the appropriate Twirp::Error from your handler. For example:

class ExampleHandler < ExampleService
  def hello_world(req : HelloWorldRequest) : HelloWorldResponse
    raise Twirp::Error::Unauthenticated.new("Sorry!")
  end
end

Any other errors raised by handler code will be caught, logged, and an internal Twirp error will be returned to the client. Exceptions can be captured by passing an exception_handler proc to Twirp::Server.new.

Logging

Twirp::Server produces log output when handling handling requests. This can be customised, e.g.:

Twirp::Log.level = Log::Severity::Warn

Development

Install shards and build

shards install
shards build

Verify that example generates cleanly

cd example && make clean generate && cd -

Run server and client:

crystal run example/server.cr
crystal run example/client.cr

There are currently no specs, sorry.

Contributors

With thanks to https://github.com/jgaskins/grpc for the generator (which is more or less verbatim), and for inspiring the double-macro approach for the service DSL (since I'm still wrapping my head around crystal macros).

twirp.cr's People

Contributors

mloughran avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.