Rust Web Programming - Second Edition

Book description

Use the Rust programming language to build fully functional web applications with async Rust to amplify security and boost the performance of your programs

Key Features

  • Work with cutting-edge web techniques such as distroless Rust servers, Terraform, and AWS deployment
  • Get acquainted with async concepts such as actors and queuing tasks using lower-level frameworks like Tokio
  • Build a full web application in Rust with a database, authentication, and frontend

Book Description

Are safety and high performance a big concern for you while developing web applications?

With this practical Rust book, you’ll discover how you can implement Rust on the web to achieve the desired performance and security as you learn techniques and tooling to build fully operational web apps.

In this second edition, you’ll get hands-on with implementing emerging Rust web frameworks, including Actix, Rocket, and Hyper. It also features HTTPS configuration on AWS when deploying a web application and introduces you to Terraform for automating the building of web infrastructure on AWS. What’s more, this edition also covers advanced async topics. Built on the Tokio async runtime, this explores TCP and framing, implementing async systems with the actor framework, and queuing tasks on Redis to be consumed by a number of worker nodes. Finally, you’ll go over best practices for packaging Rust servers in distroless Rust Docker images with database drivers, so your servers are a total size of 50Mb each.

By the end of this book, you’ll have confidence in your skills to build robust, functional, and scalable web applications from scratch.

What you will learn

  • Structure and build scalable Rust web apps by creating a basic to-do list web app
  • Manage authentication and databases in Rust web applications
  • Get to grips with wrapping web applications in distroless
  • Understand the building blocks of web development such as HTTPS, TCP, and middleware
  • Build app infrastructure on AWS using Terraform with databases, servers, load balancers, HTTPS, and URL routing
  • Build end-to-end tests using Postman
  • Build async systems implementing the actor model using Tokio

Who this book is for

This Rust programming book is for web developers who want to learn and implement Rust to build web applications. Developers familiar with languages such as Python, Ruby, and JS will be able to use this book to build high performant web apps with Rust. Although no prior experience in Rust is necessary, a solid understanding of web development principles, along with basic knowledge of HTML, CSS, and JavaScript, is necessary to get the most out of this book.

