Skip to content

Overview

Haskell is a purely functional language, especially suited for robust web applications.

There are many ways to write web applications in Haskell, from raw WAI to full-stack frameworks like Yesod, simple libraries like scotty or type-safe solutions like servant.

Configure your Haskell application

Mandatory configuration

Be sure that:

  • you have pushed in master branch
  • you listen on 0.0.0.0, on the port in PORT
  • you have one and only one binary target in your .cabal file
  • your project has a stack.yaml file

The steps ran in order are:

  • stack setup
  • stack install --only-dependencies
  • stack install
  • ./<path>/my-exe (or the contents of CC_RUN_COMMAND)

The executable built by stack build (or the command you specify) must start a web server listening on 0.0.0.0, on the port in PORT.

For instance, a minimal scotty application can look like this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
{-# LANGUAGE OverloadedStrings #-}

import System.Environment (getEnv)
import Web.Scotty

main = do
  port <- read <$> getEnv "PORT"
  scotty port $ do
    get "/" $ do
      html $ "Hello world"

Dependencies

Make sure to list all your dependencies in your .cabal file.

For the example above, you need:

1
2
3
4
executable myfirstapp
    main-is: Main.hs
    build-depends: base
                 , scotty

Cached dependencies

You can enable dependencies caching by adding the CC_CACHE_DEPENDENCIES=true. It is enabled by default only for rust and haskell applications.

To disable dependencies caching completely, you can remove the CC_CACHE_DEPENDENCIES environment variable or by setting it to false. To fully remove cached dependencies, you have to rebuild your application from scratch.

You can select rebuild and restart from the console or launch clever restart --without-cache with the Clever Tools CLI.

Custom run command

If you need to run a custom command (or just pass options to the binary built by stack), you can specify it through the CC_RUN_COMMAND environment variable.

For instance, for a hakyll website, you can define:

CC_RUN_COMMAND='~/.local/bin/site server --host 0.0.0.0 --port $PORT'

To access environment variables from your code, just get them from the environment with getEnv :: String -> IO String in System.Environment.

If some environment variables are critical to your application, here is an approach you can use:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
{-# LANGUAGE OverloadedStrings #-}
module Main where

import Data.Text.Lazy (pack)
import System.Environment (getEnv)
import Web.Scotty (get, html, scotty)

envVar = getEnv "MY_VAR"

main = do
  myVar <- envVar
  port <- read <$> getEnv "PORT"
  scotty port $ do
    get "/" $ do
      html . pack $ "Hello world " ++ myVar

This loads the environment variable in your main function and tests it. This way, the application will refuse to start with an helpful error message if MY_VAR is not defined.

Specify Stack package target

You may have several packages in your application which can be time consuming when building them, if you don’t want to build every packages you can target a package by using the CC_HASKELL_STACK_TARGET environment variable.

CC_HASKELL_STACK_TARGET="mypackage"

Go Further

Last updated on