Web Development with Rust— 02/x: Deploy your first App
Tags: node, rust, devops, docker
Published: 2019-03-19 09:12:53

---
title: Web Development with Rust— 02/x: Deploy your first App
published: true
description: If you learn something new, always have an updated version of your project in production. It keeps you motivated. We cover the most common options of how to do this in and with Rust.
tags: nodejs, rust, devops, docker
series: Practical Web Development with Rust
---
>You can find the [Introduction to web programming in Rust](https://dev.to/gruberb/intro-to-web-programming-in-rust-for-nodejs-developers-lp) over here. Follow me on [twitter](https://twitter.com/byteadventures) to always get the latest information about web development in Rust. Also checkout the [GitHub repository](https://github.com/gruberb/web-programming-in-rust) to his series.


***

*Update 08.07.2019: tide 0.2.0*

## This series has 3 goals

1. Show the obstacles new developers will face when programming in Rust but also its advantages when it comes to the specific topic (this time: Deployments).
2. Show different options for web development in Rust.
3. Always have an updated application in production afterwards.

Number three is super important for me. That’s the reason why we start part 02/x with a few ideas on what you can build, and why this tutorial will look like as it does. Whenever you learn something new, take this mental model:

>Never do things for their own sake

Which translate to: Never learn Rust just because you want to learn Rust. This is the biggest factor why you will fail to learn a new language or basically anything in life. You have to have a goal in mind, a reason to do something.

## “So how should I learn Rust then?”

1. Have an app or idea in mind you want to see in production. This can be a service to calculate prime numbers, a web app to track your programming goals, a service which fetches your latest likes on GitHub and tracks their activities etc. You decide what you want to build.
2. Hold yourself accountable to it. Tell your friends, work colleagues or your partner that you promise them that in 6 months time, they will be able to use this service. Every few days or weeks, you will keep them updated about your current status.

It doesn’t need to be a polished idea or a service to compete with other apps. It has to be something you would like to have. It will help you staying through the hardships in the beginning, and through the phase when the honey moon is over and you see that Rust can be hard at times.

#### I will make mine public as well:

_At the end of the next 6 months, I have a running web service with frontend for my MeetUp “Rust and Tell Berlin” up an running so speakers can submit proposals and slides and videos can be watched from talks which were held the previous events._

I will hold myself accountable through this tutorial series.

***

Lets move on. In this part of the series, we want to deploy our first application. If you come from NodeJS, the deployment life cycle looks like this:

![nodejs_dev_cycle](https://raw.githubusercontent.com/gruberb/web-programming-in-rust/master/assets/nodejs_dev.png)

With NodeJS, you can push any code to a production server. You have to have good tests, ESLint and other tools to catch undefined and Type errors.

In an ideal world, we have a development cycle which looks like this:

![ideal_dev_cycle](https://raw.githubusercontent.com/gruberb/web-programming-in-rust/master/assets/rust_dev.png)

So we want to break things as early and close to the code (your local machine) as possible. Once we figured out a working code base, we would like to bring exactly this working solution onto a server. Because of Rusts Type System and strong compiler, we would be able to pack a working binary and move it to production. Tests would cover the rest of the errors.

### Rust moves possible errors closer to the coding environment
a) The Rust Compiler will catch a lot of problems, almost all of them.

b) You can catch the rest with good tests (in our case: Error handling when receiving the wrong parameters).

c) After you can compile your Rust Code, you have a binary which can be shipped in many different ways.

### Difference between local and production-ready code
When we talk about deploying, we have to make sure that our code is able to:

- randomly assign a PORT based on the environment it is running
- handle errors gracefully
- respond to not expected input with proper return codes and messages
- fail early in the deployment pipeline with a proper test setup
- log events so errors can be traced

In this article we will cover the first must-have (randomly assigning a PORT). Each article in the series will cover the rest of the requirements.

### Four different deployment options
We generally have different deployment and hosting options. Some are more suited for large scale application and some are better for private projects and to get a project off the ground without too much complexity. Our options are:

- Managed Deployments / Hosting (Heroku)
- Self managed via Docker and a Docker registry
- Self managed via Docker and a Git registry
- Managed Serverless Lambda functions (AWS Lambda, ZEIT now)

We will cover each of these options in this article and see advantages, disadvantages and how to prepare your Rust Code so it can be deployed (in the best possible way).

## Building the first version of your app
As we said in the beginning, we need an idea and what we want to build. Even if we map out a bigger picture of the application in the next article (03/x), we can get started and choose a framework we want to build it with:

