Digital Library

Rust Servers, Services, and Apps (Prabhu Eshwarla)(Z-Library)

Prabhu Eshwarla

Rust Servers, Services, and Apps (Prabhu Eshwarla)(Z-Library)

Author Prabhu Eshwarla

rust

Deliver fast, reliable, and maintainable applications by building backend servers, services, and frontends all in nothing but Rust. In Rust Servers, Services, and Apps, you’ll learn: • Developing database-backed web services in Rust • Building and securing RESTful APIs • Writing server-side web applications in Rust • Measuring and benchmarking web service performance • Packaging and deploying web services • Full-stack Rust applications The blazingly fast, safe, and efficient Rust language has been voted “most loved” for multiple consecutive years on the StackOverflow survey. Rust Server, Services, and Apps shows you why! Inside, you’ll build web servers, RESTful services, server-rendered apps, and client frontends just using Rust. You’ll learn to write code with small and predictable resource footprints, and build high-performing applications with unmatched safety and reliability. About the technology Build speedy, stable, and safe web servers in Rust! With a unique approach to memory management and concurrency, Rust excels at getting the low-level details right so your applications run fast and flawlessly. And Rust’s incredible compiler helps you avoid expensive mistakes when you’re deploying web services and other core components in production. About the book Rust Servers, Services, and Apps shows you how to create modern distributed web apps using the Rust language. You’ll start with the basics: building a simple HTTP server and a RESTful web service. Then, you’ll make them production ready by adding security, database interactivity, and error handling. Finally, you’ll tackle a digital storefront service, create a single page app, and dig into asynchronous programming. All examples are fully illustrated and include annotated code you can easily adapt to your own projects. About the reader For web developers who know the basics of Rust.

Format PDF
Size 9.5 MB
14
Views
0
Downloads
0.00
Total Donations

Text Preview (First 20 pages)

Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

