Crate implementing the needed runtime for the code generated by twurst-build
in order to run Twirp servers.
Getting started
To start you need first to have a gRPC .proto file (e.g. service.proto).
Then build your proto files by creating a build.rs file with:
and add to your Cargo.toml:
[]
= ""
= ""
= ""
= ""
= ""
[]
= ""
Note that protoc must be available, see prost-build documentation on this topic.
If you are using Nix, nix-shell -p protobuf is enough to provide protoc.
Then you can implement a Twirp server with:
use *;
use twirp_fallback;
/// The service implementation
async
Note that you can make use of tower or tower-http layers to customize the server:
use twirp_fallback;
async
It is also possible to use axum extractors in the generated code.
For example, to get access to the request headers you can tweak your build.rs TwirpBuilder::new() call:
new
.with_server
.with_axum_request_extractor
.compile_protos
then your trait implementation will change to:
Any type implementing FromRequestParts work.
Note that you can use Router::merge to serve multiple Twirp services:
use twirp_fallback;
ExampleServiceServicer
.into_router
.merge
.fallback
Note the single fallback call.
To make testing easier you can use the generated client code to test your server:
use TwirpHttpClient;
let client = new;
note that you need to add to your build.rs .with_client() alongside .with_server().
gRPC support
twurst-server has also basic gRPC support to serve easily both Twirp and gRPC.
For that enable the grpc feature of the twurst-server crate, then you can serve gRPC nearly like Twirp:
use grpc_fallback;
async
Router::merge still works if you want to serve multiple services.
Note that no limit is set on requests size, use RequestBodyLimit layer if you want to set one.
Cargo features
grpcthat provides gRPC support behindtonic
License
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.