Table of contents

  1. Rust Web Programming
  2. Contributors
  3. About the author
  4. About the reviewers
  5. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
    4. Download the example code files
    5. Download the color images
    6. Conventions used
    7. Get in touch
    8. Share Your Thoughts
    9. Download a free PDF copy of this book
  6. Part 1:Getting Started with Rust Web Development
  7. Chapter 1: A Quick Introduction to Rust
    1. Technical requirements
    2. Why is Rust revolutionary?
    3. Reviewing data types and variables in Rust
      1. Using strings in Rust
      2. Using integers and floats
      3. Storing data in vectors and arrays
      4. Mapping data with HashMaps
      5. Handling results and errors
    4. Controlling variable ownership
      1. Copying variables
      2. Moving variables
      3. Immutable borrowing of variables
      4. Mutable borrowing of variables
      5. Scopes
      6. Running through lifetimes
    5. Building structs
      1. Verifying with traits
    6. Metaprogramming with macros
    7. Summary
    8. Questions
    9. Answers
    10. Further reading
  8. Chapter 2: Designing Your Web Application in Rust
    1. Technical requirements
    2. Managing a software project with Cargo
      1. Building with Cargo
      2. Shipping crates with Cargo
      3. Documenting with Cargo
      4. Interacting with Cargo
    3. Structuring code
      1. Building to-do structs
      2. Managing structs with factories
      3. Defining functionality with traits
    4. Interacting with the environment
      1. Reading and writing JSON files
      2. Revisiting traits
      3. Processing traits and structs
    5. Summary
    6. Questions
    7. Answers
  9. Part 2:Processing Data and Managing Displays
  10. Chapter 3: Handling HTTP Requests
    1. Technical requirements
    2. Introducing the Actix Web framework
    3. Launching a basic Actix Web server
    4. Understanding closures
    5. Understanding asynchronous programming
    6. Understanding async and await
    7. Exploring async and await with web programming
    8. Managing views using the Actix Web framework
    9. Summary
    10. Questions
    11. Answers
    12. Further reading
  11. Chapter 4: Processing HTTP Requests
    1. Technical requirements
    2. Getting to know the initial setup for fusing code
    3. Passing parameters into views
    4. Using macros for JSON serialization  
      1. Building our own serialization struct
      2. Implementing the Serialize trait
      3. Integrating serialization structs into our application code
      4. Packaging our custom serialized struct to be returned to users
    5. Extracting data from views
      1. Extracting JSON from the body of a request
      2. Extracting data from the header in requests
      3. Simplifying header extraction with traits
    6. Summary
    7. Questions
    8. Answers
  12. Chapter 5: Displaying Content in the Browser
    1. Technical requirements
    2. Serving HTML, CSS, and JavaScript using Rust
      1. Serving basic HTML
      2. Reading basic HTML from files
      3. Serving basic HTML loaded from files
      4. Adding JavaScript to an HTML file
      5. Communicating with our server using JavaScript
    3. Injecting JavaScript into HTML
      1. Adding the delete endpoint
      2. Adding a JavaScript loading function
      3. Adding JavaScript tags in the HTML
      4. Building a rendering JavaScript function
      5. Building an API call JavaScript function
      6. Building JavaScript functions for buttons
    4. Injecting CSS into HTML
      1. Adding CSS tags to HTML
      2. Creating a base CSS
      3. Creating CSS for the home page
      4. Serving CSS and JavaScript from Rust
    5. Inheriting components
    6. Creating a React app
    7. Making API calls in React
    8. Creating custom components in React
      1. Creating our ToDoItem component
      2. Creating custom components in React
      3. Constructing and managing custom components in our App component
    9. Lifting CSS into React
    10. Converting our React application into a desktop application
    11. Summary
    12. Questions
    13. Answers
    14. Further reading
  13. Part 3:Data Persistence
  14. Chapter 6: Data Persistence with PostgreSQL
    1. Technical requirements
    2. Building our PostgreSQL database
      1. Why we should use a proper database
      2. Why use Docker?
      3. How to use Docker to run a database
      4. Running a database in Docker
      5. Exploring routing and ports in Docker
      6. Running Docker in the background with Bash scripts
    3. Connecting to PostgreSQL with Diesel
    4. Connecting our app to PostgreSQL
      1. Creating our data models
      2. Getting data from the database
    5. Inserting into the database
      1. Editing the database
      2. Deleting data
    6. Configuring our application
    7. Building a database connection pool
    8. Summary
    9. Questions
    10. Answers
  15. Chapter 7: Managing User Sessions
    1. Technical requirements
    2. Creating our user model
      1. Creating a User data module
      2. Creating a NewUser data model
      3. Altering the to-do item data model
      4. Updating the schema file
      5. Creating and running migration scripts on the database
    3. Authenticating our users
    4. Managing user sessions
    5. Cleaning up authentication requirements
    6. Configuring expiration of auth tokens
    7. Adding authentication into our frontend
    8. Summary
    9. Questions
    10. Answers
    11. Further reading
    12. Appendix
  16. Chapter 8: Building RESTful Services
    1. Technical requirements
    2. What are RESTful services?
    3. Mapping our layered system
    4. Building a uniform interface
    5. Implementing statelessness
    6. Logging our server traffic
    7. Caching
    8. Code on demand
    9. Summary
    10. Questions
    11. Answers
  17. Part 4:Testing and Deployment
  18. Chapter 9: Testing Our Application Endpoints and Components
    1. Technical requirements
    2. Building our unit tests
    3. Building JWT unit tests
      1. Building a configuration for tests
      2. Defining the requirements for JWT tests
      3. Building basic function tests for JWT
      4. Building tests for web requests
    4. Writing tests in Postman
      1. Writing ordered requests for tests
      2. Creating a test for an HTTP request
    5. Automating Postman tests with Newman
    6. Building an entire automated testing pipeline
    7. Summary
    8. Questions
    9. Answers
    10. Further reading
  19. Chapter 10: Deploying Our Application on AWS
    1. Technical requirements
    2. Setting up our build environment
      1. Setting up an AWS SSH key for an AWS EC2 instance
      2. Setting up our AWS client
      3. Setting up our Terraform build
      4. Writing our Python application build script
      5. Writing our Bash deployment script
    3. Managing our software with Docker
      1. Writing Docker image files
      2. Building Docker images
      3. Building an EC2 build server using Terraform
      4. Orchestrating builds with Bash
      5. Writing a Docker image file for the React frontend
      6. Deploying images onto Docker Hub
    4. Deploying our application on AWS
      1. Running our application locally
      2. Running our application on AWS
      3. Writing our application build script
    5. Summary
    6. Further reading
  20. Chapter 11: Configuring HTTPS with NGINX on AWS
    1. Technical requirements
    2. What is HTTPS?
      1. Binary protocol
      2. Compressed headers
      3. Persistent connections
      4. Multiplex streaming
    3. Implementing HTTPS locally with docker-compose
    4. Attaching a URL to our deployed application on AWS
      1. Attaching an elastic IP to our server
      2. Registering a domain name
    5. Enforcing HTTPS on our application on AWS
      1. Getting certificates for our URL
      2. Creating multiple EC2 instances
      3. Creating a load balancer for our traffic
      4. Creating security groups to lock down and secure traffic
      5. Updating our Python deployment script for multiple EC2 instances
      6. Attaching our URL to the load balancer
    6. Summary
    7. Further reading
    8. Questions
    9. Answers
  21. Part 5:Making Our Projects Flexible
  22. Chapter 12: Recreating Our Application in Rocket
    1. Technical requirements
    2. What is Rocket?
    3. Setting up our server
    4. Plugging in our existing modules
    5. Implementing Rocket traits
    6. Plugging in our existing views
      1. Accepting and returning JSON
      2. Returning raw HTML
    7. Returning status with JSON
    8. Returning multiple statuses
    9. Registering our views with Rocket
    10. Plugging in our existing tests
    11. Summary
    12. Further reading
    13. Questions
    14. Answers
  23. Chapter 13: Best Practices for a Clean Web App Repository
    1. Technical requirements
    2. The general layout of a clean repository
    3. Getting our configuration from environment variables
    4. Setting up a local development database
    5. Managing variables in Postman tests
    6. Building distroless tiny server Docker images
    7. Building a clean test pipeline
    8. Building continuous integration with GitHub Actions
    9. Summary
    10. Further reading
    11. Questions
    12. Answers
  24. Part 6:Exploring Protocol Programming and Async Concepts with Low-Level Network Applications
  25. Chapter 14: Exploring the Tokio Framework
    1. Technical requirements
    2. Exploring the Tokio framework for async programming
    3. Working with workers
    4. Exploring the actor model for async programming
    5. Working with channels
    6. Working with actors in Tokio
    7. Summary
    8. Further reading
    9. Questions
    10. Answers
  26. Chapter 15: Accepting TCP Traffic with Tokio
    1. Technical requirements
    2. Exploring TCP
    3. Accepting TCP
    4. Processing bytes
    5. Passing TCP to an actor
    6. Keeping track of orders with actors
    7. Chaining communication between actors
    8. Responding with TCP
    9. Sending different commands via the client
    10. Summary
    11. Further reading
    12. Questions
    13. Answers
  27. Chapter 16: Building Protocols on Top of TCP
    1. Technical requirements
    2. Setting up our TCP client and server
      1. Setting up our TCP server
      2. Setting up our TCP client
    3. Processing bytes using structs
      1. Creating a message sender client
      2. Processing messages in the server
    4. Utilizing framing
      1. Rewriting our client so that it supports framing
      2. Rewriting our server so that it supports framing
    5. Building an HTTP frame on top of TCP
    6. Summary
    7. Further reading
    8. Questions
    9. Answers
  28. Chapter 17: Implementing Actors and Async with the Hyper Framework
    1. Technical requirements
    2. Breaking down our project
    3. Defining channel messages
    4. Building our runner actor
    5. Building our state actor
    6. Handling HTTP requests using Hyper
    7. Building an HTTP server using Hyper
    8. Running our Hyper HTTP server
    9. Summary
    10. Further reading
  29. Chapter 18: Queuing Tasks with Redis
    1. Technical requirements
    2. Breaking down our project
    3. Building the HTTP server
    4. Building the polling worker
    5. Getting our application running with Redis
    6. Defining tasks for workers
    7. Defining messages for the Redis queue
    8. Integrating routing in the HTTP server
    9. Running it all in Docker
    10. Summary
    11. Further reading
  30. Index
    1. Why subscribe?
  31. Other Books You May Enjoy
    1. Packt is searching for authors like you
    2. Share Your Thoughts
    3. Download a free PDF copy of this book

Product information

  • Title: Rust Web Programming - Second Edition
  • Author(s): Maxwell Flitton
  • Release date: January 2023
  • Publisher(s): Packt Publishing
  • ISBN: 9781803234694