prost-simple-rpc
Do you want to use type-safe Protobuf-based RPC without having to use something heavy-weight like gRPC?
This library lets you generate traits for implementing a generic RPC mechanism using Protobuf as the schema language. You have to supply your own underlying transport mechanism, for example WebSockets, UNIX pipes, HTTP, etc.
TODO
This library is quite complete but there are still a few things I would like to fix before a "1.0":
- Clean up the code generation code.
- Use unboxed futures in generated client code.
- Use unboxed futures in generated server code.
- Try to support execution errors that don't implement
failure::Fail
.
Usage
For the complete example, see the example directory.
Start by defining a schema for your service in e.g. src/schema/echo/service.proto
:
syntax = "proto3";
package echo;
// The Echo service. This service returns back the same data that it is given.
service Echo {
// Echoes back the data sent, unmodified.
rpc Echo (EchoRequest) returns (EchoResponse);
}
// The request for an `Echo.Echo` call.
message EchoRequest {
// The data to be echoed back.
bytes data = 1;
}
// The response for an `Echo.Echo` call.
message EchoResponse {
// The echoed back data from `EchoRequest.data`.
bytes data = 1;
}
Use prost
, prost-build
and prost-simple-rpc-build
to generate Rust code for this service, by
putting this in your build.rs
:
extern crate prost_build;
extern crate prost_simple_rpc_build;
Then, include the generated code in your Rust build, for example in main.rs
. There are a bunch of
extra crate dependencies for the generated code:
extern crate bytes;
extern crate failure;
extern crate futures;
extern crate prost;
extern crate prost_derive;
extern crate prost_simple_rpc;
extern crate tokio;
Client
Let's say you want to create a client for your service. You need to implement a Handler
that
handles the transport for your client calls. Let's imagine you have some form of WebSockets
transport:
You can now use this handler with the client generated by prost-simple-rpc
:
Server
To create a server for your service, start by implementing the generated service trait for the service:
;
;
You can now wrap this service with the generated server implementation to get something that can be plugged into your preferred routing system: