Book Image

Rust Web Programming

By : Maxwell Flitton
Book Image

Rust Web Programming

By: Maxwell Flitton

Overview of this book

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.
Table of Contents (19 chapters)
1
Section 1:Setting Up the Web App Structure
4
Section 2:Processing Data and Managing Displays
8
Section 3:Data Persistence
12
Section 4:Testing and Deployment

What this book covers

Chapter 1, Quick Introduction to Rust, focuses on what's different about Rust. It covers the strong typing and ownership of variables in relation to memory management as this could trip up a developer from a dynamic language. It also covers structs and how behavior is added to them with impl blocks. Finally, macros are introduced as these are heavily utilized in web development with Rust, making processing such as JSON serialization straight forward.

Chapter 2, Designing Your Web Application in Rust, covers the basic Cargo tools for managing a Rust project which include running, documenting, and managing dependencies. With this, we'll run code that has been structured in different files and directories (modules) to build some user structs with traits and manage configuration parameters in a config struct from a config JSON file. We'll finally parse parameters into Cargo to determine whether a development or production config file is parsed in.

Chapter 3, Handling HTTP Requests, introduces the Actix Web framework to get a basic web server up and running. With this, we'll manage multiple routes from different modules to host a range of different views in a structured approach. We'll also explore the async and await concepts that are behind the views of Actix.

Chapter 4, Processing HTTP Requests, explains how we pass through params, bodies, headers and forms to the views and process them returning JSON. We'll then build a response struct that enables us to add a code and an optional message.

Chapter 5, Displaying Content in the Browser, displays data from the web app via HTML through different methods using the Actix Web framework and typed crate. We'll then build on this and utilize CSS and JavaScript to enable the HTML page to interact with the web app API.

Chapter 6, Data Persistence with PostgreSQL, explains how we build a database and define user models using structs. We use the methods we learned about in Chapter 4, Processing HTTP Requests, to develop a create/delete use API.

Chapter 7, Managing User Sessions, helps us to build a login system that manages sessions and enforces expiration time frames for these login sessions, utilizing JWT and datetime.

Chapter 8, Building RESTful Services, helps us to create TODO data models that link to the user data model. We'll then build a RESTful CRUD API that manages the TODO tasks around the user.

Chapter 9, Testing Our Application Endpoints and Components, helps us to build unit tests in Rust for the structs that have functionality. We'll then test the API endpoints with Postman and then automate these tests with Newman.

Chapter 10, Deploying Our Application on AWS, helps us to build an automated deployment process for the app on an AWS EC2 instance with NGINX using docker hub.

Chapter 11, Understanding Rocket Web Framework, covers the Rocket framework. We'll explore the main differences between Rocket and Actix. We'll also build a basic server using Rocket and define the routes, and reuse code and modules that we have built before that worked in the Actix Web framework.

Appendix A, Understanding the Warp Framework, explores the main differences between Warp and Actix. We'll also build a basic server using Warp and define the routes.

Assessments, contains the answers to the questions from all the chapters.