Developing software in the Real World

Kitura tutorial part 4: Configuration

Now that we have created a working endpoint in part 3, lets look at the configuration. At the moment, it’s all hard coded! We can do better than that.

If we’re ever going to run this application in the cloud, then it’s advantageous to use environment variables for configuration as per the 12 Factor App guidelines as these vary per deploy.

Environment variable configuration works especially well in containerised / VM applications, but can be a little more problematic when developing on your local computer as different apps may have the same envrionment variable names. To solve this, .env files (pronounced “dotenv”) have become popular for storing configuration, especially in development and test environments.

We will use a .env file locally, but not store it in version control.

.env

This file contains the 6 confiugration values we need for our CouchDB configuration along with the port we wish to run our API on as we may not always want to use port 8090.

.env files are only to be used in development, so we don’t need it in our git repository. Hence the first thing we do is add it to .gitignore:

.gitignore

Adding SwiftDotEnv<

To read our .env file, we are going to use the SwiftDotEnv package. This library will read our .env file and put the variables into the environment for us. It also provides some convenience methods for reading environment variables.

It is used like this:

As you can see, DotEnv provides three get methods:

  • get() returns a String?
  • getInt() returns an Int?
  • getBool() returns a Bool? where case-insensitive "true", "yes" and "1" evaluate to true

We then can use the nil coalescing operator (??) to unwrap with a default if the environment variable doesn’t exist.

To install it, we add its GitHub URL to the dependencies array in our Package.swift file:

Package.swift

A quick run of make will download the library into our Packages directory and compile it.

We can now update main.swift to take advantage of our new configuration system.

Firstly, we import the DotEnv module:

Sources/BookshelfAPI/main.swift

Now we can update our hardcoded configuration values:

Sources/BookshelfAPI/main.swift<

Replace:

with:

We have now replaced all our hardcoded values with the value from the environment. If the environment variable doesn’t exist then we provide defaults so that the ?? operator can unwrap the Optional from the get methods.

We also need to update the port that Kitura will serve our API on:

Sources/BookshelfAPI/main.swift

Replace:

with:

All done

With those changes, you can now make the app and run it. Nothing should change. However if you edit .env and change, say APP_PORT to 8091 then when you make and run you should see that it’s now listening on port 8091:

Let’s now look at adding some more end points in part 5.

Tutorial navigation

GitHub repository: kitura_bookshelfapi