Twurst is an implementation of Twirp v7 in Rust
on top of prost
, the tower
ecosystem and axum
.
It fully supports JSON and its server can also serve regular gRPC.
Twirp is a protocol similar to gRPC but with some differences:
- Based on protobuf and gRPC
service
definitions - Uses plain HTTP 1.1 as a wire format (with upgrade to HTTP 2/3 if the client and server agree)
- No stream requests/responses
- Protobuf JSON format is a first class citizen and can be used instead of protobuf binary encoding
- Errors are encoded as a JSON payload and correct HTTP status code must be used
Useful properties:
- Same protocol level behaviors as web browsers and tools like
curl
orreqwest
just work - JSON if text based human-readable communications are required
- Same definition files as gRPC, fairly simple migration coming from gRPC
Twirp has been created by Twitch and its original implementation is in Go. A list of other implementations is provided.
This is a simple implementation of Twirp.
Its aim is to be a replacement of tonic
using Twirp instead of gRPC.
It is also based on prost
with the help of prost-reflect
for JSON support.
It is split into multiple crates:
twurst-build
is the equivalent oftonic-build
for Twirp: it generates code from the.proto
files.twurst-client
wraps areqwest::Client
or anytower::Service
and provides the needed code for the*Client
structs generated bytwurst-build
to work.twurst-server
implements a Twirp server on top ofaxum
from an implementation of the service trait generated bytwurst-build
. It is not a fully fledged server but only anaxum::Router
that can be integrated into a bigger HTTP server (remind, Twirp uses regular HTTP(S)).twurst-error
provides theTwirpError
type (thinktonic::Status
but for Twirp). It is reexported by the other crates, and you should not need to depend on it directly.
Client and server examples are provided in the example
directory.
example/js-client
provides an example of a naive JS client.
Support for gRPC is also provided behind the grpc
feature in twurst-build
and twurst-server
.
It allows to easily serve both Twirp and gRPC.
example/server
provides an example.
For more detailed documentation see the client and the server READMEs.
- Read the
example/client
andexample/server
directories. - Copy/paste and adapt the code if you want to build a server or a client.
- Read the client and/or the server READMEs for more documentation.
prost_twirp
does not support JSON, Twirp 7 and last commit was on January 2023.twirp-rs
does not allow any TowerService
in the client, has a more restricted JSON support based onprost-wkt
and not onprost-reflect
leading to eg. troubles withoneOf
support or does not support custom fields based on Axum extractors added to the server generated methods.
Copyright 2024 Helsing GmbH
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.