Environment variables for configuration

There’s a recent move, perhaps most prominently advocated by the twelve-factor app, to store all app configuration in environment variables, instead of using config files or hard-coding them into the application source code (you don’t do that, right?).

Yesod’s scaffolding comes built in with some support for this, most specifically for respecting the YESOD_APPROOT environment variable to indicate how URLs should be generated, the YESOD_PORT environment variable for which port to listen for requests on, and database connection settings. (Incidentally, this ties in nicely with the Keter deployment manager.)

The technique for doing this is quite easy: just do the environment variable lookup in your main function. This example demonstrates this technique, along with the slightly special handling necessary for setting the application root.

{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes       #-}
{-# LANGUAGE RecordWildCards   #-}
{-# LANGUAGE TemplateHaskell   #-}
{-# LANGUAGE TypeFamilies      #-}
import           Data.Text          (Text, pack)
import           System.Environment
import           Yesod

data App = App
    { myApproot      :: Text
    , welcomeMessage :: Text

mkYesod "App" [parseRoutes|
/ HomeR GET

instance Yesod App where
    approot = ApprootMaster myApproot

getHomeR :: Handler Html
getHomeR = defaultLayout $ do
    App {..} <- getYesod
    setTitle "Environment variables"
        <p>Here's the welcome message: #{welcomeMessage}
            <a href=@{HomeR}>And a link to: @{HomeR}

main :: IO ()
main = do
    myApproot <- fmap pack $ getEnv "YESOD_APPROOT"
    welcomeMessage <- fmap pack $ getEnv "WELCOME_MESSAGE"
    warp 3000 App {..}

The only tricky things here are:

  1. You need to convert the String value returned by getEnv into a Text by using pack.

  2. We use the ApprootMaster constructor for approot, which says "apply this function to the foundation datatype to get the actual application root."