- [rocket](https://rocket.rs/)
- [actix](https://actix.rs/)
- [gotham](https://gotham.rs/)
- [tide](https://github.com/rustasync/tide) (work in progress)

As seen in the [first article](https://dev.to/gruberb/intro-to-web-programming-in-rust-for-nodejs-developers-lp), you can go lower level if you want:

- [hyper](https://hyper.rs/)
- [reqwest](https://docs.rs/reqwest/0.9.11/reqwest/)
- [h2](https://github.com/carllerche/h2)

We will pick one framework for the written version of this article. I will pick [tide](https://github.com/rustasync/tide), since I am planning to contribute to it more in the future. I will map out solutions for rocket and actix in the [GitHub repository](https://github.com/gruberb/web-programming-in-rust) for this series.

### Set up our app
We want to make sure to use asynchronous code, which is not in Rust stable yet. Therefore we need to install and set the [nightly version](https://doc.rust-lang.org/book/appendix-07-nightly-rust.html) of Rust:

```bash
$ rustup install nightly-2019-02-25
$ rustup default nightly
```

This will generate our first folder structure. The bare bones of a running web app with tide look like this:

**Cargo.toml**
```
[package]
name = "my-cool-web-app"
version = "0.1.0"
authors = ["YOUR NAME + EMAIL"]
edition = "2018"

[dependencies]
tide = "0.2.0"
```

**main.rs**
```
#![feature(async_await)]

fn main() {
    let mut app = tide::App::new(());
    app.at("/").get(async move |_| "Hello, world!");

    app.serve();
}
```

As we said earlier, we need to give the hosting environment the chance to assign a PORT to our application. 

Our **main.rs** has to accompany these requirements:
```
#![feature(async_await)]

extern crate tide;

use tide::App;
use std::{env, net::SocketAddr};


fn main() {
    let mut app = App::new(());
    let address = SocketAddr::from(([127, 0, 0, 1], get_server_port()));

    app.at("/").get(async move |_| "hello world");
    app.serve(address).expect("Start server");
}

fn get_server_port() -> u16 {
    env::var("PORT")
        .ok()
        .and_then(|port| port.parse().ok())
        .unwrap_or_else(|| 8186)
}
```

With this setup ready, we can go over each deployment option.

## Managed Deployments via Heroku
> Managed environments are for the most part just an abstraction. They internally do the same as you would with your own pipeline: Push code to a git repository. A “hook” is watching this repository and on changes will start to compile the latest version and run it. For you however, it’s just a `git push heroku master`.

![heroku_deployments](https://raw.githubusercontent.com/gruberb/web-programming-in-rust/master/assets/heroku.png)

To get started, you need a [Heroku account](https://signup.heroku.com/) (free). Login with your new account and create a new app:

![heroku_new_app](https://raw.githubusercontent.com/gruberb/web-programming-in-rust/master/assets/Bildschirmfoto%202019-03-17%20um%2011.35.50.png)

After clicking “Create app”, Heroku explains under the “Deploy” tab how to push your code to their servers:

![heroku_deploy](https://raw.githubusercontent.com/gruberb/web-programming-in-rust/master/assets/Bildschirmfoto%202019-03-17%20um%2011.36.13.png)

#### Prepare your code
First, we need to be able to push our code base to the remote location (Heroku). Therefore please install the [Heroku tool chain](https://devcenter.heroku.com/articles/heroku-cli). Afterwards we can add the remote location to our GIT repository:

```bash
$ cd my-cool-web-app
$ heroku login
$ heroku git:remote -a my-cool-web-app
```

Next, we need to tell Heroku how to run our application after it is build. Heroku expects a file with the name **Procfile**, which has the start command in it:

```bash
$ touch Procfile
```

And put the following line it it:

```
web ./target/release/my-cool-web-app
```

We also have to tell Heroku which version of Rust we are using. Since we want to use nightly, we create a file called **RustConfig** in the root directory:

```bash
$ touch RustConfig
```

with the following line:

```
VERSION=nightly
```

#### Caveat
Rust is so new that Heroku doesn’t support it out of the box. We need to install and activate a “buildpack” for Rust. So inside the root directory of your application, execute the following commands:

```bash
$ heroku create --buildpack emk/rust
$ heroku buildbpacks:set emk/rust
```

This will activate the language support for Rust.

Now we can:

```bash
$ git add .
$ git commit -m "Init"
$ git push heroku master
```

When succeeded, we go back to the Heroku dashboard in the browser and click on the the generated domain (under “Settings”). A browser windiw should open and display “Hello, World!”.

### Summary
- Heroku makes it easy to deploy your application
- In less then 5 minutes you have a running version of your app live
- You can assign your own domain and activate HTTPS (if you pay for it)
- Heroku ist the best option when it comes to this tutorial and starting side projects: Cheap, easy to use and removes the overhead of deplyoments especially in the beginning

## Docker
>Using Docker has the huge advantage of being free in choosing your pipelines and environments. You can either build the image locally and push it as-is to a Docker registry. From there a server can take(download) and execute (`docker run`) it. Or you create a blueprint (Dockerfile) which other service can use to build on their servers.

If you are using Docker for your deployments, you have two options. The first one is to push your code (with a Dockerfile) to a Git registry (like GitHub or Bitbucket) and then have a configured deployment server which listens to changes, SSHs into the Git registry, takes the code, deploys and runs it.

![docker_git_registry](https://raw.githubusercontent.com/gruberb/web-programming-in-rust/master/assets/git_docker.png)

Your second option is to use a Docker registry. There you have the advantage to pre build your container and ship it as-it-is. This makes it sometimes faster to run deployments and you have to ship less code (especially in case of Rust).

![docker_registry](https://raw.githubusercontent.com/gruberb/web-programming-in-rust/master/assets/docker_registry.png)

We can use Rusts feature of being able to be compiled to a binary. We can even go one step further and compile a static Rust binary with no external dependencies. What we would need for this, is:

- Build a Rust binary
- Statically linked the needed C libraries to it so it can run on it’s own

The result would be to have a binary which doesn’t even need Rust to run. Thanks to the Open Source community and Erik Kidd, there is already a solution out there which helps us with that.

The result is a super small Docker image with no external dependencies. Meet [rust-musl-builder](https://github.com/emk/rust-musl-builder). It is a Docker image which helps you build static Rust binaries. It will download the whole image just after the first execution.

Everything we type and create happens from the root directory of our application.

```bash
$ cd my-cool-web-app
```

Before we create our Dockerfile, lets see what we actually trying to do. We are using the rust-musl-builder to statically link the musl-libc library into our binary.

```bash
$ docker run --rm -it -v "$(pwd)":/home/rust/src ekidd/rust-musl-builder cargo build --release
```

This will create our super small binary. You can inspect it like that:

```bash
$ ls -lh target/x86_64-unknown-linux-musl/release/my-cool-web-app
```

It is just a few MB small (in my example: 4,4MB). To be able to recreate this procedure over and over again, and not just on our local machine but also in a deployment pipeline on different servers, we create a multi-stage Dockerfile.

```
FROM ekidd/rust-musl-builder:nightly AS build
COPY . ./
RUN sudo chown -R rust:rust .
RUN cargo build --release

FROM scratch
COPY --from=build /home/rust/src/target/x86_64-unknown-linux-musl/release/my-cool-web-app /
ENV PORT 8181
EXPOSE ${PORT}
CMD ["/my-cool-web-app"]
```

You can build the image now via:

```bash
$ docker build -t my-cool-web-app:latest .
```

And run it with:

```bash
$ docker run -d --rm -P --name heroku heroku:latest
```

Now you can open your browser (in macOS) via:

```bash
$ open http://$(docker container port my-cool-web-app 8181)
```

We just created a super minimal Docker image which contains our binary with no external dependencies. You can inspect your just created image via:

```bash
$ docker image ls my-cool-web-app
```

![small_docker_image](https://raw.githubusercontent.com/gruberb/web-programming-in-rust/master/assets/carbon.png)

### Summary
- Docker is a beast, but when used wisely can be quite helpful
- Especially with Rust: You can create statically linked binaries which are super small and don’t even need a Rust environment to run in
- You also have much more options to host and run your application when choosing Docker
- However, managed hosting environments like Heroku don’t allow pushing Docker images to their environment

## Serverless runtimes — ZEIT/now
>Serverless is a different mindset then the first two options. Serverless also means stateless, so you are not building web applications but functions. Instead of having API endpoints build into your app, you basically just have those API endpoints (in serverless terms: handlers). Our web frameworks like rocket and actix might be an overkill here. [Right now, ZEIT is not supporting Rust nightly builds in their new serverless environment](https://github.com/zeit/now-builders/issues/297#issuecomment-473682599).

So instead of creating a binary (with `cargo new web-app`), we create a library:

```bash
$ cargo new now-service --lib
$ cd now-service
```

Here we have to create a file called `now.json`

```
{
  "name": "now-service",
  "version": 2,
  "builds": [
    {
      "src": "src/index.rs",
      "use": "@now/rust"
    }
  ]
}
```

And our `src/lib.rs` example looks like this:

```
use http::{Request, Response, StatusCode, header};

fn handler(request: Request<()>) -> http::Result<Response<String>> {
    let response = Response::builder()
        .status(StatusCode::OK)
        .header(header::CONTENT_TYPE, "text/html")
        .body("<!doctype html><html><head><title>A simple deployment with Now!</title></head><body><h1>Welcome to Rust on Now</h1></body></html>".to_string())
        .expect("failed to render response");

    Ok(response)
}
```

As with Heroku, you need to install the [ZEIT toolchain](https://zeit.co/docs/v2/getting-started/installation/), which is called “now”. There are several options. If you are on macOS, you can do it via:

```bash
$ brew cask install now
```

Which installs the Now application. Find it in your /Applications folder and open it. You can finish the installation by typing in your email address. This will also install the command line tool chain.

That’s basically it. You can type:

```bash
$ now
```

and hit Enter. This will start the upload of your application. Login to your [ZEIT dashboard](https://zeit.co/dashboard) and click on the provided link.

![ZEIT_dashboard](https://raw.githubusercontent.com/gruberb/web-programming-in-rust/master/assets/zeit_dashboard.png)

### Summary

- Serverless lets you save costs: The service is just running when requested
- This ends up in higher boot times which need to be considered
- The mindset of serverless lets you rethink state and if you really need a fully fledged web application for some use cases
- The deployment can take a bit longer when using AWS Lambda or ZEITs now

> Follow me on [twitter](https://twitter.com/byteadventures) to always get the latest information about web development in Rust. Also checkout the [GitHub repository](https://github.com/gruberb/web-programming-in-rust) to his series.