Giter Site home page Giter Site logo

grpc's Introduction

GRPC

This project is a pure-Crystal implementation of gRPC.

Installation

  1. Add the dependency to your shard.yml:

    dependencies:
      grpc:
        github: jgaskins/grpc
  2. Run shards install

  3. Make sure you have Google's grpc tools installed

    • macOS: brew install grpc

Usage

  1. Write a protos/hello_world.proto file that contains a service entry and any message types it depends on:

    syntax = "proto3";
    
    service HelloWorld {
      rpc MethodName (TheRequest) returns (TheResponse) {}
    }
    
    message TheRequest {
      string text = 1;
    }
    
    message TheResponse {
      string data = 1;
    }
  2. Compile the .proto files. If your messages are defined in protos/hello_world.proto and you want your code written out to the app's src/protobufs directory, use the following command:

    $ protoc -I protos \
        --grpc_out=src/protobufs \
        --crystal_out=src/protobufs \
        --plugin=protoc-gen-grpc=bin/grpc_crystal \
        --plugin=protoc-gen-crystal=bin/protoc-gen-crystal \
        protos/hello_world.proto
    

    This compiles both the service and the messages.

Server

To handle gRPC requests for the above service definition, we need 3 things:

  • One or more service handlers
  • A gRPC server
  • An HTTP/2 server to wrap the gRPC server (gRPC runs on top of HTTP/2)

Service Handlers

Services are compiled into abstract classes with the protoc command above. To implement a handler for a given service, we subclass the generated abstract class, defining the methods with snake_cased names:

require "./protobufs/hello_world_services.pb"
require "./protobufs/hello_world.pb"

class HelloWorldHandler < HelloWorld
  # You can define your own initialize method to inject dependencies

  def method_name(request : TheRequest) : TheResponse
    TheResponse.new(data: "Hello #{request.text}")
  end
end

The methods defined in the service declaration are required to be implemented by this class. Your program will not compile without them.

gRPC Server

require "grpc"
grpc = GRPC::Server.new
grpc << HelloWorldHandler.new

You can add as many service handlers as you like.

HTTP/2 Server

The HTTP2::Server works similarly to HTTP::Server:

require "grpc/http2"
server = HTTP2::ClearTextServer.new([grpc]) # TLS isn't supported yet
server.listen "0.0.0.0", 50000

And now gRPC requests for your HelloWorld service will be handled by HelloWorldHandler.

Client

To write a client to consume the HelloWorld service, you simply use a Stub:

# Load the service and message definitions
require "./protobufs/hello_world_services.pb"
require "./protobufs/hello_world.pb"

HelloWorldService = HelloWorld::Stub.new("localhost", 50000)

# from anywhere in your app
pp HelloWorldService.method_name(TheRequest.new(text: "foo"))
# => TheResponse(@data="Hello foo")

Limitations

This implementation currently only supports "simple gRPC" โ€” send a synchronous request, get a synchronous response. Streaming is not yet implemented.

Roadmap

Development

TODO: Write development instructions here

Contributing

  1. Fork it (https://github.com/jgaskins/grpc/fork)
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

Contributors

grpc's People

Contributors

jgaskins avatar wonderix 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.