liveBook · Manning

What you will learn in Rust Web Development

With warp, tokio, and reqwest

by Bastian Gruber

Chapter by chapter

Chapter 1: Why Rust?

This introduction presents Rust’s performance, safety, tooling, and versatility as reasons to use it for web development and other systems.

1.1 Batteries included: Rust’s tooling · 1.2 The Rust compiler · 1.3 Rust for web services · 1.4 Maintainability of Rust applications · 1.5 Summary

What you learn in chapter 1 →

Chapter 2: Laying the foundation

This introduction frames the chapter’s Rust foundations and their value for building adaptable web services.

2.1 Following the Rust playbook · 2.1.1 Modeling your resources with structs · 2.1.2 Understanding options · 2.1.3 Using documentation to solve errors · 2.1.4 Handling strings in Rust · 2.1.5 Taking an excursion into moving, borrowing, and ownership · 2.1.6 Using and implementing traits · 2.1.7 Handling results · 2.2 Creating our web server · 2.2.1 Handling multiple requests at once · 2.2.2 Rust’s asynchronous environment · 2.2.3 Rust’s handling of async/await · 2.2.4 Using Rust’s Future type · 2.2.5 Choosing a runtime · 2.2.6 Choosing a web framework · 2.3 Summary

What you learn in chapter 2 →

Chapter 3: Create your first route handler

The introduction establishes Warp’s web-service stack and the purpose of handling cross-origin requests.

3.1 Getting to know our web framework: Warp · 3.1.1 What is included in Warp · 3.1.2 Warp’s filter system · 3.2 GET your first JSON response · 3.2.1 Align with your framework’s way of thinking · 3.2.2 Handle the success route · 3.2.3 Get help from Serde · 3.2.4 Handle errors gracefully · 3.3 Handling CORS headers · 3.3.1 Returning CORS headers on the application level · 3.3.2 Testing CORS responses · 3.4 Summary

What you learn in chapter 3 →

Chapter 4: Implement a RESTful API

This introduction explains RESTful resource access, representation independence, and why the web service must share data safely across threads.

4.1 GET questions from in-memory · 4.1.1 Setting up a mock database · 4.1.2 Preparing a set of test data · 4.1.3 Reading from the fake database · 4.1.4 Parsing query parameters · 4.1.5 Returning custom errors · 4.2 POST, PUT, and DELETE questions · 4.2.1 Updating our data in a thread-safe way · 4.2.2 Adding a question · 4.2.3 Updating a question · 4.2.4 Handling malformed requests · 4.2.5 Removing questions from the storage · 4.3 POST answers via url-form-encoded · 4.3.1 Difference between url-form-encoded and JSON · 4.3.2 Adding answers via url-form-encoded · 4.4 Summary

What you learn in chapter 4 →

Chapter 5: Clean up your codebase

This introduction motivates cleaning up the growing web-service codebase and describes Rust’s documentation, linting, and formatting support.

5.1 Modularizing your code · 5.1.1 Using Rust’s built-in mod system · 5.1.2 Practical folder structure for different use cases · 5.1.3 Creating libraries and sub-crates · 5.2 Documenting your code · 5.2.1 Using doc comments and private comments · 5.2.2 Adding code in your comments · 5.3 Linting and formatting your codebase · 5.3.1 Installing and using Clippy · 5.3.2 Formatting your code with Rustfmt · 5.4 Summary

What you learn in chapter 5 →

Chapter 6: Logging, tracing, and debugging

This introduction explains why a Rust web service needs runtime instrumentation and debugging even when its code compiles.

6.1 Logging in your Rust application · 6.1.1 Implementing logging in your web service · 6.1.2 Logging incoming HTTP requests · 6.1.3 Creating structured logs · 6.2 Tracing in asynchronous applications · 6.2.1 Introducing the Tracing crate · 6.2.2 Integrating tracing in our application · 6.3 Debugging Rust applications · 6.3.1 Using GDB on the command line · 6.3.2 Debugging our web service with LLDB · 6.3.3 Using Visual Studio Code and LLDB · 6.4 Summary

