Browse files

Make the example use sqlite for auth

  • Loading branch information...
1 parent 425c9dd commit 0ee6039f784f3387898d2336182430e66671055b @nurpax committed Aug 19, 2012
Showing with 17 additions and 9 deletions.
  1. +10 −2 example/src/Application.hs
  2. +7 −7 example/src/Site.hs
View
12 example/src/Application.hs
@@ -1,4 +1,7 @@
-{-# LANGUAGE TemplateHaskell #-}
+{-# LANGUAGE FlexibleInstances #-}
+{-# LANGUAGE OverloadedStrings #-}
+{-# LANGUAGE RecordWildCards #-}
+{-# LANGUAGE TemplateHaskell #-}
------------------------------------------------------------------------------
-- | This module defines our application's state type and an alias for its
@@ -7,15 +10,17 @@ module Application where
------------------------------------------------------------------------------
import Data.Lens.Template
-import Snap.Snaplet
+import Snap
import Snap.Snaplet.Heist
import Snap.Snaplet.Auth
import Snap.Snaplet.Session
+import Snap.Snaplet.SqliteSimple
------------------------------------------------------------------------------
data App = App
{ _heist :: Snaplet (Heist App)
, _sess :: Snaplet SessionManager
+ , _db :: Snaplet Sqlite
, _auth :: Snaplet (AuthManager App)
}
@@ -24,6 +29,9 @@ makeLens ''App
instance HasHeist App where
heistLens = subSnaplet heist
+instance HasSqlite (Handler b App) where
+ getSqliteState = with db get
+
------------------------------------------------------------------------------
type AppHandler = Handler App App
View
14 example/src/Site.hs
@@ -16,9 +16,10 @@ import qualified Data.Text as T
import Snap.Core
import Snap.Snaplet
import Snap.Snaplet.Auth
-import Snap.Snaplet.Auth.Backends.JsonFile
+import Snap.Snaplet.Auth.Backends.SqliteSimple
import Snap.Snaplet.Heist
import Snap.Snaplet.Session.Backends.CookieSession
+import Snap.Snaplet.SqliteSimple
import Snap.Util.FileServe
import Text.Templating.Heist
------------------------------------------------------------------------------
@@ -76,12 +77,11 @@ 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.
- a <- nestSnaplet "auth" auth $
- initJsonFileAuthManager defAuthSettings sess "users.json"
+ -- Initialize auth that's backed by an sqlite database
+ d <- nestSnaplet "db" db sqliteInit
+ a <- nestSnaplet "auth" auth $ initSqliteAuth sess d
+
addRoutes routes
addAuthSplices auth
- return $ App h s a
+ return $ App h s d a

0 comments on commit 0ee6039

Please sign in to comment.