Parse Heroku's DATABASE_URL into a PostgresConf for use in a Yesod application
{-# LANGUAGE OverloadedStrings #-} | |
-------------------------------------------------------------------------------- | |
-- | | |
-- Module : Yesod.HerokuConf | |
-- Copyright : (c) Patrick Brisbin 2013 | |
-- License : as-is | |
-- Maintainer : pbrisbin@gmail.com | |
-- Stability : unstable | |
-- Portability : unportable | |
-- | |
-- Parses DATABASE_URL into a valid PostgresConf. Useful when running | |
-- a Yesod app with Postgresql on Heroku. | |
-- | |
-- > makeFoundation conf = do | |
-- > manager <- newManager def | |
-- > s <- staticSite | |
-- > dbconf <- herokuConf | |
-- > p <- Database.Persist.Store.createPoolConfig dbconf | |
-- > Database.Persist.Store.runPool dbconf (runMigration migrateAll) p | |
-- > return $ App conf s p manager dbconf | |
-- | |
-------------------------------------------------------------------------------- | |
module Yesod.HerokuConf where | |
import Data.ByteString (ByteString) | |
import Data.Text (Text) | |
import Data.Text.Encoding (encodeUtf8) | |
import Database.Persist.Postgresql (PostgresConf(..)) | |
import Web.Heroku (dbConnParams) | |
import qualified Data.Text as T | |
herokuConf :: IO PostgresConf | |
herokuConf = do | |
params <- dbConnParams | |
return PostgresConf | |
{ pgConnStr = formatParams params | |
, pgPoolSize = 100 -- TODO: how to set this? | |
} | |
where | |
formatParams :: [(Text, Text)] -> ByteString | |
formatParams = encodeUtf8 . T.unwords . map toKeyValue | |
toKeyValue :: (Text, Text) -> Text | |
toKeyValue (k, v) = k `T.append` "=" `T.append` v |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment