Created
March 13, 2013 21:50
-
-
Save pbrisbin/5156677 to your computer and use it in GitHub Desktop.
Parse Heroku's DATABASE_URL into a PostgresConf for use in a Yesod application
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# 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