Dropshot
Dropshot is a general-purpose crate for exposing REST APIs from a Rust program. For more, see the online Dropshot documentation. You can build the documentation yourself with:
$ cargo +nightly doc
Contributing
You can build and run the whole test suite with cargo test
. The test
suite runs cleanly and should remain clean.
You can format the code using cargo fmt
. CI checks that code is correctly formatted.
Clippy is used to check for common code issues. (We disable the style checks.) You can run it with cargo clippy --all-targetsโโโ--deny warnings --allow clippy::style
. CI will run clippy as well.
For maintainers (e.g., publishing new releases and managing dependabot), see MAINTAINERS.adoc.
Configuration reference
Dropshot servers
Dropshot servers use a TOML configuration file. Supported config properties include:
Name | Example | Required? | Description |
---|---|---|---|
|
|
No |
Specifies that the server should bind to the given IP address and TCP port. In general, servers can bind to more than one IP address and port, but this is not (yet?) supported. Defaults to "127.0.0.1:0". |
|
|
No |
Specifies the maximum number of bytes allowed in a request body. Larger requests will receive a 400 error. Defaults to 1024. |
|
|
No |
Specifies if and how TLS certificate and key information is provided. Valid values include "AsFile" and "AsBytes". |
|
|
Only if |
Specifies the path to a PEM file containing a certificate chain for the server to identify itself with. The first certificate is the end-entity certificate, and the remaining are intermediate certificates on the way to a trusted CA. If specified, the server will only listen for TLS connections. |
|
|
Only if |
Specifies the path to a PEM-encoded PKCS #8 file containing the private key the server will use. If specified, the server will only listen for TLS connections. |
|
|
Only if |
Identical to |
|
|
Only if |
Identical to |
Logging
Dropshot provides a small wrapper to configure a slog-based Logger. You can use this without using the rest of Dropshot. Logging config properties include:
Name | Example | Required? | Description |
---|---|---|---|
|
|
Yes |
Controls where server logging will go. Valid modes are |
|
|
Yes |
Specifies what severity of log messages should be included in the log. Valid
values include |
|
|
Only if |
If |
|
|
Only if |
If |
Design notes
Why is there no way to add an API handler function that runs on every request?
In designing Dropshot, weโve tried to avoid a few problems we found with frameworks we used in the past. Many (most?) web frameworks, whether in Rust or another language, let you specify a chain of handlers for each route. You can usually specify some handlers that run before or after every request, regardless of the route. We found that after years of evolving a complex API server using this approach, it can get quite hard to follow the control flow for a particular request and to understand the implicit dependencies between different handlers within the chain. This made it time-consuming and error-prone to work on these API servers. (For more details, see the discussion in issue 58.)
With Dropshot, we wanted to try something different: if the primary purpose of these handlers is to share code between handlers, what if we rely instead on existing mechanismsโโโi.e., function calls. The big risk is that itโs easy for someone to accidentally forget some important function call, like the one that authenticates or authorizes a user. We havenโt gotten far enough in a complex implementation to need this yet, but the plan is to create a pattern of utility functions that return typed values. For example, where in Node.js you might add an early authentication handler that fills in request.auth
, with Dropshot youโd have an authentication function that returns an AuthzContext
struct. Then anything that needs authentication consumes the AuthzContext
as a function argument. As an author of a handler, you know if youโve got an AuthzContext
available and, if not, how to get one (call the utility function). This composes, too: you can have an authorization function that returns an AuthnContext
, and the utility function that returns one can consume the AuthzContext
. Then anything that requires authorization can consume just the AuthnContext
, and you know itโs been authenticated and authorized (possibly with details in that structure).
Itโs early, and we may find we need richer facilities in the framework. But weโre hopeful this approach will make it faster and smoother to iterate on complex API servers. If you pick up Dropshot and try this out, let us know how it goes!
Examples
To run the examples in dropshot/examples, clone the repository and run cargo run --example [example_name]
, e.g. cargo run --example basic
. (Do not include the file extension.)