8 breaking releases

0.10.0 Feb 6, 2024
0.9.0 Jan 20, 2023
0.8.0 Sep 9, 2022
0.7.0 May 6, 2022
0.1.0 Jun 25, 2020

#1352 in Procedural macros

Download history 134/week @ 2024-01-03 122/week @ 2024-01-10 61/week @ 2024-01-17 76/week @ 2024-01-24 108/week @ 2024-01-31 320/week @ 2024-02-07 241/week @ 2024-02-14 516/week @ 2024-02-21 411/week @ 2024-02-28 600/week @ 2024-03-06 415/week @ 2024-03-13 765/week @ 2024-03-20 948/week @ 2024-03-27 1082/week @ 2024-04-03 1161/week @ 2024-04-10 1058/week @ 2024-04-17

4,470 downloads per month
Used in 6 crates (via dropshot)

Apache-2.0

59KB
1.5K SLoC

:showtitle: :toc: left :icons: font

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:

[source,text]

$ 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 link:./MAINTAINERS.adocMAINTAINERS.adoc.

Configuration reference

Dropshot servers

Dropshot servers use a TOML configuration file. Supported config properties include:

[cols="1,1,1,3",options="header"] |=== |Name |Example |Required? |Description

|bind_address |"127.0.0.1:12220" |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".

|request_body_max_bytes |4096 |No |Specifies the maximum number of bytes allowed in a request body. Larger requests will receive a 400 error. Defaults to 1024.

|tls.type |"AsFile" |No |Specifies if and how TLS certificate and key information is provided. Valid values include "AsFile" and "AsBytes".

|tls.cert_file |"/path/to/cert.pem" |Only if tls.type = AsFile |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.

|tls.key_file |"/path/to/key.pem" |Only if tls.type = AsFile |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.

|tls.certs |Vec<u8> of certificate data |Only if tls.type = AsBytes |Identical to tls.cert_file, but provided as a buffer.

|tls.key |Vec<u8> of key data |Only if tls.type = AsBytes |Identical to tls.key_file, but provided as a buffer. |===

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:

[cols="1,1,1,3",options="header"] |=== |Name |Example |Required? |Description

|mode |"file" |Yes |Controls where server logging will go. Valid modes are "stderr-terminal" and "file". If the mode is "stderr-terminal", human-readable output, with colors and other terminal formatting if possible, will be sent to stderr. If the mode is "file", Bunyan-format output will be sent to the filesystem path given by log.path. See also log.if_exists`, which controls the behavior if the destination path already exists.

|level |"info" |Yes |Specifies what severity of log messages should be included in the log. Valid values include "trace", "debug", "info", "warn", "error", and "critical", which are increasing order of severity. Log messages at the specified level and more severe levels will be included in the log.

|path |"logs/server.log" |Only if log.mode = "file" |If log.mode is "file", this property determines the path to the log file. See also log.if_exists.

|if_exists |"append" |Only if log.mode = "file" |If log.mode is "file", this property specifies what to do if the destination log file already exists. Valid values include "append" (which appends to the existing file), "truncate" (which truncates the existing file and then uses it as though it had just been created), and "fail" (which causes the server to exit immediately with an error).

|===

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.)


lib.rs:

This package defines macro attributes associated with HTTP handlers. These attributes are used both to define an HTTP API and to generate an OpenAPI Spec (OAS) v3 document that describes the API.

Dependencies

~0.5–1MB
~25K SLoC