Rust Web Programming

Book description

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. Rust Web Programming
  2. Contributors
  3. About the author
  4. About the reviewer
  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. Code in Action
    6. Download the color images
    7. Conventions used
    8. Get in touch
    9. Reviews
  6. Section 1:Setting Up the Web App Structure
  7. Chapter 1: Quick Introduction to Rust
    1. Technical requirements
    2. Reviewing data types and variables in Rust
      1. Why Rust?
      2. Using strings in Rust
      3. Understanding integers and floats
      4. Storing data in vectors and arrays
      5. Mapping data with hash maps
      6. Handling results and errors
    3. Controlling variable ownership
      1. Scopes
      2. Running through lifetimes
    4. Building structs
      1. Verifying with traits
    5. Metaprogramming with macros
    6. Summary
    7. Questions
    8. Further reading
  8. Chapter 2: Designing Your Web Application in Rust
    1. Technical requirements
    2. Managing 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
  9. Section 2:Processing Data and Managing Displays
  10. Chapter 3: Handling HTTP Requests
    1. Technical requirements
    2. Introducing the Actix Web framework
      1. Launching a basic Actix Web server
    3. Understanding closures
    4. Understanding asynchronous programming
    5. Understanding async and await
    6. Managing views using the Actix Web framework
    7. Putting it together
    8. Summary
    9. Questions
  11. Chapter 4: Processing HTTP Requests
    1. Technical requirements
    2. Getting to know the initial setup
    3. Passing parameters
    4. Using macros for JSON serialization
    5. Extracting data
    6. Summary
    7. Questions
  12. Chapter 5: Displaying Content in the Browser
    1. Technical requirements
    2. Displaying HTML in the browser from a server
    3. Injecting JavaScript into HTML
    4. Injecting CSS into HTML
      1. Adding CSS tags to our HTML file
      2. Creating a base CSS file for the whole app
      3. Creating a CSS file for our main view
      4. Updating our Rust crate to serve our CSS and JavaScript
    5. Inheriting components
    6. Summary
    7. Questions
  13. Section 3:Data Persistence
  14. Chapter 6: Data Persistence with PostgreSQL
    1. Technical requirements
    2. Building our PostgreSQL database
    3. Connecting to PostgreSQL with Diesel
    4. Connecting our application to PostgreSQL
      1. Creating our data models and migrations
      2. Getting data from the database
    5. Inserting data into the database
      1. Editing the database
      2. Deleting data
    6. Summary
    7. Questions
    8. Further reading
  15. Chapter 7: Managing User Sessions
    1. Technical requirements
    2. Creating our user model
      1. Creating a user data model
      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. Summary
    6. Questions
    7. Further reading
  16. Chapter 8: Building RESTful Services
    1. Technical requirements
    2. What are RESTful services?
      1. Mapping our layered system
      2. Uniform interface
      3. Statelessness
      4. Logging our server traffic
      5. Caching
      6. Code on demand
    3. Summary
    4. Questions
  17. Section 4:Testing and Deployment
  18. Chapter 9: Testing Our Application Endpoints and Components
    1. Technical requirements
    2. Cleaning up our code
    3. Building our unit tests
      1. Building JWT unit tests
    4. Writing tests in Postman
    5. Automating Postman tests with Newman
    6. Summary
    7. Questions
    8. Further reading
  19. Chapter 10: Deploying Our Application on AWS
    1. Technical requirements
    2. Running our application locally
      1. Creating our Docker image
      2. Defining our NGINX container
      3. Defining our server structure
    3. Deploying our application image on dockerhub
      1. Creating a dockerhub repository for our application image
      2. Creating a bash script for our application
      3. Using a docker-compose.yml file to pull our to-do application image
    4. Deploying our application on a server
      1. Creating an EC2 instance on AWS
      2. Configuring traffic rules for the AWS server
      3. Writing a bash script that connects to the server, deploys and starts the application via SSH
      4. Configuring docker-compose for the server
    5. Enabling data persistence on our server
      1. Creating a database on AWS
      2. Refactoring the server structure to accommodate an outside database
    6. Summary
    7. Further reading
  20. Chapter 11: Understanding Rocket Web Framework
    1. Technical requirements
    2. What is Rocket?
    3. Setting up our server
    4. Connecting to our database
      1. Using Diesel crate to connect to our database
      2. Using view parameters
      3. Mounting views onto the server
    5. Authenticating our requests
      1. Implementing a request guard
      2. Building a JWT struct
      3. Applying Rocket traits to our struct
    6. Defining middleware with fairings
    7. Passing data into our views
    8. Putting it all together
    9. Summary
    10. Further reading
  21. Appendix A: Understanding the Warp Framework
    1. Technical requirements
    2. What is Warp?
    3. Setting up our server
    4. Connecting to our database
    5. Authenticating our requests
      1. Adding a header extraction filter
      2. Configuring our JWT to validate the supplied token
      3. Processing and returning the data by using our token
    6. Passing data into our views
    7. Summary
  22. Assessments
    1. Chapter 1, Quick Introduction to Rust
    2. Question 1
    3. Answer
    4. Question 2
    5. Answer
    6. Question 3
    7. Answer
    8. Question 4
    9. Answer
    10. Question 5
    11. Answer
    12. Question 6
    13. Answer
    14. Question 7
    15. Answer
    16. Question 8
    17. Answer
    18. Question 9
    19. Answer
    20. Question 10
    21. Answer
    22. Chapter 2, Designing Your Web Application in Rust
    23. Question 1
    24. Answer
    25. Question 2
    26. Answer
    27. Question 3
    28. Answer
    29. Question 4
    30. Answer
    31. Question 5
    32. Answer
    33. Question 6
    34. Answer
    35. Chapter 3, Handling HTTP Requests
    36. Question 1
    37. Answer
    38. Question 2
    39. Answer
    40. Question 3
    41. Answer
    42. Question 4
    43. Answer
    44. Question 5
    45. Answer
    46. Question 6
    47. Answer
    48. Question 7
    49. Answer
    50. Chapter 4, Processing HTTP Requests
    51. Question 1
    52. Answer
    53. Question 2
    54. Answer
    55. Question 3
    56. Answer
    57. Question 4
    58. Answer
    59. Question 5
    60. Answer
    61. Chapter 5, Displaying Content in the Browser
    62. Question 1
    63. Answer
    64. Question 2
    65. Answer
    66. Question 3
    67. Answer
    68. Question 4
    69. Answer
    70. Question 5
    71. Answer
    72. Chapter 6, Data Persistence with PostgreSQL
    73. Question 1
    74. Answer
    75. Question 2
    76. Answer
    77. Question 3
    78. Answer
    79. Question 4
    80. Answer
    81. Chapter 7, Managing User Sessions
    82. Question 1
    83. Answer
    84. Question 2
    85. Answer
    86. Question 3
    87. Answer
    88. Question 4
    89. Answer
    90. Question 5
    91. Answer
    92. Question 6
    93. Answer
    94. Chapter 8, Building RESTful Services
    95. Question 1
    96. Answer
    97. Question 2
    98. Answer
    99. Question 3
    100. Answer
    101. Question 4
    102. Answer
    103. Question 5
    104. Answer
    105. Question 6
    106. Answer
    107. Question 7
    108. Answer
    109. Chapter 9, Testing Our Application Endpoints and Components
    110. Question 1
    111. Answer
    112. Question 2
    113. Answer
    114. Question 3
    115. Answer
    116. Question 4
    117. Answer
    118. Question 5
    119. Answer
    120. Question 6
    121. Answer
    122. Question 7
    123. Answer
    124. Why subscribe?
  23. Other Books You May Enjoy
    1. Packt is searching for authors like you
    2. Leave a review - let other readers know what you think

Product information

  • Title: Rust Web Programming
  • Author(s): Maxwell Flitton
  • Release date: February 2021
  • Publisher(s): Packt Publishing
  • ISBN: 9781800560819