Page 1
M A N N I N G Prabhu Eshwarla
Page 2
2 EPILOGUE Our example application (EzyTutors) API & application (external-facing) Rust web service (APIs) Tutor Rust web application (WASM) Internet browsers RDBMS Logging Application configuration Software infra / config P ro du ct io n re ad in es s, p ac ka gi ng , de pl oy m en t, be nc hm ar ki ng Non-Rust web application (React/Angular/Vue) User User UserUser Mobile apps (iOS/Android) Not in project scopeActivities Legend Web service Web application Infra/configApplication modules Course Rust web application (server-side rendering) User auth (email/password) Templates and form handling Custom error handling API auth (JWT token) Application state Automated test scripts Application modules Database access library Actix HTTP server Actix app Get a course Get all courses for tutor Post a course Route 1 In-memory data store Route 3 Route 2 Handler 3 Handler 2 Handler 1 Web client Internet Mobile client EzyTutors web service Web service API request is sent from web and mobile clients to the Actix HTTP server. The Actix HTTP server directs the request to the respective route in the Actix application. Each route directs the request to the corresponding handler. Each handler stores and retrieves data from the in-memory data store and sends HTTP responses back to web and mobile clients. RESTful APIs with Actix
Page 3
Rust Servers, Services, and Apps
Page 4
(This page has no text content)
Page 5
Rust Servers, Services, and Apps PRABHU ESHWARLA M A N N I N G SHELTER ISLAND
Page 6
For online information and ordering of this and other Manning books, please visit www.manning.com. The publisher offers discounts on this book when ordered in quantity. For more information, please contact Special Sales Department Manning Publications Co. 20 Baldwin Road PO Box 761 Shelter Island, NY 11964 Email: orders@manning.com ©2023 by Manning Publications Co. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior written permission of the publisher. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in the book, and Manning Publications was aware of a trademark claim, the designations have been printed in initial caps or all caps. Recognizing the importance of preserving what has been written, it is Manning’s policy to have the books we publish printed on acid-free paper, and we exert our best efforts to that end. Recognizing also our responsibility to conserve the resources of our planet, Manning books are printed on paper that is at least 15 percent recycled and processed without the use of elemental chlorine. The author and publisher have made every effort to ensure that the information in this book was correct at press time. The author and publisher do not assume and hereby disclaim any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from negligence, accident, or any other cause, or from any usage of the information herein. Manning Publications Co. Development editor: Elesha Hyde 20 Baldwin Road Technical development editor: Alain Couniot PO Box 761 Review editor: Aleksandar Dragosavljevic Shelter Island, NY 11964 Production editor: Keri Hales Copy editor: Andy Carroll Proofreader: Melody Dolab Technical proofreader: Jerry Kuch Typesetter and cover designer: Marija Tudor ISBN 9781617298608 Printed in the United States of America
Page 7
brief contents PART 1 WEB SERVERS AND SERVICES ............................................ 1 1 ■ Why Rust for web applications? 3 2 ■ Writing a basic web server from scratch 19 3 ■ Building a RESTful web service 53 4 ■ Performing database operations 80 5 ■ Handling errors 105 6 ■ Evolving the APIs and fearless refactoring 129 PART 2 SERVER-SIDE WEB APPLICATIONS .................................. 167 7 ■ Introducing server-side web apps in Rust 169 8 ■ Working with templates for tutor registration 189 9 ■ Working with forms for course maintenance 208 PART 3 ADVANCED TOPIC: ASYNC RUST .................................. 227 10 ■ Understanding async Rust 229 11 ■ Building a P2P node with async Rust 256 12 ■ Deploying web services with Docker 271v
Page 8
contents preface x acknowledgments xii about this book xiii about the author xvii about the cover illustration xviii PART 1 WEB SERVERS AND SERVICES ............................. 1 1 Why Rust for web applications? 3 1.1 Introducing modern web applications 4 1.2 Choosing Rust for web applications 7 Characteristics of web applications 7 ■ Benefits of Rust for web applications 8 ■ What does Rust not have? 13 1.3 Visualizing the example application 13 What will we build? 14 ■ Technical guidelines for the example application 16 2 Writing a basic web server from scratch 19 2.1 The networking model 20 2.2 Writing a TCP server in Rust 22 Designing the TCP/IP communication flow 22 ■ Writing the TCP server and client 23vi
Page 9
CONTENTS vii2.3 Writing an HTTP server in Rust 27 Parsing HTTP request messages 29 ■ Constructing HTTP response messages 37 ■ Writing the main() function and server module 44 ■ Writing the router and handler modules 45 Testing the web server 50 3 Building a RESTful web service 53 3.1 Getting started with Actix 53 Writing the first REST API 54 ■ Understanding Actix concepts 56 3.2 Building web APIs with REST 59 Defining the project scope and structure 60 ■ Defining and managing application state 63 ■ Defining the data model 66 Posting a course 70 ■ Getting all the courses for a tutor 74 Getting the details of a single course 76 4 Performing database operations 80 4.1 Setting up the project structure 81 4.2 Writing our first async connection to database (iteration 1) 82 Selecting the database and connection library 82 ■ Setting up the database and connecting with an async pool 83 4.3 Setting up the web service and writing unit tests (iteration 2) 88 Setting up the dependencies and routes 89 ■ Setting up the application state and data model 89 ■ Setting up the connection pool using dependency injection 90 ■ Writing the unit tests 93 4.4 Creating and querying records from the database (iteration 3) 95 Writing database access functions 95 ■ Writing handler functions 98 ■ Writing the main() function for the database- backed web service 101 5 Handling errors 105 5.1 Setting up the project structure 106 5.2 Basic error handling in Rust and Actix Web 109 5.3 Defining a custom error handler 115 5.4 Error handling for retrieving all courses 118 5.5 Error handling for retrieving course details 124 5.6 Error handling for posting a new course 126
Page 10
CONTENTSviii6 Evolving the APIs and fearless refactoring 129 6.1 Revamping the project structure 130 6.2 Enhancing the data model for course creation and management 135 Making changes to the data model 136 ■ Making changes to the course APIs 141 6.3 Enabling tutor registration and management 154 Data model and routes for tutors 154 ■ Handler functions for tutor routes 156 ■ Database access functions for tutor routes 158 Database scripts for tutors 160 ■ Run and test the tutor APIs 161 PART 2 SERVER-SIDE WEB APPLICATIONS ................... 167 7 Introducing server-side web apps in Rust 169 7.1 Introducing server-side rendering 170 7.2 Serving a static web page with Actix 172 7.3 Rendering a dynamic web page with Actix and Tera 174 7.4 Adding user input with forms 176 7.5 Displaying a list with templates 179 7.6 Writing and running client-side tests 182 7.7 Connecting to the backend web service 185 8 Working with templates for tutor registration 189 8.1 Writing the initial web application 190 8.2 Displaying the registration form 196 8.3 Handling registration submission 201 9 Working with forms for course maintenance 208 9.1 Designing user authentication 209 9.2 Setting up the project structure 210 9.3 Implementing user authentication 211 9.4 Routing HTTP requests 216 9.5 Creating a resource with the HTTP POST method 219 9.6 Updating a resource with the HTTP PUT method 221 9.7 Deleting a resource with the HTTP DELETE method 224
Page 11
CONTENTS ixPART 3 ADVANCED TOPIC: ASYNC RUST ................... 227 10 Understanding async Rust 229 10.1 Introducing async programming concepts 230 10.2 Writing concurrent programs 236 10.3 Diving deeper into async Rust 241 10.4 Understanding futures 245 10.5 Implementing a custom future 252 11 Building a P2P node with async Rust 256 11.1 Introducing peer-to-peer networks 257 Transport 259 ■ Peer identity 259 ■ Security 259 Peer routing 259 ■ Messaging 259 ■ Stream multiplexing 260 11.2 Understanding the core architecture of libp2p networking 260 Peer IDs and key pairs 261 ■ Multiaddresses 263 ■ Swarm and network behavior 264 11.3 Exchanging ping commands between peer nodes 266 11.4 Discovering peers 268 12 Deploying web services with Docker 271 12.1 Introducing production deployment of servers and apps 272 Software deployment cycle 272 ■ Docker container basics 274 12.2 Writing the Docker container 276 Checking the Docker installation 276 ■ Writing a simple Docker container 278 ■ Multistage Docker build 280 12.3 Building the database container 283 Packaging the Postgres database 284 ■ Creating database tables 288 12.4 Packaging the web service with Docker 290 12.5 Orchestrating Docker containers with Docker Compose 292 appendix Postgres installation 299 index 301
Page 12
preface Building high-performance network services remains a challenge with any program- ming language. Rust has several unique features that significantly lower the challenge threshold. Indeed, Rust has been designed from the very beginning to be a language for highly concurrent and safe systems. Several programming languages (such as C, C++, Go, Java, JavaScript, and Python) are used to develop highly performant and reliable network services that can run on a single node or as part of a multi-node distributed system, either in on-premises data centers or in the cloud, but several points make Rust an attractive alternative: A small footprint (due to full control over memory and CPU usage) Security and reliability (due to memory and data-race safety, enforced by the compiler) Low latency (there is no garbage collector) Modern language features This book teaches the various tools, techniques, and technologies that can be used to build efficient and reliable web services and applications using Rust. It also provides a hands-on introduction to network services and web applications in Rust, all the way from basic single-node, single-threaded servers built from standard library primitives to advanced multithreaded, asynchronous distributed servers, cutting across different layers of the protocol stack. You will learn about Networking primitives in the Rust standard library Basic HTTP services REST API servers backed by a relational databasex
Page 13
PREFACE xi Distributed servers with P2P networking Highly concurrent asynchronous servers This book is designed to teach you to develop web services and applications in Rust using a tutorial-like approach by taking a single example and progressively enhancing it over multiple iterations as you progress through the chapters. I hope you will find the book interesting and the approach practical enough to apply directly to your area of work.
Page 14
acknowledgments Writing a book of this nature in a fast-paced, deep-tech area is a significant commit- ment of time and effort. I would first like to thank my family, who sacrificed a ton of time to allow me to complete this book. There aren’t enough words to say how grateful I am to them. I would like to thank the many people at Manning who have assisted in various ways to help me develop the book in a highly iterative and consultative manner. I want to thank Mike Stephens for giving me this opportunity, as well as the various develop- ment editors, in particular Elesha Hyde for her remarkable support, guidance, and patience in helping take the book to the finish line, tackling numerous challenges along the way. Many thanks also go to the production staff for creating this book in its final form. Last, but not least, my sincere gratitude to Alain Couniot, technical devel- opment editor, without whom this book simply would not have been completed. Thanks, Alain, for patiently and diligently reviewing the chapters, upgrading the code, and elevating the technical quality and relevance of the content for the readers. You rock! Finally, I would also like to thank all the reviewers who provided valuable feedback on the manuscript: Adam Wendell, Alessandro Campeis, Alex Lucas, Bojan Djurkovic, Casey Burnett, Clifford Thurber, Dan Sheikh, David Paccoud, Gustavo Gomes, Hari Khalsa, Helmut Reiterer, Jerome Meyer, Josh Sandeman, Kent R. Spillner, Marcos Oliveira, Matthew Krasnick, Michal Rutka, Pethuru Raj Chelliah, Richard Vaughan, Slavomir Furman, Stephane Negri, Tim van Deurzen, Troi Eisler, Viacheslav Koryagin, William Wheeler, and Yves Dorfsman. Your suggestions were instrumental in making this book even better. My gratitude also goes to the MEAP readers who contributed on the liveBook forum with interesting questions and opinions and spotted the occa- sional typo.xii
Page 15
about this book This book is not a reference guide; rather, it is meant as an introduction and should serve as an inspiring guide to the breadth of network services that can be developed in Rust. It takes the form of a hands-on tutorial in order to maximize learning and retention. Who should read this book This book is designed primarily for backend software engineers involved or interested in server-side, web backend, and API development; distributed systems engineers who wish to explore Rust as an alternative to Go, Java, or C++; and software engineers working on low-latency servers and applications in areas such as machine learning, artificial intelligence, the Internet of Things, image/video/audio processing, and backends for real-time systems. To get the most from this book, you should have both backend development expe- rience and some familiarity with Rust. Specifically, as a backend developer, you should have proficiency in web service concepts including HTTP, JSON, database access with ORM, and API development in any high-level language (e.g., Java, JavaScript, Python, C#, Go, or Ruby). As an advanced beginner or intermediate-level Rust programmer, you should understand how to replicate and modify open source tutorials and reposi- tories and be familiar with the following aspects of Rust: Rust primitives (data types), user-defined data structures (structs, enums), func- tions, expressions, and control loops (if, for, and while loops) Immutability, ownership, references, and borrowing Error handling with Result and option structures Basic functional constructs in Rustxiii
Page 16
ABOUT THIS BOOKxiv The Rust toolchain, including Cargo for build and dependency management and code formatting, documentation, and automated testing tools Please see “Other online resources” later in this section for recommendations for refreshing or increasing your Rust knowledge. How this book is organized: A road map This book is organized as a series of practical projects, each dealing with a specific type of networking service that can be developed in Rust. You will learn by examining the code and by coding along. The relevant theory is explained along the way within the context of these projects. You will also be encouraged to try some suggested cod- ing exercises. This book contains 12 chapters divided among three parts. Part 1 sets the scene by introducing the basic concepts of a web application and laying down the foundations for the following sections. We will develop a web application backend of increasing sophistication, finally reaching a stage close to production readiness. Part 1 consists of the following chapters: Chapter 1 introduces key concepts, such as distributed architectures and web applications. It also introduces the application that we will develop in this book. Finally, it summarizes Rust’s strengths and provides some hints as to when to use and not to use Rust. Chapter 2 is a warm-up chapter for the rest of the book. We will develop a few TCP-based components to get acquainted with Rust’s capabilities in this domain. Chapter 3 shows how to build RESTful web services using Rust and some well- chosen crates among the rich ecosystem that already exists (and keeps grow- ing). It also explains what application state is and how to manage it. Chapter 4 addresses the need to persist data in a database. We will use a simple but efficient crate that interacts with SQL databases. Chapter 5 tackles the important aspect of dealing with unforeseen circum- stances upon invoking the web services we have developed so far. Chapter 6 shows how easy and safe it is to refactor code when developing with Rust as our web service API gets more powerful and sophisticated. Part 2 deals with the other part of the web application, namely its frontend, with its graphical user interface (GUI). In this book, I have opted for a simple approach that relies on server-side rendering instead of sophisticated web frameworks that run in the browser. This part consists of three chapters: Chapter 7 introduces the chosen server-side rendering framework and shows how to prompt the user for input and how to deal with lists of items. It also shows how to interact with the backend web service developed in the previous part.
Page 17
ABOUT THIS BOOK xv Chapter 8 focuses on the templating engine used on the server side. It shows how to support user registration through a few forms. Chapter 9 addresses more advanced web application topics, such as user authentication, routing, and effectively using RESTful web services for main- taining data in a CRUD (create, read, update, delete) fashion. Part 3 covers three advanced topics that are not directly related to the web service and web app we have built so far, but that are important for anyone interested in building complex Rust servers and preparing them for production deployment: Chapter 10 introduces asynchronous programming and how Rust supports this programming paradigm. Then, async programming is illustrated with a few sim- ple examples. Chapter 11 shows the power of Rust for the development of peer-to-peer (P2P) applications using Rust and a few well-chosen crates. Chapter 12 demonstrates the preparation and packaging of our web applica- tion into a Docker image that can then be deployed in a variety of environ- ments (from a local workstation to the cloud). About the code The source code for this book is available on GitHub: https://github.com/pesh war9/rust-servers-services-apps. This repository is structured by book chapter. Gener- ally, the provided code for each chapter corresponds to the final stage of the code for the chapter. You are invited to code along, starting with the code in its state at the end of the previous chapter and letting it evolve incrementally as described in each chap- ter. In the case of a problem, the source code from GitHub should show you what went wrong or at least provide a good basis to resume your development. Setting up the environment should be straightforward for anybody who has already developed a bit in Rust: all that is required is the standard Rust toolchain and a good IDE (integrated development environment), such as VS Code, with some Rust support extensions (the Rust Extension Pack is recommended; Rust Syntax and Rust Doc Viewer are nice additions too). To benefit the most from GitHub and version con- trol, Git should also be installed, but this is not mandatory as you can also download the source code as a zip archive from GitHub. This book contains many examples of source code both in numbered listings and in-line with normal text. In both cases, source code is formatted in a fixed-width font like this to separate it from ordinary text. In many cases, the original source code has been reformatted; we’ve added line breaks and reworked indentation to accommodate the available page space in the book. In rare cases, even this was not enough, and listings include line-continuation markers ( ). Additionally, comments in the source code have often been removed from the listings when the code is described in the text. Code annotations accompany many of the listings, highlighting important concepts.
Page 18
ABOUT THIS BOOKxvi You can get executable snippets of code from the liveBook (online) version of this book at https://livebook.manning.com/book/rust-servers-services-and-apps. The complete code for the examples in the book is available for download from the Man- ning website at https://www.manning.com/books/rust-servers-services-and-apps and from GitHub at https://github.com/peshwar9/rust-servers-services-apps. liveBook discussion forum Purchase of Rust Servers, Services, and Apps includes free access to liveBook, Manning’s online reading platform. Using liveBook’s exclusive discussion features, you can attach comments to the book globally or to specific sections or paragraphs. It’s a snap to make notes for yourself, ask and answer technical questions, and receive help from the author and other users. To access the forum, go to https://livebook.manning.com/book/rust -servers-services-and-apps/discussion. You can also learn more about Manning’s forums and the rules of conduct at https://livebook.manning.com/discussion. Manning’s commitment to our readers is to provide a venue where a meaningful dialogue between individual readers and between readers and the author can take place. It is not a commitment to any specific amount of participation on the part of the author, whose contribution to the forum remains voluntary (and unpaid). We sug- gest you try asking the author some challenging questions lest his interest stray! The forum and the archives of previous discussions will be accessible from the publisher’s website as long as the book is in print. Other online resources Rust as a programming language is supported through several excellent online resources, managed by the Rust creators, as well as a number of independent resources, such as on Medium. Here are some recommended resources: The Rust Book—The official guide from the developers of Rust (www.rust -lang.org/learn). This online book features a section on writing network serv- ers, but it is very basic. Rust by Example—A companion to The Rust Book (https://doc.rust-lang.org/rust -by-example/index.html). The Cargo Book—Another book from the official Rust language site, devoted to the Cargo package manager (https://doc.rust-lang.org/cargo/index.html). The Rust Users Forum (https://users.rust-lang.org/) Medium Rust articles (https://medium.com/tag/rust)
Page 19
about the author PRABHU ESHWARLA is currently the CTO of a startup building a layer-1 blockchain, engineered using Rust. Prabhu became deeply interested in Rust as a programming language and has been actively learning and working on it since July 2019. He has pre- viously held several software engineering and tech leadership roles at Hewlett Packard. xvii
Page 20
about the cover illustration The figure on the cover of Rust Servers, Services, and Apps is “Homme Toungouse,” or “Tungus Man,” taken from a collection by Jacques Grasset de Saint-Sauveur, published in 1788. Each illustration is finely drawn and colored by hand. In those days, it was easy to identify where people lived and what their trade or sta- tion in life was just by their dress. Manning celebrates the inventiveness and initiative of the computer business with book covers based on the rich diversity of regional cul- ture centuries ago, brought back to life by pictures from collections such as this one. xviii
The above is a preview of the first 20 pages. Register to read the complete e-book.

Support Author

0.00
Total Amount (¥)
0
Donation Count
Please enter an amount Minimum ¥1

You will be redirected to Alipay to complete payment, then return here.

Recommended for You

Loading recommended books...
Failed to load, please try again later
Back to List