Rust Web Development

This document was uploaded by one of our users. The uploader already confirmed that they had the permission to publish it. If you are author/publisher or own the copyright of this documents, please report to us by using this DMCA report form.

Simply click on the Download Book button.

Yes, Book downloads on Ebookily are 100% Free.

Sometimes the book is free on Amazon As well, so go ahead and hit "Search on Amazon"

Create bulletproof, high-performance web apps and servers with Rust. In Rust Web Development you will learn: • Handling the borrow checker in an asynchronous environment • Learning the ingredients of an asynchronous Rust stack • Creating web APIs and using JSON in Rust • Graceful error handling • Testing, tracing, logging, and debugging • Deploying Rust applications • Efficient database access Rust Web Development is a pragmatic, hands-on guide to creating server-based web applications with Rust. If you’ve designed web servers using Java, NodeJS, or PHP, you’ll instantly fall in love with the performance and development experience Rust delivers. Hit the ground running! Author Bastian Gruber’s sage advice makes it easy to start tackling complex problems with Rust. You’ll learn how to work efficiently using pure Rust, along with important Rust libraries such as tokio for async runtimes, warp for web servers and APIs, and reqwest to run external HTTP requests. About the technology If you’re sick of cookie-cutter web development tools that are slow, resource hungry, and unstable, Rust is the solution. Rust services deliver rock-solid safety guarantees, an amazing developer experience, and even a compiler that automatically prevents common mistakes! About the book Rust Web Development, teaches you to build server-side web apps using Rust, along with important Rust libraries like tokio for async runtimes, warp for web servers and APIs, and reqwest to run external HTTP requests. The book is packed full of examples, code samples, and pro tips for setting up your projects and organizing your code. As you go, you’ll build a complete Q&A web service and iterate on your code chapter-by-chapter, just like a real development project. What's inside • Handle the borrow checker in an asynchronous environment • Build web APIs and handle JSON • Compose a tech stack for asynchronous Rust development • Handle errors gracefully • Test, trace, log, and debug • Deploy Rust applications to multiple environments About the reader This book is for web developers familiar with Java, Node, or Go, and the absolute basics of Rust. About the author Bastian Gruber is a Protocol Engineer at Centrifuge. He was part of the official Rust Async Working Group, and founded the Rust and Tell Berlin MeetUp group.

Author(s): Bastian Gruber
Edition: 1
Publisher: Manning Publications
Year: 2023

Language: English
Commentary: Publisher's PDF
Pages: 400
City: Shelter Island, NY
Tags: Debugging; Web Applications; SQL; Relational Databases; Logging; Deployment; Documentation; Unit Testing; Web Servers; Rust; Testing; Tracing; RESTful API; Linting; Database Access; Authentication; Warp

Rust Web Development
brief contents
contents
preface
acknowledgments
about this book
Who should read this book
How this book is organized: A road map
About the code
liveBook discussion forum
about the author
about the cover illustration
Part 1—Introduction to Rust
1 Why Rust?
1.1 Batteries included: Rust’s tooling
1.2 The Rust compiler
1.3 Rust for web services
1.4 Maintainability of Rust applications
Summary
2 Laying the foundation
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
Summary
Part 2—Getting started
3 Create your first route handler
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
Summary
4 Implement a RESTful API
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
Summary
5 Clean up your codebase
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
Summary
6 Logging, tracing, and debugging
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
Summary
7 Add a database to your application
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
Summary
8 Integrate third-party APIs
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
Summary
Part 3—Bring it into production
9 Add authentication and authorization
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
Summary
10 Deploy your application
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
Summary
11 Testing your Rust application
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
Summary
Appendix—Thinking about security
A.1 Verify your dependencies for security issues
A.2 Verify your own code
A.3 Closing words
index
Symbols
Numerics
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
Q
R
S
T
U
V
W