Rust Web Programming: A Hands-on Guide to Developing, Packaging, and Deploying Fully Functional Rust Web Applications [2 ed.] 1803234695, 9781803234694

Use the Rust programming language to build fully functional web applications with async Rust to amplify security and boo

1,837 98 8MB

English Pages 666 Year 2023

Report DMCA / Copyright

DOWNLOAD FILE

Rust Web Programming: A Hands-on Guide to Developing, Packaging, and Deploying Fully Functional Rust Web Applications [2 ed.]
 1803234695, 9781803234694

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

Polecaj historie