Rust Web Programming: A hands-on guide to Rust for modern web development, with microservices and nanoservices. 3 Ed

Rust Web Programming: A hands-on guide to Rust for modern web development, with microservices and nanoservices. 3 Ed

Rust Web Programming: A hands-on guide to Rust for modern web development, with microservices and nanoservices. 3 Ed
0 0 оценок 0 отзывов
Автор: Flitton Maxwell
Дата выхода: 2026
Издательство: Packt Publishing Limited
Количество страниц: 675
Размер файла: 4,1 МБ
Тип файла: PDF
Добавил: it-books
Книгу скачали 5 раз(а)

Key Features

  • Get a comprehensive introduction to Rust for full-stack web development
  • Explore the exciting evolution of Rust in recent years with WebAssembly, Axum, native TLS, and SurrealDB
  • Build code in a scalable way with microservice and nanoservice design patterns

Book Description

Rust is no longer just for systems programming. This book will show you why this safe and performant language is a crucial up-and-coming option for developing web applications, and get you on your way to building fully functional Rust web apps. You don’t need any experience with Rust to get started, and this new edition also comes with a shallower learning curve.

You’ll get hands-on with emerging Rust web frameworks including Actix, Axum, Rocket, and Hyper. You’ll look at injecting Rust into the frontend with WebAssembly and HTTPS configuration with NGINX. Later, you’ll move on to more advanced async topics, exploring TCP and framing, and implementing async systems.

As you work through the book, you’ll build a to-do application with authentication using a microservice architecture that compiles into one Rust binary, including the embedding of a frontend JavaScript application in the same binary. The application will have end-to-end atomic testing and a deployment pipeline.

By the end of this book, you’ll fully understand the significance of Rust for web development. You’ll also have the confidence to build robust, functional, and scalable Rust web applications from scratch.

What you will learn

  • Build scalable Rust web applications as monoliths or microservices
  • Develop a deeper understanding of async Rust
  • Get to grips with Rust language features such as traits and the borrow checker
  • Manage authentication and databases in Rust web apps
  • Build app infrastructure on AWS using Terraform
  • Learn how to package and deploy Rust servers
  • Build unit tests and end-to-end tests for your Rust web apps with Python

Who this book is for

This book is for web developers who are looking to learn or adopt Rust to build safe and performant web applications. This includes developers familiar with languages such as Python, Ruby, and JavaScript.

You don’t need any prior experience in Rust to start this book. However, you’ll need a solid understanding of web development principles, along with basic knowledge of HTML, CSS, and JavaScript to get the most out of it.

Cover....1

Copyright Page....3

Contributors....4

Tabel of Contents....6

Preface....18

Free benefits with your book....23

Chapter 1: A Quick Introduction to Rust....26

Technical requirements....27

What is Rust?....27

Why is Rust revolutionary?....28

Reviewing data types and variables in Rust....30

Using strings in Rust....32

Using integers and floats....34

8-byte integers....35

16-byte integers....35

Introducing floats....37

Storing data in arrays....37

Storing data in vectors....39

Mapping data with enums....40

Mapping data with HashMaps....41

Handling results and errors....43

Controlling variable ownership....46

Copying variables....47

Moving variables....48

Immutable borrowing of variables....50

Mutable borrowing of variables....52

Scopes....54

Running through lifetimes....55

Building Structs....58

Summary....63

Questions....64

Answers....64

Further reading....65

Chapter 2: Useful Rust Patterns for Web Programming....66

Technical requirements....67

Verifying with traits....67

Metaprogramming with macros....71

Mapping messages with macros....74

Defining our contracts and contract handler....75

Defining functions to handle contracts....75

Building a register contract macro....76

Using our macro in a program....77

Configuring our functions with traits....79

Defining the trait signature....79

Implementing our trait for the database handle....80

Calling our macro with our trait....81

Checking struct state with the compiler....83

Defining the state of a struct....83

Changing the state of a struct....84

Summary....85

Questions....86

Answers....86

Chapter 3: Designing Your Web Application in Rust....88

Technical requirements....89

Managing a software project with Cargo....89

Basic Rust compilation....89

Building with Cargo....90

Shipping crates with Cargo....92

Documenting with Cargo....93

Interacting with Cargo....97

Structuring code through nanoservices....102

Building to-do structs....102

Configuring our file structure....103

Defining the task status....104

Building our to-do structs....105

Managing structs with an API....107

Storing tasks with our data access layer....111

Defining our module layout....111

Building our JSON file handle....112

Building storage API functions....114

Creating a task using our DAL....116

Summary....119

Questions....119

Answers....120

Chapter 4: Async Rust....122

Technical requirements....123

Understanding threads and processes....123

Understanding async and await with Rust....126

Implementing our own async task queue....130

Building a task-spawning function....131

Building an async sleep future....132

Running our async sleep future....134

Exploring high-level concepts of tokio....135

Implementing an HTTP server in Hyper....138

Setting up the Hyper server environment....138

