An edition of Zero To Production In Rust (2022)

Zero To Production In Rust

An Opinionated Introduction to Backend Development

  • 3 Want to read
Locate

My Reading Lists:

Create a new list

Check-In

×Close
Add an optional check-in date. Check-in dates are used to track yearly reading goals.
Today

  • 3 Want to read

Buy this book

Last edited by Evy322
June 12, 2024 | History
An edition of Zero To Production In Rust (2022)

Zero To Production In Rust

An Opinionated Introduction to Backend Development

  • 3 Want to read

Zero To Production is the ideal starting point for your journey as a Rust backend developer.
You will learn by doing: you will build a fully functional email newsletter API, starting from scratch.

You'll learn how to:


  • Navigate and leverage Rust's crates ecosystem

  • Structure your application to make it modular and extensible

  • Write tests, from single units to full-blown integration tests

  • Enforce your domain invariants using Rust's type system

  • Authenticate and authorize users of your API

  • Implement a robust error handling strategy

  • Observe the state of your application using structured logs

  • Set up an extensive continuous integration and continuous deployment pipeline for your Rust projects

Publish Date
Language
English

Buy this book

Book Details


First Sentence

"When you read these lines, Rust has achieved its biggest goal: make an offer to programmers to write their production systems in a different language."

Table of Contents

