Making A Web App With Actix

Web Ecosystem#

One area where Rust stands out is in the building of web servers.

Rust has its origins at Mozilla primarily as a tool for building a new browser engine. The existing engine being written in C++ combined with the syntactical similarities encourages the idea the Rust was meant to be a replacement for C++. There is obviously some truth to this, but in many ways this characterization sells Rust's potential short. While it is capable of being a systems programming language, there are a plethora of language features that make it suitable for innumerable programming tasks, including building web servers.

There are a few different layers to the web programming stack. Primarily we are concerned here with the application layer which is comparable to where Django, Rails, and Express live in Python, Ruby, and NodeJS, respectively.

The ecosystem around web application development in Rust is still quite nascent despite the Rust language hitting 1.0 in 2015. Much of the underlying infrastructure for building concurrent programs took until 2019 to reach a maturity sufficient to be included in the stable version of the standard library. However, most of the ecosystem has coalesced around similar ideas which take advantage of Rust's particular features.

Before jumping in to building a simple web server, let's briefly discuss a few of the libraries that make up the web landscape.

Hyper#

Hyper is a low level HTTP library built on even lower level libraries for building network services. Currently most web frameworks use Hyper internally for handling the actual HTTP requests.

It can be used to build both HTTP clients and servers. However, there is a bit more boilerplate than you might want to write yourself when you want to focus on building an application. Therefore, we will use a library at a higher level of abstraction which still allows us to take advantage of what Hyper offers.

Actix#

Actix

The Actix project is actually a group of projects which define an actor system as well as a framework for building web applications. The web framework is aptly named actix-web. It has been built on top of futures and async primitives from the beginning. It also runs on the stable version of the compiler.

It recently hit the 1.0 milestone which should bring some much needed stability to the ecosystem. Additionally, it has been at the top of the Tech Empower web framework benchmarks. Even if those are artificial benchmarks, it still points to the performance potential possible.

Actix is the library that we are going to use in this chapter, but before we dive in, let's look at a few others from a high level.

Rocket#

Rocket

Rocket is a web framework which is easy to use while maintaining flexibility, speed, and safety. One of the downsides to this is that currently Rocket only works with the nightly compiler. This is mostly because certain compiler features that make the application developer's life better are not available on stable yet.

Moreover, Rocket intends to move to an async backend once those ideas stabilize in the standard library, but as of version 0.4 the backend is still a synchronous design.

Finally, Rocket has not yet reached 1.0 as there is still a lot of work to do to which might necessitate breaking changes. It is a high quality library which will not likely have much breakage going forward, but there is still some risk which depends on your tolerance.

Others#

There are several other web frameworks in the Rust ecosystem and but it's not possible to cover everything here. Some promising libraries that were built up have fallen into an unmaintained state, whereas others are still building up and are explicitly not ready for prime time.

Iron

Iron is arguably the original Rust web framework. It was actively maintained for quite a while, then was partially abandoned for a while, and then recently resurrected and updated. Iron takes a different approach than the other web frameworks and is more similar to frameworks in other languages.

It is built to have a small, focused core and then provide the rest of its functionality via plugins and middleware. This style is possibly more familiar to web developers coming from other languages, but in the Rust ecosystem, at least currently, this is not the norm.

The core Rust language has an Async Working Group that has an experimental web framework called Tide. They are working out how the async primitives in the standard library can be used to build a web framework that is easy to use and safe. It is explicitly not ready for production, but many of the ideas are similar to what is being done in Actix and Rocket.

Starting out#

We are going to use actix-web because it works with the stable compiler. A lot of the underlying ideas about how data flows in the system is similar to how Rocket works, so switching between the two should not be a fundamental shift in how the application is built.

So let's get started with a new project:

We let cargo bootstrap our project as we have done before. The first thing we are going to do is edit our Cargo.toml file to add all of the dependencies that we will use:

We have already talked about our first dependency, actix-web. The second dependency listed, env_logger, will be used to allow us turn on/off the logging features of actix.

 

This page is a preview of Fullstack Rust

Start a new discussion. All notification go to the author.