Building the Hyper request handle function....139

Building the main function for running the Hyper server....139

Implementing HTTP2 for our Hyper server....141

Summary....142

Questions....142

Answers....142

Chapter 5: Handling HTTP Requests....144

Technical requirements....144

Launching a basic web server....145

Defining our file layout....145

Writing our server code....146

Running our server....148

Connecting the core to the server....149

Defining our core file layout....149

Coding our networking layer....151

Refactoring our to-do items....152

Serving our to-do items....155

Defining the JSON body....155

Defining the get all API....156

Connecting get_all to our server....157

Handling errors in API endpoints....160

Defining our glue module....161

Building our custom error....162

Mapping our custom error....165

Integrating our glue module into our dal....166

Integrating our glue module into our core layer....169

Integrating our glue module into our networking layer....171

Summary....172

Questions....172

Answers....172

Chapter 6: Processing HTTP Requests....174

Technical requirements....174

Passing parameters via the URL....175

Passing data via POST body....178

Defining the core create function....178

Wrapping our core in the networking layer....179

Configuring our create network route....182

Testing our endpoint with cURL....183

Deleting resources using the DELETE method....184

Defining our delete JSON file method....185

Adding delete to our networking layer....186

Defining the delete route....187

Refactoring our JSON file code....187

Automating running our server....189

Updating resources using a PATCH method....192

Extracting data from HTTP request headers....195

Defining our header token modules....195

Implementing an Actix header extraction trait....196

Extracting headers from requests in the Actix networking layer....198

Summary....199

Questions....200

Answers....200

Chapter 7: Displaying Content in the Browser....202

Technical requirements....203

Building out a development setup....203

Connecting our development environment and application....207

Running our frontend server....208

Serving the frontend from Rust....211

Defining our frontend embedding dependencies....211

Embedding the HTML file....213

Embedding the React application....214

Serving the embedded frontend....214

Connecting backend API endpoints to the frontend....219

Defining our interfaces....224

Defining our API utils....226

Defining our GET API call....228

Integrating our get API call in our main app....229

Building out the rest of the API calls....231

Creating React components....233

Building a create item form....234

Injecting our form component into our app....236

Testing our create item form....237

Rendering multiple React components....238

Inserting styles with CSS....243

Serving our CSS....243

Defining page borders with CSS for different screen sizes....245

Styling React components with CSS....247

Summary....251

Questions....251

Answers....252

Chapter 8: Injecting Rust in the Frontend with WebAssembly....254

Technical requirements....254

What is WASM?....255

Setting up our WASM build....256

Defining our WASM module....256

Loading WASM in the frontend....261

Inspecting a WASM interface....262

Integrating WASM loading into our React component....264

Passing our WASM output to subcomponents....265

Loading WASM on the local machine....268

Building a WASM kernel....269

Building a WASM library....271

Building a WASM client....274

Summary....279

Questions....279

Answers....279

Chapter 9: Data Persistence with PostgreSQL....282

Technical requirements....282

Building our PostgreSQL database....283

Why we should use a proper database....283

Why use Docker?....283

How to use Docker....284

Running a database in Docker....286

Exploring routing and ports in Docker....287

Running Docker in the background with Bash scripts....291

Adding SQLX to the data access layer....293

Defining our database transactions....297

Connecting our transactions to the core....304

Connecting our transactions to the server....306

Creating our database migrations....310

Refactoring our frontend....312

Summary....315

Questions....315

Answers....316

Appendix....316

Chapter 10: Managing User Sessions....322

Technical requirements....322

Building our auth server....323

Building our data access layer....324

Building our auth core module....325

Networking layer....326

Defining our user data model....328

Storing passwords....330

Verifying passwords....333

Creating users....334

Defining our create user database transactions....334

Defining our core create API endpoint....335

Defining our networking create API endpoint....336

Refactoring our JWT....337

Restructuring our JWT for a unique ID....338

Creating a get key function for encoding....340

Creating an encode function to encode user credentials....341

Creating a decode function to extract user credentials....341

Building our login API....342

Getting users via email in the data access layer....342

Creating our core login API....344

Mounting our core login function to our server....345

Interacting with our auth server....348

Adding authentication to our frontend....351

Building a login API call....351

Adding tokens to our API calls....353

Building a login form component....353

Connecting the login form to the app....356

Summary....357

Questions....358

Answers....358

Appendix....358

Chapter 11: Communicating Between Servers....362

Technical requirements....362

Getting users from auth with the unique ID....362

Adding get by unique ID to dal....363

Adding get by unique ID to core....364

Adding get by unique ID to networking....364

Making auth accessible to other servers....365

Tethering users to to-do items....369

Linking our to-do items to users in the database....370

Adding user IDs to data access transactions....373

Adding user IDs to core functions....379

Adding user IDs to networking functions....380

Testing our server-to-server communication with Bash....383

Summary....386

Questions....386

Answers....386

Chapter 12: Caching Auth Sessions....388

