Rust Web Programming: A hands-on guide to developing fast and secure web apps with the Rust programming language

This document was uploaded by one of our users. The uploader already confirmed that they had the permission to publish it. If you are author/publisher or own the copyright of this documents, please report to us by using this DMCA report form.

Simply click on the Download Book button.

Yes, Book downloads on Ebookily are 100% Free.

Sometimes the book is free on Amazon As well, so go ahead and hit "Search on Amazon"

Adopt the Rust programming language by learning how to build fully functional web applications and services and address challenges relating to safety and performance

Key Features

  • Build scalable web applications in Rust using popular frameworks such as Actix, Rocket, and Warp
  • Create front-end components that can be injected into multiple views
  • Develop data models in Rust to interact with the database

Book Description

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

While most programming languages have a safety or speed trade-off, Rust provides memory safety without using a garbage collector. This means that with its low memory footprint, you can build high-performance and secure web apps with relative ease.

This book will take you through each stage of the web development process, showing you how to combine Rust and modern web development principles to build supercharged web apps.

You'll start with an introduction to Rust and understand how to avoid common pitfalls when migrating from traditional dynamic programming languages. The book will show you how to structure Rust code for a project that spans multiple pages and modules. Next, you'll explore the Actix Web framework and get a basic web server up and running. As you advance, you'll learn how to process JSON requests and display data from the web app via HTML, CSS, and JavaScript. You'll also be able to persist data and create RESTful services in Rust. Later, you'll build an automated deployment process for the app on an AWS EC2 instance and Docker Hub. Finally, you'll play around with some popular web frameworks in Rust and compare them.

By the end of this Rust book, you'll be able to confidently create scalable and fast web applications with Rust.

What you will learn

  • Structure scalable web apps in Rust in Rocket, Actix Web, and Warp
  • Apply data persistence for your web apps using PostgreSQL
  • Build login, JWT, and config modules for your web apps
  • Serve HTML, CSS, and JavaScript from the Actix Web server
  • Build unit tests and functional API tests in Postman and Newman
  • Deploy the Rust app with NGINX and Docker onto an AWS EC2 instance

Who this book is for

This book on web programming with Rust is for web developers who have programmed in traditional languages such as Python, Ruby, JavaScript, and Java and are looking to develop high-performance web applications with Rust. Although no prior experience with Rust is necessary, a solid understanding of web development principles and basic knowledge of HTML, CSS, and JavaScript are required if you want to get the most out of this book.

Table of Contents

  1. Quick Introduction to Rust
  2. Designing Your Web Application in Rust
  3. Handling HTTP Requests
  4. Processing HTTP Requests
  5. Displaying Content in the Browser
  6. Data Persistence with PostgreSQL
  7. Managing User Sessions
  8. Building RESTful Services
  9. Testing Our Application Endpoints and Components
  10. Deploying Our Application on AWS
  11. Understanding Rocket Web Framework
  12. Appendix A: Understanding the Warp Framework

Author(s): Maxwell Flitton
Edition: 1
Publisher: Packt Publishing
Year: 2021

Language: English
Commentary: Vector PDF
Pages: 394
City: Birmingham, UK
Tags: Amazon Web Services; Web Programming; Web Applications; PostgreSQL; HTTP; Deployment; Rust; Testing; Metaprogramming; ngnix; RESTful API; Postman; Newman; Docker Hub; Data Persistence; Rocket

Cover
Title Page
Copyright and Credits
Contributors
Table of Contents
Preface
Section 1:Setting Up the Web App Structure
Chapter 1: Quick Introduction to Rust
Technical requirements
Reviewing data types and variables in Rust
Why Rust?
Using strings in Rust
Understanding integers and floats
Storing data in vectors and arrays
Mapping data with hash maps
Handling results and errors
Controlling variable ownership
Scopes
Running through lifetimes
Building structs
Verifying with traits
Metaprogramming with macros
Summary
Questions
Further reading
Chapter 2: Designing Your Web Application in Rust
Technical requirements
Managing 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
Section 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
Managing views using the Actix Web framework
Putting it together
Summary
Questions
Chapter 4: Processing HTTP Requests
Technical requirements
Getting to know the initial setup
Passing parameters
Using macros for JSON serialization
Extracting data
Summary
Questions
Chapter 5: Displaying Content in the Browser
Technical requirements
Displaying HTML in the browser from a server
Injecting JavaScript into HTML
Injecting CSS into HTML
Adding CSS tags to our HTML file
Creating a base CSS file for the whole app
Creating a CSS file for our main view
Updating our Rust crate to serve our CSS and JavaScript
Inheriting components
Summary
Questions
Section 3:Data Persistence
Chapter 6: Data Persistence with PostgreSQL
Technical requirements
Building our PostgreSQL database
Connecting to PostgreSQL with Diesel
Connecting our application to PostgreSQL
Creating our data models and migrations
Getting data from the database
Inserting data into the database
Editing the database
Deleting data
Summary
Questions
Further reading
Chapter 7: Managing User Sessions
Technical requirements
Creating our user model
Creating a user data model
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
Summary
Questions
Further reading
Chapter 8: Building RESTful Services
Technical requirements
What are RESTful services?
Mapping our layered system
Uniform interface
Statelessness
Logging our server traffic
Caching
Code on demand
Summary
Questions
Section 4:Testing and Deployment
Chapter 9: Testing Our Application Endpoints and Components
Technical requirements
Cleaning up our code
Building our unit tests
Building JWT unit tests
Writing tests in Postman
Automating Postman tests with Newman
Summary
Questions
Further reading
Chapter 10: Deploying Our Application on AWS
Technical requirements
Running our application locally
Creating our Docker image
Defining our NGINX container
Defining our server structure
Deploying our application image on dockerhub
Creating a dockerhub repository for our application image
Creating a bash script for our application
Using a docker-compose.yml file to pull our to-do application image
Deploying our application on a server
Creating an EC2 instance on AWS
Configuring traffic rules for the AWS server
Writing a bash script that connects to the server, deploys and starts the application via SSH
Configuring docker-compose for the server
Enabling data persistence on our server
Creating a database on AWS
Refactoring the server structure to accommodate an outside database
Summary
Further reading
Chapter 11: Understanding Rocket Web Framework
Technical requirements
What is Rocket?
Setting up our server
Connecting to our database
Using Diesel crate to connect to our database
Using view parameters
Mounting views onto the server
Authenticating our requests
Implementing a request guard
Building a JWT struct
Applying Rocket traits to our struct
Defining middleware with fairings
Passing data into our views
Putting it all together
Summary
Further reading
Understanding the Warp Framework
Technical requirements
What is Warp?
Setting up our server
Connecting to our database
Authenticating our requests
Adding a header extraction filter
Configuring our JWT to validate the supplied token
Processing and returning the data by using our token
Passing data into our views
Summary
Assessments
Other Books You May Enjoy
Index