Oops, I Did It Again...I Made A Rust Web API And It Was Not That Difficult
Over two years ago (oof), I posted a walkthrough of my Rust implementation of todo-mvp by @gypsydave5 demonstrating how to build a simple Rust API without a framework. The core functionality was built using hyper, a lower-level HTTP library instead of a full-blown framework.
It turns out I wrote that post about six months too early. I published it in May 2019, and in November, Rust released 1.39.0 containing async/await syntax. Womp womp.
My intent here was to simply upgrade the existing application to use the new syntax where applicable and call it a day. But, you know that thing that happens when you go back and review code you wrote years ago? Yeah, that thing happened. We’re starting from scratch.
Our result functions almost identically to the implementation in the previous post, so some of this code will look very similar. Here’s a quick overview of the new stuff you’ll find in this post not present in the previous version:
- anyhow - Error handling for humans.
- async/await - New syntax for expressing asynchronous computation - like a webserver!
- catch_unwind - A panicking task shouldn’t crash your whole server! Gracefully catch the panic and keep on servin’.
- compression - Every response will be DEFLATE compressed, simply because we can.
- Rust 2021 - the future of Rust.
- state management - Instead of a global variable, we’ll use protocol extensions to access the app state.
- tracing - The log crate is old news. All the cool kids use tracing now.
- unit testing - We didn’t have any last time - tsk tsk! Learn how to write async unit tests for your handlers.
To follow along, you’ll need a stable Rust toolchain. See the install page for instructions to install rustup for your platform. You should prefer this method to your distribution’s package manager. If you’re a NixOS freak, I recommend fenix.
Once your environment is set up, start a new project and make sure you can build it:
Open up your Cargo.toml and make it look like this:
There are some new elements here already, up in the package section. Rust 2021 was recently released, and along with it, the rust-version metadata key, allowing you to specify the Minimum Supported Rust Version (MSRV) directly in the package.
This post is only concerned with Rust and will use assets identical to the last post. Create a folder called templates at the project’s top-level, and place this index.html inside. You will also need to create a directory at src/resource and fill it with these files. There is a stylesheet and a handful of SVG files. Your structure should look like this:
Good to go!
This whole app will live in src/main.rs. Start off by adding the imports:
It’s nice to allow the user to specify where to run their app. The clap library provides a convenient way to specify command-line arguments in a struct. This feature is still in beta but will stabilize soon. We can create a struct and use the Parser feature to generate the options:
When run without any arguments, the server will bind to 0.0.0.0:3000, a reasonable default. This allows users to either use the ADDRESS and PORT environment variables or -a/--address and -p/--port command-line arguments. It also provides a nice --help implementation:
Our main() function will just parse these arguments and pass them off to our app routine:
I’ve brought anyhow::Result into scope, making error handling super easy to use. We don’t need to specify all our Error types. It can automatically convert any errors that implement std::error::Error, which should be all of them. If an error propagates all the way up to main(), we’ll get all the info it’s captured printed to stdout.
The app() function is our real entrypoint:
This function is tagged with #[tokio::main], meaning it will execute an async runtime. All the functions we’re using now can be marked async, meaning we can await the result. Under the hood, Rust converts these functions into constructs called Futures, and in the previous iteration we built these manually. Futures can either be Ready or Waiting. You can call the poll() method on a future, which just asks: “are you ready, or are you waiting?” If the future is ready to resolve, it will pass up the response, and if it’s waiting, it will just respond with Pending. It can return a wake() callback, letting the caller know there’s a value. When wake is executed, the caller will know to poll() this future again, resolving it to a value.
This all involved a lot of ceremony. This async/.await syntax abstracts these concepts for us. We just write functions like normal, except we mark them async, and when control flow hits a point where we need to wait for a result to resolve, we can use await. If these are used within an executor like #[tokio::main], the runtime will cover all the details. Instead of blocking control flow, each of these await points will yield to other running tasks on this executor until the underlying Future returns from a poll() request as Ready<T>. This makes our types easier to reason about and our code much more straightforward to write.
This top-level function reads our argument struct to build the SocketAddr to bind to, starts up the logging system with tracing_subscriber, and builds our state management. Then, we call the serve() async function and await on the result. This executor will run forever until the user kills the process and can handle multiple concurrent connections for us seamlessly.
For a sneak preview, this is the signature of serve():
It requires we pass an address, then an Arc<C>. That C type is our app state, and for this to work, it must implement the Send and Sync traits. Send means the value can be sent to another thread, and Sync means it can be shared between threads simultaneously.
In our case, we don’t need to think too hard about this. We’re using an Arc<RwLock<T>> to allow mutation to provide a type that can be accessed from multiple tasks and safely mutated as needed. As long as each task grabs the proper lock, we don’t need to worry about concurrent tass clobbering each other. Only one task will be able to write to this type at a time, so every new reader will always have the correct data.
Finally, we need to add a handler with type H. These types start to peel back a little of what async is doing for us. Stripping out the Send + Sync trait bounds, this function satisfies the trait bound Fn(Request) -> Future<Output = Response>. Because we’re in an async environment, we can just write async fn handle(request: Request) -> Response - it’s an asynchronous function from a request to a response. Sounds like a web server to me! Using Rust’s async/.await, we get to simply write what we mean.
We’ll come back to the handler shortly - first, we have a little bit of setup to take care of.
This application only consists of a single page that will be refreshed whenever the state changes. We placed markup at templates/index.html, an HTML file using Jinja-style templating. We’ll use Tera to handle this in Rust.
The templates need to be compiled before use, but this only needs to happen once. We can use lazy_static to ensure this compilation happens the first time the templates are accessed, and then reuse the compiled result for all subsequent access:
Now we can use the TERA global. If Tera could not compile the templates for any reason, the process will exit here and display the error. Our server only works if this step completes successfully.
Next, we need to define the app state. The application revolves around a Todo type with a name, an ID, and a boolean tracking whether it’s been completed:
We just need to provide a string name, like Todos::new("Task"), and this type will generate a new unique ID and set it to incomplete.
Storage is pretty simple:
We need methods to add new todos, remove existing todos, and toggle the done boolean:
Notably, we don’t have to write any unique code to make this thread safe. We can write it exactly as we would in a single-threaded, synchronous context and trust that Rust won’t let us mutably access this unsafely. This is what got instantiated up in app() with these lines:
Wrapping this all in an Arc means that any task accessing this value can get their own reference to it, and the RwLock will allow multiple concurrent readers or exactly one writer at a time. When the lock is released, the next waiting task will be able to take control.
We’re finally ready to take a look at the handler. Per the signature up above, we know we need a function with the following signature: async fn handle(request: Request) -> Response. Each time our web server receives an HTTP request, it will call this function to produce a Response to send back. First, add type aliases for the request and response:
The hyper crate defines all the types we need. In both cases, the request and response will carry a hyper::Body. Every single handler will use this type signature, so defining these aliases saves us a lot of typing.
The request contains information about how it was sent. We can use Rust’s match construct to read both the incoming URI and HTTP method to correctly dispatch a response. Here’s the whole body:
Each match arm will match with a specific combination of HTTP verb and path. For example, GET /static/todo.css will properly dispatch the stylesheet() handler, but POST /static/todo.css is not supported and will fall through to four_oh_four(). Each of these handlers is itself an async function, but we don’t want to return up to the caller until they’ve been polled as ready, and return an actual Response. Remember, Rust is doing this for us - when we write async fn() -> Response, we actually get a Fn() -> impl Future<Output = Response>. We can’t use that return type until the future resolves! That’s what the .await syntax signifies. Once the future is ready, we’ll use the resulting Response output but not before.
The most straightforward handler is four_oh_four():
NOT FOUND!", http::StatusCode::NOT_FOUND).await }
This response doesn’t depend on the request - the request didn’t make any sense to us, after all! There’s no input parameter, but like all handlers, it produces a Response. Because all of our routes need to build responses, I pulled this logic out into a series of building-block functions.
Most of our Response building shares a lot of logic. We usually send back some form of string, and we want to attach a content type and a status code. Because we care about our user’s bandwidth usage, we also want to compress our responses, ensuring as little as possible is sent over the wire. The most common case is a successful response containing HTML:
This, in turn, calls the HTML string handler:
Our four_oh_four() handler used this directly to include a different status code. Ultimately, though, it’s all just strings:
These helpers allow for other content types as long as the body is still passed as a string. This covers all the body types we need for this application. At the bottom, we get to bytes_handler:
This function takes a byte slice (&[u8]), and DEFLATE compresses it. It adds the proper Content-Encoding header so that any connected client can uncompress the payload before presenting it back to the user. This is a rapid operation, and the smaller your payloads, the better. Every response that contains a body will eventually pass through this function before bubbling back up to the top-level handle() function and back to the client.
There’s one more response type for this application, and this doesn’t use a body at all. Whenever our app state changes, we will use the 301 HTTP status code to trigger the client to redirect back to the index. Every time the index is rendered, it reads the current app state, meaning any mutation executed in the handler will be automatically reflected on the refresh. This function calls Response::builder() directly:
Now, we have everything we need to render the app. Our index function is the first handler that requires state:
In the previous iteration of this app, the Todos struct was instantiated alongside our TERA templates in a global static variable. A better solution is to thread it through to our handler using the Request itself. We’ll look at the implementation lower down, but by the time we get here, there’s a fresh Arc containing the context ready to be read. We can use request.extensions().get() and Arc::clone() to grab our very own reference to the app state to use for building this response. The request extensions use the type of what’s stored for access, so we need to explicitly add the type of todos_ctx to indicate what we’re looking for.
Next, we build the index page using the current state of the app. This handler won’t perform any mutation, so we can use todos_ctx.read(). By introducing a child scope, we ensure our read lock gets dropped when we’re done with it, allowing any writers waiting for access to grab their own locks. If we needed to wait, no problem! We’re in an async function, the caller can poll us any time, and we’ll just return Pending until we’re ready to go. Nice and neat.
Once we’ve received our handle to the app state, we can pass it through to Tera. TERA.render() will return an HTML string with all template values resolved using our app state. Then, we can use our trusty ok_html_handler() response builder to tag it with a proper content type and status code and compress the result before returning to the caller.
The index.html template will request a style sheet when it loads from /static/todo.css. That’s a pretty simple handler:
The include_str!() macro actually bundles the string content directly in your compiled binary. These files need to be present at compile-time but do not need to be distributed in production. The compiled binary already includes everything it needs.
All the image assets in this application are SVG, which is represented as XML. This means we just need to read these strings and pass the proper content-type:
I just used a catch-all - any GET request that’s not for the index or the stylesheet is assumed to be for an image. At the top, there’s a little extra logic to make sure we’re looking for an image file. If there’s no extension, i.e. /nonsense, this handler will dispatch a four_oh_four(). Otherwise, we press forward and try to find the actual SVG file. If we succeed, we just pass the string back, and if not, we also four_oh_four().
The remaining handlers are involved with mutating the state. All of them will pass a request body with an item. For a new todo, it will be item=Task, and for toggling or removing, it will hold the id: item=e2104f6a-624d-498f-a553-29e559e78d33. In either case, we just need to extract the value after the equals sign:
The body may come in chunks, so we use hyper::body::to_bytes() to produce a single Bytes value with everything concatenated. We can then convert the bytes to a UTF-8 string and split on the = to grab the actual payload. All of our state mutation handlers call this function on the incoming request:
Each handler grabs its own unique reference to the app state, then extracts the payload. Like we did in index(), we open a new scope to interact with the RwLock mutex, and in this case, we use todos_ctx.write() to request a mutable lock. This blocks all other tasks until the mutation is complete. Then, we just redirect_home(). This prompts the client to send a GET / request, which leads our to-level handler to call index(), which reads the newly-mutated app state to build the page.
Groovy! That’s a full-fledged TODO app.
There’s one missing piece. We defined our handle() function, but we haven’t talked about serve() beyond the type signature. This one is pretty beefy:
I know, I know. There’s a lot here. The meat of this function happens right at the end:
We build a hyper::Server, bind it to the address we constructed from the Args struct, and serve this service thing. The service is built right above that:
We also wrap the handler function in an Arc. Our context is already wrapped, so we clone both to get a local reference within this closure. This allows the executor to spin up multiple concurrent versions of our handler service that all have access to the same state and logic.
The beefy stuff happens in this service() closure above. This is where we take an incoming request and match it up with our handler and context. A new instance is executed for each incoming request, and all this ceremony allows this to happen without interfering with other simultaneous requests.
For one, this is where we add our context to the request:
When we call request.extensions().get() in our mutating handlers, we’re pulling out the context added at this stage.
There’s also some logging added. We trace the request’s specifics and start a timer that reports how long the request took. To see this logging in action, set the environment variable RUST_LOG=info when executing the server process.
The most exciting part (to me, at least) is the panic handler. We always want our requests to succeed. However, there are situations where we may encounter a panic. This will cause the entire Rust program to crash and print out a stack trace in normal usage. However, this is a web service. A panic situation in one request handling process shouldn’t prevent other requests from executing. We don’t want the whole server to crash; we still want to handle these situations gracefully. We can intercept the normal panic behavior and instead simply produce a different response containing the details.
At the top, we create a task-local storage location:
This is local to just the currently executing Tokio task, not the whole program. Then, we replace the default panic logic with our own:
First, we grab the existing hook and store it to the hook variable. Then, we overwrite our own. At the end of the function, we make sure to reset the global panic hook back to what it was. If the task panics inside this function - for example, one of our unwrap() statements encounter a non-success, we’ll store the panic message and backtrace to this task-local location. However, we will not abort the process.
Up above, in the service location, we can catch this happening:
We attempt to build the response, but this result will not be a success if anything happens. If it was a success, great, we pass it back up. However, if we find an error value here, we can dispatch different logic:
For most requests, that result.unwrap() went fine, and we just stored our response to Response. However, if it was an error, we can read the result of the panic from this task-local area. We emit an error trace on the server-side and then build a new response with status code INTERNAL_SERVER_EROR. This response includes the full backtrace as the body. This means our server can keep handling other requests without interruption, but the specific client that caused the panic gets a complete log of the problem, and our server has logged the backtrace as well. We can diagnose the issue without losing uptime for any other client.
Now, no matter what happened while processing the request, we’ve stored a valid hyper::Response to this response value, and we can pass that back to the caller even if something catastrophic occurs. We can safely use Ok::<_, Infallible>, signifying that there is no possible way for control to fail to hit this point. Our server will always generate a response and continue, even if something terrible happens. Good stuff.
Finally, we want to ensure we can automate tests. I’ll just demonstrate a test of our 404 handler, which includes all the pieces needed to build a robust test suite:
Tokio provides a #[tokio::test] macro for building async tests. We can use hyper::Request to construct a request and build our context the same way we did in the server. Because our handler is just a function from a Request to a Response, we can test it very simply: let response = handle(request).await;.
We first assert the status code matches what we expect, then we have to decode the body. We use the ZlibDecoder to read the response body and decompress it back to a string.
Once we have our string response, we can use the select.rs library to ensure the structure matches our intent. In this case, we are asserting we’ve received an h1 element with a text body matching the string NOT FOUND!.
This implementation is an improvement over the previous in several fundamental ways. The async/.await syntax allows us to write code closely matching our intent without getting bogged down with Boxes and Futures. We avoid polluting the global scope and use the Request itself to handle concurrent access to the app state and even catastrophic request handling errors gracefully without affecting other clients. Our handler is straightforward to test. This application provides a solid, performant base for building more complex applications and keeps the dependencies and therefore compile times and bundle sizes to a minimum.
While there are many different frameworks for building web applications in Rust, it’s worth asking yourself whether you actually need that level of abstraction. For many web service needs, putting the building blocks together yourself isn’t much more complicated, and you retain control over how you build your application. If we want to deconstruct the request URI, we can do that already. If we return JSON, we just need to create a struct that implements serde::Serialize.
The conclusion here is the same as before: when the end goal is simple enough, why not use simple tools?