Technical requirements....388

What is caching?....389

Setting up Redis....390

Building our Redis module....392

Defining the user session....394

Building the login process....399

Building the logout process....400

Building the update process....401

Building our Redis client....403

Building the loginlogout client....404

Building the update client....406

Connecting our cache to our servers....407

Building our cache kernel....408

Calling the kernel from our to-do server....411

Calling the kernel from our auth server....415

Summary....419

Questions....419

Answers....420

Chapter 13: Observability Through Logging....422

Technical requirements....422

What are RESTful services?....422

Building frontend code on command....423

What is logging?....424

Logging via the terminal....426

Defining a logger....426

Creating a logging middleware....428

Integrating our logger into our servers....431

Logging via a database....433

What is an actor?....433

Building our logging actor....434

Update logging functions....437

Configuring our logging database....439

Summary....442

Questions....442

Answers....442

Chapter 14: Unit Testing....446

Technical requirements....446

The importance of unit testing....446

Writing our first unit test....449

Testing our core....451

Unit testing our networking layer....455

Defining our test module....456

Mocking our database handle....457

Mocking our user cache handle....459

Defining our mock service....461

Writing tests....461

Unit testing our data access layer....467

Defining a teardown SQL command....467

Creating a DAL unit test....468

Refactoring our testing script....471

Summary....473

Questions....474

Answers....474

Chapter 15: End-to-End Testing....476

Technical requirements....476

The importance of end-to-end testing....476

Building our to-do HTTP kernel....478

Building our create item HTTP kernel....481

Building our delete item HTTP kernel....484

Building our atomic end-to-end tests....486

Writing the end-to-end test....488

Running our atomic end-to-end test....491

Testing workflows with end-to-end tests....493

Configuring existing code to accommodate the workflow tests....494

Building a kernel API call for creating a user....495

Building a kernel API call for logging in....497

Creating a workflow test workspace....499

Creating a delete workflow test....500

Updating our test runner bash script....503

Summary....505

Questions....505

Answers....505

Chapter 16: Deploying Our Application on AWS....508

Technical requirements....509

Setting up our build environment....509

Setting up an AWS SSH key for an Amazon EC2 instance....509

Setting up our AWS account....512

Setting up our Terraform build....517

Building a server build pipeline....522

Packaging our system with Docker....525

Building the base Docker image for all servers....525

Defining the build config for our monolith....530

Defining the build config for our microservices....533

Distributing our system with Docker....537

Deploying our system with Docker....540

Writing a Terraform script for our server and database....541

Writing a Docker Compose system for our server....543

Writing a deployment script for our system....545

Summary....547

Questions....548

Answers....548

Chapter 17: Configuring HTTPS with NGINX on AWS....550

Technical requirements....550

What is HTTPS?....551

Implementing HTTPS locally with Docker Compose....553

Installing SSL....556

Attaching a URL to our deployed application on AWS....559

Attaching an Elastic IP to our server....561

Registering a domain name....565

Enforcing HTTPS on our application on AWS....570

Getting certificates for our URL....571

Prepping our EC2 instances....574

Creating a load balancer for our traffic....575

Creating security groups to lock down and secure traffic....579

Updating our deployment script....584

Attaching our URL to the load balancer....585

Summary....589

Questions....589

Answers....589

Further reading....590

Chapter 18: Practicalities of Using Microservices and Nanoservices....592

Technical requirements....592

When to use nanoservices, microservices, and monoliths....593

The negative effects of microservices....593

Container orchestration....593

Flying blind....595

Network calls are expensive....597

The positive effects of microservices....598

Rolling out releases....598

Isolation of code....599

Flexibility to experiment....600

Managing private repositories with dockpack....601

Integrating Rust nanoservices with other languages using C interfaces....604

Creating our to-do Rust library....605

Creating our C interface....606

Interfacing with our Rust nanoservice with Python....609

Summary....610

Chapter 19: Low-Level Networking....612

Technical requirements....612

Building a TCP server....612

Building a kernel for key-value messages....613

Building a TCP server....614

Building a TCP client....617

Implementing HTTP on top of TCP....620

Defining a basic server....620

Defining an HTTP client from scratch....621

Implementing HTTPS on top of TCP....623

Exploring WebSockets....626

Summary....629

Questions....629

Answers....630

Chapter 20: Distributed Computing....632

Technical requirements....632

Introduction to queuing....633

Defining the layout of the queuing system....636

Client....636

Compute unit and kernel....637

Server and worker....637

Other configurations....637

Building our kernel....639

Building our compute unit....639

Building our worker....642

Building our server....648

Building our client....650

Running our queuing system....652

Summary....655

Questions....655

Answers....655

Chapter 21: Unlock Your Exclusive Benefits....656

PacktPage....660

Other Books You May Enjoy....662

Index....666

Похожие книги

Ваш отзыв

Оцените книгу
Напишите отзыв о книге
Войти и опубликовать

Отзывы

Нет отзывов к книге.