What you learn in chapter 6 →

Chapter 7: Add a database to your application

This introduction previews adding a database to the Rust web service and the choices involved in selecting database tooling.

7.1 Setting up our example database · 7.2 Creating our first tables · 7.3 Working with a database crate · 7.3.1 Adding SQLx into our project · 7.3.2 Connecting Store to our database · 7.4 Reimplementing our route handlers · 7.4.1 Adding the database to get_questions · 7.4.2 Reimplementing the add_question route handler · 7.4.3 Adjusting the update and delete questions handler · 7.4.4 Updating the add_answer route · 7.5 Error handling and tracing database interactions · 7.6 Integrating SQL migrations · 7.7 Case study: Switching database management systems · 7.8 Summary

What you learn in chapter 7 →

Chapter 8: Integrate third-party APIs

The section introduces considerations for integrating external HTTP calls, including failure handling, concurrent operations, and HTTP client choices.

8.1 Preparing the codebase · 8.1.1 Picking an API · 8.1.2 Getting to know our HTTP crate · 8.1.3 Adding an example HTTP call with Reqwest · 8.1.4 Handling errors for external API requests · 8.2 Deserializing JSON responses to structs · 8.2.1 Gathering API response information · 8.2.2 Creating types for our API responses · 8.3 Sending questions and answers to the API · 8.3.1 Refactoring the add_question route handler · 8.3.2 Making profanity checks for updating questions · 8.3.3 Updating the add_answer route handler · 8.4 Handling timeouts and multiple requests at once · 8.4.1 Implementing a retry for external HTTP calls · 8.4.2 Executing futures concurrently or in parallel · 8.5 Summary

What you learn in chapter 8 →

Chapter 9: Add authentication and authorization

The introduction outlines the application changes needed for authentication and authorization while distinguishing common changes from implementation choices.

9.1 Adding authentication to our web service · 9.1.1 Creating the user concept · 9.1.2 Migrating the database · 9.1.3 Adding the registration endpoint · 9.1.4 Hashing the password · 9.1.5 Handling duplicate account errors · 9.1.6 Stateful vs. stateless authentication · 9.1.7 Adding the login endpoint · 9.1.8 Adding an expiry date to tokens · 9.2 Adding authorization middleware · 9.2.1 Migrating the database tables · 9.2.2 Creating token validation middleware · 9.2.3 Extending existing routes to handle account IDs · 9.3 What we didn’t cover · 9.4 Summary

What you learn in chapter 9 →

Chapter 10: Deploy your application

The introduction explains why deployment requires externally supplied configuration and previews production build and packaging topics.

10.1 Setting up your application through environment variables · 10.1.1 Set up config files · 10.1.2 Accept command-line inputs for your application · 10.1.3 Read and parse environment variables into your web service · 10.2 Compiling your web service for different environments · 10.2.1 Development vs. release flag when building your binary · 10.2.2 Cross-compile your binary for different environments · 10.3 Using build.rs in your build process · 10.4 Creating the right Docker image for your web service · 10.4.1 Create a statically linked Docker image · 10.4.2 Set up a local Docker environment with docker-compose · 10.4.3 Extract the configuration of the web server into a new module · 10.5 Summary

What you learn in chapter 10 →

Chapter 11: Testing your Rust application

This introduction explains why and where to test a Rust web service, distinguishes unit and integration tests, and introduces Rust’s basic testing tools.

11.1 Unit testing our business logic · 11.1.1 Testing the pagination logic and dealing with custom errors · 11.1.2 Testing the Config module with environment variables · 11.1.3 Testing the profanity module with a newly created mock server · 11.2 Testing our Warp filters · 11.3 Creating an integration testing setup · 11.3.1 Splitting up the codebase into a lib.rs and a binary · 11.3.2 Creating the integration-test crate and the oneshot server implementation · 11.3.3 Adding the registration test · 11.3.4 Unwinding in case of an error · 11.3.5 Testing the login and posting questions · 11.4 Summary

What you learn in chapter 11 →

Concepts explained in this book

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.