10 Deploy your application

 

This chapter covers

  • Setting up your application to read environment variables
  • Optimizing your built binary for production
  • Cross-compiling your service to various operating systems
  • Creating a more complex build process before releasing your code
  • Creating optimized Docker files
  • Setting up a local Docker environment with Docker Compose

After adding authentication and authorization in chapter 9, we can shift gears and leave writing business logic behind. At this stage, all code is written and done, and it is time to show the world what we built. The compiler doesn’t just help us greatly in creating a solid codebase; it also shines when shipping your code to production—for example, cross-compiling the binary for various architectures or creating the smallest binary possible, so we can deploy it via Docker (or without) to different services.

Before we can do that, however, we must go through the code and look for parameters we hardcoded in our application that we have to extract and feed either from environment variables or through CLI commands. Once we move our code from the local machine to a third-party hosting provider, we lose control over which port we have to listen to and how the database URL is defined. These settings are fed by the third-party-provider (or DevOps teams) through environment variables.

10.1 Setting up your application through environment variables

10.1.1 Set up config files

10.1.2 Accept command-line inputs for your application

10.1.3 Read and parse environment variables into your web service

10.2 Compiling your web service for different environments

10.2.1 Development vs. release flag when building your binary

10.2.2 Cross-compile your binary for different environments

10.3 Using build.rs in your build process

10.4 Creating the right Docker image for your web service

10.4.1 Create a statically linked Docker image

10.4.2 Set up a local Docker environment with docker-compose

10.4.3 Extract the configuration of the web server into a new module

Summary