Created
April 22, 2014 22:53
-
-
Save mietek/11197162 to your computer and use it in GitHub Desktop.
How to connect a Snap app to Heroku Postgres
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
diff --git a/hello-snap.cabal b/hello-snap.cabal | |
index c759d41..af67b1a 100644 | |
--- a/hello-snap.cabal | |
+++ b/hello-snap.cabal | |
@@ -31,6 +31,7 @@ Executable hello-snap | |
snap-core >= 0.9 && < 0.11, | |
snap-server >= 0.9 && < 0.11, | |
snap-loader-static >= 0.9 && < 0.10, | |
+ snaplet-postgresql-simple >= 0.5 && < 0.6, | |
text >= 0.11 && < 1.2, | |
time >= 1.1 && < 1.5, | |
xmlhtml >= 0.1 | |
diff --git a/src/Application.hs b/src/Application.hs | |
index 8378832..43d27e7 100644 | |
--- a/src/Application.hs | |
+++ b/src/Application.hs | |
@@ -11,11 +11,13 @@ import Snap.Snaplet | |
import Snap.Snaplet.Heist | |
import Snap.Snaplet.Auth | |
import Snap.Snaplet.Session | |
+import Snap.Snaplet.PostgresqlSimple | |
------------------------------------------------------------------------------ | |
data App = App | |
{ _heist :: Snaplet (Heist App) | |
, _sess :: Snaplet SessionManager | |
+ , _db :: Snaplet Postgres | |
, _auth :: Snaplet (AuthManager App) | |
} | |
diff --git a/src/Site.hs b/src/Site.hs | |
index cefdc45..19b158d 100644 | |
--- a/src/Site.hs | |
+++ b/src/Site.hs | |
@@ -11,14 +11,16 @@ module Site | |
------------------------------------------------------------------------------ | |
import Control.Applicative | |
import Data.ByteString (ByteString) | |
+import qualified Data.ByteString.Char8 as BS | |
import qualified Data.Text as T | |
-import Snap.Core | |
-import Snap.Snaplet | |
+import Snap | |
import Snap.Snaplet.Auth | |
-import Snap.Snaplet.Auth.Backends.JsonFile | |
+import Snap.Snaplet.Auth.Backends.PostgresqlSimple | |
import Snap.Snaplet.Heist | |
+import Snap.Snaplet.PostgresqlSimple | |
import Snap.Snaplet.Session.Backends.CookieSession | |
import Snap.Util.FileServe | |
+import System.Environment | |
import Heist | |
import qualified Heist.Interpreted as I | |
------------------------------------------------------------------------------ | |
@@ -77,12 +79,12 @@ app = makeSnaplet "app" "An snaplet example application." Nothing $ do | |
s <- nestSnaplet "sess" sess $ | |
initCookieSessionManager "site_key.txt" "sess" (Just 3600) | |
- -- NOTE: We're using initJsonFileAuthManager here because it's easy and | |
- -- doesn't require any kind of database server to run. In practice, | |
- -- you'll probably want to change this to a more robust auth backend. | |
+ connStr <- BS.pack <$> liftIO (getEnv "DATABASE_URL") | |
+ d <- nestSnaplet "db" db $ | |
+ pgsInit' (pgsDefaultConfig connStr) | |
a <- nestSnaplet "auth" auth $ | |
- initJsonFileAuthManager defAuthSettings sess "users.json" | |
+ initPostgresAuth sess d | |
addRoutes routes | |
addAuthSplices h auth | |
- return $ App h s a | |
+ return $ App h s d a |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment