Skip to content
This repository

Snaplet-Environments

branch: master

Merge pull request #2 from statichippo/patch-1

fix bug requiring both app.environments and environments
latest commit b2a2f2460b
Kamil Ciemniewski authored August 07, 2012
Octocat-spinner-32 src Merge pull request #2 from statichippo/patch-1 August 07, 2012
Octocat-spinner-32 .gitignore Init January 21, 2012
Octocat-spinner-32 LICENSE Init January 21, 2012
Octocat-spinner-32 Makefile snap-0.8; bump to version 0.2 June 12, 2012
Octocat-spinner-32 README.markdown Init January 21, 2012
Octocat-spinner-32 Setup.hs Init January 21, 2012
Octocat-spinner-32 snaplet-environments.cabal unordered-container to 0.2+ July 03, 2012
README.markdown

Snaplet-Environments

Snap Framework ( http://snapframework.com ) support for having different configuration environments.

Ex. you can have development database "your_app_dev" and production "your_app_prod" and test "your_app_test" and switch between them just by running your app in those environments.

Depends on Snap 0.7.* and others

Overview

This isn't traditional snaplet that adds something to your App state. It just base its works on Snap's configuration API giving you a more easy to use ways of getting conf values for environment that your app is currently running.

Let's say you have defined "development" environment in your snaplet.cfg Then running app in this env is as simple as:

yourapp @development

Defining environments

You just have to put your environments group in config file like ex.:

# Your App main configuration file

database-prefix = "yourapp"
database-address = "localhost"

recaptcha-test-mode = false
recaptcha-key  = "sdfgsfdgsdfgsdgs"

heist-root = "resources/templates"

mailers-test-mode = false

environments
{
  development
  {
    database-name = "$(database-prefix)_development"
  }

  production
  {
    database-name = "$(database-prefix)_production"
  }

  test
  {
    database-name = "$(database-prefix)_test"
    recaptcha-test-mode = true
    mailers-test-mode = true
  }
}

If you don't specify which env to run your app - it'll choose first env declaration for you. In this exaple:

yourapp

Would run yourapp in "development" environment.

Integration

In Site.hs

-- (...)
makeSnaplet "app" "An snaplet example application." Nothing $ do
    heistRoot   <- lookupEnvDefault "heist-root" "resources/templates"

    dbName      <- lookupEnvDefault "database-name"        "yourapp_development"
    dbAddress   <- lookupEnvDefault "database-address"     "localhost"
    dbConns     <- lookupEnvDefault "database-connections" 12

    h  <- nestSnaplet "heist" heist $ do
            heistInit heistRoot
    m  <- nestSnaplet "mongoDB" mongoDB $
            mongoDBInit (host dbAddress) dbConns dbName

    --- (...)

The "lookupEnvDefault" helper function reads value from current environment group or if it doesn't have given key - from root of conf file.

lookupEnvDefault :: (Configured a, Monad (m b v), MonadSnaplet m, MonadIO (m b v)) => Name -> a -> m b v a

That's all:)

Something went wrong with that request. Please try again.