Rust Servers, Services, and Apps....1
brief contents....7
contents....8
preface....12
acknowledgments....14
about this book....15
Who should read this book....15
How this book is organized: A road map....16
About the code....17
liveBook discussion forum....18
Other online resources....18
about the author....19
about the cover illustration....20
Part 1 Web servers and services....21
1 Why Rust for web applications?....23
1.1 Introducing modern web applications....24
1.2 Choosing Rust for web applications....27
1.2.1 Characteristics of web applications....27
1.2.2 Benefits of Rust for web applications....28
1.2.3 What does Rust not have?....33
1.3 Visualizing the example application....33
1.3.1 What will we build?....34
1.3.2 Technical guidelines for the example application....36
Summary....37
2 Writing a basic web server from scratch....39
2.1 The networking model....40
2.2 Writing a TCP server in Rust....42
2.2.1 Designing the TCP/IP communication flow....42
2.2.2 Writing the TCP server and client....43
2.3 Writing an HTTP server in Rust....47
2.3.1 Parsing HTTP request messages....49
2.3.2 Constructing HTTP response messages....57
2.3.3 Writing the main() function and server module....64
2.3.4 Writing the router and handler modules....65
2.3.5 Testing the web server....70
Summary....72
3 Building a RESTful web service....73
3.1 Getting started with Actix....73
3.1.1 Writing the first REST API....74
3.1.2 Understanding Actix concepts....76
3.2 Building web APIs with REST....79
3.2.1 Defining the project scope and structure....80
3.2.2 Defining and managing application state....83
3.2.3 Defining the data model....86
3.2.4 Posting a course....90
3.2.5 Getting all the courses for a tutor....94
3.2.6 Getting the details of a single course....96
Summary....98
4 Performing database operations....100
4.1 Setting up the project structure....101
4.2 Writing our first async connection to database (iteration 1)....102
4.2.1 Selecting the database and connection library....102
4.2.2 Setting up the database and connecting with an async pool....103
4.3 Setting up the web service and writing unit tests (iteration 2)....108
4.3.1 Setting up the dependencies and routes....109
4.3.2 Setting up the application state and data model....109
4.3.3 Setting up the connection pool using dependency injection....110
4.3.4 Writing the unit tests....113
4.4 Creating and querying records from the database (iteration 3)....115
4.4.1 Writing database access functions....115
4.4.2 Writing handler functions....118
4.4.3 Writing the main() function for the database-backed web service....121
Summary....123
5 Handling errors....125
5.1 Setting up the project structure....126
5.2 Basic error handling in Rust and Actix Web....129
5.3 Defining a custom error handler....135
5.4 Error handling for retrieving all courses....138
5.5 Error handling for retrieving course details....144
5.6 Error handling for posting a new course....146
Summary....147
6 Evolving the APIs and fearless refactoring....149
6.1 Revamping the project structure....150
6.2 Enhancing the data model for course creation and management....155
6.2.1 Making changes to the data model....156
6.2.2 Making changes to the course APIs....161
6.3 Enabling tutor registration and management....174
6.3.1 Data model and routes for tutors....174
6.3.2 Handler functions for tutor routes....176
6.3.3 Database access functions for tutor routes....178
6.3.4 Database scripts for tutors....180
6.3.5 Run and test the tutor APIs....181
Summary....184
Part 2 Server-side web applications....187
7 Introducing server-side web apps in Rust....189
7.1 Introducing server-side rendering....190
7.2 Serving a static web page with Actix....192
7.3 Rendering a dynamic web page with Actix and Tera....194
7.4 Adding user input with forms....196
7.5 Displaying a list with templates....199
7.6 Writing and running client-side tests....202
7.7 Connecting to the backend web service....205
Summary....208
8 Working with templates for tutor registration....209
8.1 Writing the initial web application....210
8.2 Displaying the registration form....216
8.3 Handling registration submission....221
Summary....226
9 Working with forms for course maintenance....228
9.1 Designing user authentication....229
9.2 Setting up the project structure....230
9.3 Implementing user authentication....231
9.4 Routing HTTP requests....236
9.5 Creating a resource with the HTTP POST method....239
9.6 Updating a resource with the HTTP PUT method....241
9.7 Deleting a resource with the HTTP DELETE method....244
Summary....245
Part 3 Advanced topic: Async Rust....247
10 Understanding async Rust....249
10.1 Introducing async programming concepts....250
10.2 Writing concurrent programs....256
10.3 Diving deeper into async Rust....261
10.4 Understanding futures....265
10.5 Implementing a custom future....272
Summary....275
11 Building a P2P node with async Rust....276
11.1 Introducing peer-to-peer networks....277
11.1.1 Transport....279
11.1.2 Peer identity....279
11.1.3 Security....279
11.1.4 Peer routing....279
11.1.5 Messaging....279
11.1.6 Stream multiplexing....280
11.2 Understanding the core architecture of libp2p networking....280
11.2.1 Peer IDs and key pairs....281
11.2.2 Multiaddresses....283
11.2.3 Swarm and network behavior....284
11.3 Exchanging ping commands between peer nodes....286
11.4 Discovering peers....288
Summary....290
12 Deploying web services with Docker....291
12.1 Introducing production deployment of servers and apps....292
12.1.1 Software deployment cycle....292
12.1.2 Docker container basics....294
12.2 Writing the Docker container....296
12.2.1 Checking the Docker installation....296
12.2.2 Writing a simple Docker container....298
12.2.3 Multistage Docker build....300
12.3 Building the database container....303
12.3.1 Packaging the Postgres database....304
12.3.2 Creating database tables....308
12.4 Packaging the web service with Docker....310
12.5 Orchestrating Docker containers with Docker Compose....312
Summary....317
appendix Postgres installation....319
index....321
Symbols....321
Numerics....321
A....321
B....321
C....322
D....322
E....323
F....324
G....324
H....324
I....325
K....325
L....325
M....325
N....325
O....325
P....325
Q....326
R....326
S....327
T....327
U....328
V....328
W....328
Rust Servers, Services, and Apps - back....330
The blazingly fast, safe, and efficient Rust language has been voted “most loved” for multiple consecutive years on the StackOverflow survey. Rust Server, Services, and Apps shows you why! Inside, you’ll build web servers, RESTful services, server-rendered apps, and client frontends just using Rust. You’ll learn to write code with small and predictable resource footprints, and build high-performing applications with unmatched safety and reliability.
Build speedy, stable, and safe web servers in Rust! With a unique approach to memory management and concurrency, Rust excels at getting the low-level details right so your applications run fast and flawlessly. And Rust’s incredible compiler helps you avoid expensive mistakes when you’re deploying web services and other core components in production.
Rust Servers, Services, and Apps shows you how to create modern distributed web apps using the Rust language. You’ll start with the basics: building a simple HTTP server and a RESTful web service. Then, you’ll make them production ready by adding security, database interactivity, and error handling. Finally, you’ll tackle a digital storefront service, create a single page app, and dig into asynchronous programming. All examples are fully illustrated and include annotated code you can easily adapt to your own projects.
For web developers who know the basics of Rust.