Rewriting to Haskell–Configuration

Posted on March 23, 2020 by Riccardo

This is part of a series:

Coming from Rails we are used to employing yaml files to configure a web application. This is why we decided to do the same with Servant. As a matter of fact, we now have a configuration.yml file:

That is great for development but how can we run test against the test database? Turns out that the package we use to parse the yaml file allows the use of ENV variables:

That is, now we can just run DATABASE=stream_test stack test!

In the repository we actually keep a configuration.yml.example file and git ignore configuration.yml to avoid leaking credentials:

For production we use Ansible (with Ansible Vault) to put in place the correct configuration.yml. Plus, we instruct Hapistrano to make that file available for each deployment:

To read the configuration inside the Servant application we use loadYamlSettings from the yaml package:

In other words, given a type settings that is an instance of FromJSON we can decode yaml files into a value of that type. And this is how we do it for Stream: