Skip to content

About

This repository contains the sample code for Rainer Stropek's talk at Eurorust 2022.

Topics

Resources

Stars

105 stars

Watchers

3 watching

Forks

Repository files navigation

Building Web APIs with Rust

This repository compares Rust web frameworks by implementing the same small API in each of them. A larger sample then shows what a more realistic API looks like, including a database, validation, error handling, and tests.

The samples started out as the demo code for Rainer Stropek's talk Building Web APIs with Rust - State of the Union at Eurorust 2022 and BASTA 2023 (slides). The code has since been updated to current Rust and framework versions, so details may differ from the slides.

Samples

Sample Framework What to look for
todo-axum Axum Macro-free routing, extractors, and Tower middleware
todo-actix-web Actix Web Routing with and without macros, responders, and ResponseError
todo-rocket Rocket Attribute macros, request guards, and derived responders
todo-warp Warp Composable filters and rejections
hero-manager-axum Axum and SQLx Postgres access, validation, problem details, graceful shutdown, and handler tests with mocks

The four todo samples expose the same API and share their business logic through todo-logic, so the code in each sample is only what is specific to its framework.

Prerequisites

  • Rust 1.94 or later
  • just (optional, for the commands below)
  • Docker and sqlx-cli for hero-manager-axum

Run a todo sample

Pick axum, actix-web, rocket, or warp:

just run axum

Each todo sample listens on port 3000 and keeps its data in memory. requests.http contains example requests for the REST Client extension for VS Code.

All samples log with tracing. Set RUST_LOG to change the log filter:

RUST_LOG=debug just run warp

Run the hero manager

Run these commands in the hero-manager-axum folder:

just start-pg
just create-db
just apply-migrations
just run

The API listens on port 4000. The connection string comes from .env, and requests.http contains example requests. The credentials in .env are for the local demo database only.

Check the code

just check
just test

The tests do not need a database because the handlers are tested against a mocked repository.

About

This repository contains the sample code for Rainer Stropek's talk at Eurorust 2022.

Topics

Resources

Stars

105 stars

Watchers

3 watching

Forks

Used by

Contributors

Languages