Foreword
Page xiii
Preface
Page xv
What Is This Book About
Page xv
Cloud-native applications
Page xv
Working in a team
Page xvi
Who Is This Book For
Page xvii
1. Getting Started
Page 1
1.1. Installing The Rust Toolchain
Page 1
1.1.1. Compilation Targets
Page 1
1.1.2. Release Channels
Page 2
1.1.3. What Toolchains Do We Need?
Page 2
1.2. Project Setup
Page 3
1.3. IDEs
Page 3
1.3.1. Rust-analyzer
Page 4
1.3.2. IntelliJ Rust
Page 4
1.3.3. What Should I Use?
Page 4
1.4. Inner Development Loop
Page 4
1.4.1. Faster Linking
Page 5
1.4.2. cargo-watch
Page 6
1.5. Continuous Integration
Page 6
1.5.1. CI Steps
Page 7
1.5.2. Ready-to-go CI Pipelines
Page 10
2. Building An Email Newsletter
Page 11
2.1. Our Driving Example
Page 11
2.1.1. Problem-based Learning
Page 11
2.2. What Should Our Newsletter Do?
Page 12
2.2.1. Capturing Requirements: User Stories
Page 12
2.3. Working In Iterations
Page 13
2.3.1. Coming Up
Page 13
2.4. Checking Your Progress
Page 14
3. Sign Up A New Subscriber
Page 15
3.1. Our Strategy
Page 15
3.2. Choosing A Web Framework
Page 16
3.3. Our First Endpoint: A Basic Health Check
Page 16
3.3.1. Wiring up actix-web
Page 16
3.3.2. Anatomy Of An actix-web Application
Page 18
3.3.3. Implementing The Health Check Handler
Page 23
3.4. Our First Integration Test
Page 25
3.4.1. How Do You Test An Endpoint?
Page 25
3.4.2. Where Should I Put My Tests?
Page 27
3.4.3. Changing Our Project Structure For Easier Testing
Page 28
3.5. Implementing Our First Integration Test
Page 31
3.5.1. Polishing
Page 35
3.6. Refocus
Page 38
3.7. Working With HTML Forms
Page 39
3.7.1. Refining Our Requirements
Page 39
3.7.2. Capturing Our Requirements As Tests
Page 40
3.7.3. Parsing Form Data From A POST Request
Page 42
3.8. Storing Data: Databases
Page 50
3.8.1. Choosing A Database
Page 51
3.8.2. Choosing A Database Crate
Page 52
3.8.3. Integration Testing With Side-effects
Page 54
3.8.4. Database Setup
Page 55
3.8.5. Writing Our First Query
Page 61
3.9. Persisting A New Subscriber
Page 69
3.9.1. Application State In actix-web
Page 69
3.9.2. actix-web Workers
Page 71
3.9.3. The Data Extractor
Page 73
3.9.4. The INSERT Query
Page 74
3.10. Updating Our Tests
Page 78
3.10.1. Test Isolation
Page 81
3.11. Summary
Page 85
4. Telemetry
Page 86
4.1. Unknown Unknowns
Page 86
4.2. Observability
Page 87
4.3. Logging
Page 88
4.3.1. The log Crate
Page 88
4.3.2. actix-web's Logger Middleware
Page 89
4.3.3. The Facade Pattern
Page 90
4.4. Instrumenting POST /subscriptions
Page 92
4.4.1. Interactions With External Systems
Page 93
4.4.2. Think Like A User
Page 94
4.4.3. Logs Must Be Easy To Correlate
Page 96
4.5. Structured Logging
Page 98
4.5.1. The tracing Crate
Page 99
4.5.2. Migrating from log To tracing
Page 99
4.5.3. tracing's Span
Page 100
4.5.4. Instrumenting Futures
Page 103
4.5.5. tracing's Subscriber
Page 104
4.5.6. tracing-subscriber
Page 105
4.5.7. tracing-bunyan-formatter
Page 106
4.5.8. tracing-log
Page 108
4.5.9. Removing Unused Dependencies
Page 109
4.5.10. Cleaning Up Initialisation
Page 110
4.5.11. Logs For Integration Tests
Page 113
4.5.12. Cleaning Up Instrumentation Code - tracing::instrument
Page 117
4.5.13. Protect Your Secrets - secrecy
Page 121
4.5.14. Request Id
Page 124
4.5.15. Leveraging The tracing Ecosystem
Page 126
4.6. Summary
Page 127
5. Going Live
Page 128
5.1. We Must Talk About Deployments
Page 128
5.2. Choosing Our Tools
Page 129
5.2.1. Virtualisation: Docker
Page 129
5.2.2. Hosting: DigitalOcean
Page 130
5.3. A Dockerfile For Our Application
Page 130
5.3.1. Dockerfiles
Page 130
5.3.2. Build Context
Page 131
5.3.3. Sqlx Offline Mode
Page 132
5.3.4. Running An Image
Page 134
5.3.5. Networking
Page 135
5.3.6. Hierarchical Configuration
Page 136
5.3.7. Database Connectivity
Page 141
5.3.8. Optimising Our Docker Image
Page 142
5.4. Deploy To DigitalOcean Apps Platform
Page 147
5.4.1. Setup
Page 147
5.4.2. App Specification
Page 147
5.4.3. How To Inject Secrets Using Environment Variables
Page 150
5.4.4. Connecting To Digital Ocean’s Postgres Instance
Page 152
5.4.5. Environment Variables In The App Spec
Page 156
5.4.6. One Last Push
Page 156
6. Reject Invalid Subscribers #1
Page 158
6.1. Requirements
Page 159
6.1.1. Domain Constraints
Page 159
6.1.2. Security Constraints
Page 159
6.2. First Implementation
Page 160
6.3. Validation Is A Leaky Cauldron
Page 163
6.4. Type-Driven Development
Page 163
6.5. Ownership Meets Invariants
Page 167
6.5.1. AsRef
Page 170
6.6. Panics
Page 172
6.7. Error As Values - Result
Page 174
6.7.1. Converting parse To Return Result
Page 175
6.8. Insightful Assertion Errors: claims
Page 178
6.9. Unit Tests
Page 179
6.10. Handling A Result
Page 181
6.10.1. match
Page 181
6.10.2. The ? Operator
Page 182
6.10.3. 400 Bad Request
Page 183
6.11. The Email Format
Page 183
6.12. The SubscriberEmail Type
Page 184
6.12.1. Breaking The Domain Sub-Module
Page 184
6.12.2. Skeleton Of A New Type
Page 186
6.13. Property-based Testing
Page 188
6.13.1. How To Generate Random Test Data With fake
Page 188
6.13.2. quickcheck Vs proptest
Page 189
6.13.3. Getting Started With quickcheck
Page 190
6.13.4. Implementing The Arbitrary Trait
Page 191
6.14. Payload Validation
Page 193
6.14.1. Refactoring With TryFrom
Page 197
6.15. Summary
Page 199
7. Reject Invalid Subscribers #2
Page 201
7.1. Confirmation Emails
Page 201
7.1.1. Subscriber Consent
Page 201
7.1.2. The Confirmation User Journey
Page 202
7.1.3. The Implementation Strategy
Page 202
7.2. EmailClient, Our Email Delivery Component
Page 203
7.2.1. How To Send An Email
Page 203
7.2.2. How To Write A REST Client Using reqwest
Page 206
7.2.3. How To Test A REST Client
Page 214
7.2.4. First Sketch Of EmailClient::send_email
Page 219
7.2.5. Tightening Our Happy Path Test
Page 227
7.2.6. Dealing With Failures
Page 234
7.3. Skeleton And Principles For A Maintainable Test Suite
Page 244
7.3.1. Why Do We Write Tests?
Page 244
7.3.2. Why Don’t We Write Tests?
Page 245
7.3.3. Test Code Is Still Code
Page 245
7.3.4. Our Test Suite
Page 246
7.3.5. Test Discovery
Page 247
7.3.6. One Test File, One Crate
Page 247
7.3.7. Sharing Test Helpers
Page 248
7.3.8. Sharing Startup Logic
Page 251
7.3.9. Build An API Client
Page 260
7.3.10. Summary
Page 264
7.4. Refocus
Page 264
7.5. Zero Downtime Deployments
Page 265
7.5.1. Reliability
Page 265
7.5.2. Deployment Strategies
Page 265
7.6. Database Migrations
Page 268
7.6.1. State Is Kept Outside The Application
Page 268
7.6.2. Deployments And Migrations
Page 269
7.6.3. Multi-step Migrations
Page 269
7.6.4. A New Mandatory Column
Page 270
7.6.5. A New Table
Page 271
7.7. Sending A Confirmation Email
Page 272
7.7.1. A Static Email
Page 272
7.7.2. A Static Confirmation Link
Page 277
7.7.3. Pending Confirmation
Page 281
7.7.4. Skeleton of GET /subscriptions/confirm
Page 284
7.7.5. Connecting The Dots
Page 287
7.7.6. Subscription Tokens
Page 296
7.8. Database Transactions
Page 304
7.8.1. All Or Nothing
Page 304
7.8.2. Transactions In Postgres
Page 305
7.8.3. Transactions In Sqlx
Page 305
7.9. Summary
Page 309
8. Error Handling
Page 311
8.1. What Is The Purpose Of Errors?
Page 311
8.1.1. Internal Errors
Page 312
8.1.2. Errors At The Edge
Page 314
8.1.3. Summary
Page 316
8.2. Error Reporting For Operators
Page 317
8.2.1. Keeping Track Of The Error Root Cause
Page 319
8.2.2. The Error Trait
Page 324
8.3. Errors For Control Flow
Page 328
8.3.1. Layering
Page 328
8.3.2. Modelling Errors as Enums
Page 329
8.3.3. The Error Type Is Not Enough
Page 331
8.3.4. Removing The Boilerplate With thiserror
Page 335
8.4. Avoid “Ball Of Mud” Error Enums
Page 337
8.4.1. Using anyhow As Opaque Error Type
Page 342
8.4.2. anyhow Or thiserror?
Page 344
8.5. Who Should Log Errors?
Page 345
8.6. Summary
Page 346
9. Naive Newsletter Delivery
Page 348
9.1. User Stories Are Not Set In Stone
Page 348
9.2. Do Not Spam Unconfirmed Subscribers
Page 349
9.2.1. Set Up State Using The Public API
Page 351
9.2.2. Scoped Mocks
Page 351
9.2.3. Green Test
Page 352
9.3. All Confirmed Subscribers Receive New Issues
Page 353
9.3.1. Composing Test Helpers
Page 353
9.4. Implementation Strategy
Page 355
9.5. Body Schema
Page 355
9.5.1. Test Invalid Inputs
Page 356
9.6. Fetch Confirmed Subscribers List
Page 358
9.7. Send Newsletter Emails
Page 361
9.7.1. context Vs with_context
Page 362
9.8. Validation Of Stored Data
Page 363
9.8.1. Responsibility Boundaries
Page 366
9.8.2. Follow The Compiler
Page 368
9.8.3. Remove Some Boilerplate
Page 370
9.9. Limitations Of The Naive Approach
Page 371
9.10. Summary
Page 372
10. Securing Our API
Page 373
10.1. Authentication
Page 373
10.1.1. Drawbacks
Page 374
10.1.2. Multi-factor Authentication
Page 374
10.2. Password-based Authentication
Page 374
10.2.1. Basic Authentication
Page 374
10.2.2. Password Verification - Naive Approach
Page 380
10.2.3. Password Storage
Page 383
10.2.4. Do Not Block The Async Executor
Page 400
10.2.5. User Enumeration
Page 407
10.3. Is it safe?
Page 412
10.3.1. Transport Layer Security (TLS)
Page 412
10.3.2. Password Reset
Page 412
10.3.3. Interaction Types
Page 412
10.3.4. Machine To Machine
Page 412
10.3.5. Person Via Browser
Page 413
10.3.6. Machine to machine, on behalf of a person
Page 414
10.4. Interlude: Next Steps
Page 414
10.5. Login Forms
Page 415
10.5.1. Serving HTML Pages
Page 415
10.6. Login
Page 417
10.6.1. HTML Forms
Page 418
10.6.2. Redirect On Success
Page 421
10.6.3. Processing Form Data
Page 421
10.6.4. Contextual Errors
Page 430
10.7. Sessions
Page 462
10.7.1. Session-based Authentication
Page 462
10.7.2. Session Store
Page 462
10.7.3. Choosing A Session Store
Page 463
10.7.4. actix-session
Page 464
10.7.5. Admin Dashboard
Page 467
10.8. Seed Users
Page 479
10.8.1. Database Migration
Page 479
10.8.2. Password Reset
Page 480
10.9. Refactoring
Page 497
10.9.1. How To Write An actix-web Middleware
Page 498
10.10. Summary
Page 505
11. Fault-tolerant Workflows
Page 507
11.1. POST /admin/newsletters - A Refresher
Page 507
11.2. Our Goal
Page 509
11.3. Failure Modes
Page 509
11.3.1. Invalid Inputs
Page 509
11.3.2. Network I/O
Page 510
11.3.3. Application Crashes
Page 511
11.3.4. Author Actions
Page 511
11.4. Idempotency: An Introduction
Page 511
11.4.1. Idempotency In Action: Payments
Page 512
11.4.2. Idempotency Keys
Page 513
11.4.3. Concurrent Requests
Page 513
11.5. Requirements As Tests #1
Page 514
11.6. Implementation Strategies
Page 515
11.6.1. Stateful Idempotency: Save And Replay
Page 515
11.6.2. Stateless Idempotency: Deterministic Key Generation
Page 516
11.6.3. Time Is a Tricky Beast
Page 516
11.6.4. Making A Choice
Page 516
11.7. Idempotency Store
Page 517
11.7.1. Which Database Should We Use?
Page 517
11.7.2. Schema
Page 517
11.8. Save And Replay
Page 519
11.8.1. Read Idempotency Key
Page 519
11.8.2. Retrieve Saved Responses
Page 523
11.8.3. Save Responses
Page 526
11.9. Concurrent Requests
Page 533
11.9.1. Requirements As Tests #2
Page 533
11.9.2. Synchronization
Page 535
11.10. Dealing With Errors
Page 542
11.10.1. Distributed Transactions
Page 544
11.10.2. Backward Recovery
Page 544
11.10.3. Forward Recovery
Page 545
11.10.4. Asynchronous Processing
Page 545
11.11. Epilogue
Page 562
Links reference
Page 564

The Physical Object

Format
Paperback
Pagination
616
Dimensions
10 x 8 x 1.35 inches
Weight
2.8 pounds

ID Numbers

Open Library
OL46652657M
ISBN 13
9798847211437

Source records

Better World Books record

Links outside Open Library

Community Reviews (0)

Feedback?
No community reviews have been submitted for this work.

Lists

This work does not appear on any lists.

History

Download catalog record: RDF / JSON
June 12, 2024 Edited by Evy322 Edited without comment.
June 12, 2024 Edited by Evy322 Edited without comment.
June 12, 2024 Edited by Evy322 Edited without comment.
February 17, 2023 Created by ImportBot import new book