Environment Variables for Configuration - Examples - Developing Web Apps with Haskell and Yesod, Second Edition (2015)

Developing Web Apps with Haskell and Yesod, Second Edition (2015)

Part III. Examples

Chapter 28. 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 hardcoding 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 APPROOT environment variable to indicate how URLs should be generated, the 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. The following 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

dataApp=App

{ myApproot ::Text

, welcomeMessage ::Text

}

mkYesod "App" [parseRoutes|

/ HomeRGET

|]

instanceYesodAppwhere

approot =ApprootMaster myApproot

getHomeR ::HandlerHtml

getHomeR =defaultLayout $ do

App {..} <-getYesod

setTitle "Environment variables"

[whamlet|

<p>Here's the welcome message: #{welcomeMessage}

<p>

<a href=@{HomeR}>And a link to: @{HomeR}

|]

main ::IO ()

main =do

myApproot <-fmap pack $ getEnv "APPROOT"

welcomeMessage <-fmap pack $ getEnv "WELCOME_MESSAGE"

warp 3000 App {..}

The only tricky things here are:

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

§ We use the ApprootMaster constructor for approot, which says “Apply this function to the foundation data type to get the actual application root.”