Skip to content

Commit

Permalink
Ship with default config files
Browse files Browse the repository at this point in the history
  • Loading branch information
mightybyte committed Mar 29, 2012
1 parent cf7af26 commit 6fbf493
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 18 deletions.
6 changes: 3 additions & 3 deletions example/Site.hs
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ addHandler = do
-- | The application initializer.
app :: SnapletInit App App
app = makeSnaplet "app" "An snaplet example application." Nothing $ do
s <- nestSnaplet "" sess $ initCookieSessionManager "site_key.txt"
"_cookie" Nothing
d <- nestSnaplet "db" db $ pgsInit
s <- nestSnaplet "" sess $
initCookieSessionManager "site_key.txt" "_cookie" Nothing
d <- nestSnaplet "db" db pgsInit
a <- nestSnaplet "auth" auth $ initPostgresAuth sess d
addRoutes routes
return $ App s d a
Expand Down
7 changes: 0 additions & 7 deletions example/snaplet.cfg

This file was deleted.

21 changes: 21 additions & 0 deletions resources/auth/snaplet.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Currently this option is not enforced. See current auth documentation for
# more information.
minPasswordLen = 8

# Name of the cookie to use for remembering the logged in user.
rememberCookie = "_remember"

# Number of seconds of inactivity before the user is logged out. If ommitted,
# the user will remain logged in until the end of the session.
rememberPeriod = 1209600 # 2 weeks

# Lockout strategy. The first value is the max number of invalid login
# attempts before lockout. The second value is how long the locked lasts. If
# ommitted, then incorrect passwords will never result in lockout.
# lockout = [5, 86400]

# File where the auth encryption key is stored.
siteKey = "site_key.txt"

# Name of the table where the user data is stored.
authTable = "snap_auth_user"
5 changes: 5 additions & 0 deletions resources/db/snaplet.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
host = "localhost"
port = 5432
user = "postgres"
pass = ""
db = "testdb"
5 changes: 5 additions & 0 deletions snaplet-postgresql-simple.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ category: Web

extra-source-files: LICENSE

data-files:
resources/db/snaplet.cfg
resources/auth/snaplet.cfg

source-repository head
type: git
location: https://github.com/mightybyte/snaplet-postgresql-simple.git
Expand All @@ -26,6 +30,7 @@ Library
exposed-modules:
Snap.Snaplet.PostgresqlSimple
Snap.Snaplet.Auth.Backends.PostgresqlSimple
Paths_snaplet_postgresql_simple

build-depends:
base >= 4 && < 5,
Expand Down
29 changes: 24 additions & 5 deletions src/Snap/Snaplet/Auth/Backends/PostgresqlSimple.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}

module Snap.Snaplet.Auth.Backends.PostgresqlSimple where
module Snap.Snaplet.Auth.Backends.PostgresqlSimple
( initPostgresAuth
) where

------------------------------------------------------------------------------
import Control.Arrow
Expand All @@ -25,6 +27,7 @@ import Snap.Snaplet.PostgresqlSimple
import Snap.Snaplet.Session
import Snap.Snaplet.Session.Common
import Web.ClientSession
import Paths_snaplet_postgresql_simple


data PostgresAuthManager = PostgresAuthManager
Expand All @@ -46,19 +49,34 @@ settingsFromConfig = do
rememberPeriod <- liftIO $ C.lookup config "rememberPeriod"
let rp = maybe id (\x s -> s { asRememberPeriod = Just x }) rememberPeriod
lockout <- liftIO $ C.lookup config "lockout"
let lo = maybe id (\x s -> s { asLockout = Just (second fromInteger x) }) lockout
let lo = maybe id (\x s -> s { asLockout = Just (second fromInteger x) })
lockout
siteKey <- liftIO $ C.lookup config "siteKey"
let sk = maybe id (\x s -> s { asSiteKey = x }) siteKey
return $ (pw . rc . rp . lo . sk) defAuthSettings


------------------------------------------------------------------------------
-- |
-- | Initializer for the postgres backend to the auth snaplet. To use this
-- in your application first add this line to your application state:
--
-- > data App = App
-- > { ... -- your own application state here
-- > , _sess :: Snaplet SessionManager
-- > , _db :: Snaplet Postgres
-- > , _auth :: Snaplet (AuthManager App)
-- > }
--
-- Then in your initializer you'll have something like this:
--
-- > d <- nestSnaplet "db" db pgsInit
-- > a <- nestSnaplet "auth" auth $ initPostgresAuth sess d
--
initPostgresAuth
:: Lens b (Snaplet SessionManager) -- ^ Lens to the session snaplet
-> Snaplet Postgres -- ^ The postgres snaplet
-> SnapletInit b (AuthManager b)
initPostgresAuth sess db = makeSnaplet "PostgresAuth" desc Nothing $ do
initPostgresAuth sess db = makeSnaplet "postgresql-auth" desc datadir $ do
config <- getSnapletUserConfig
authTable <- liftIO $ C.lookupDefault "snap_auth_user" config "authTable"
authSettings <- settingsFromConfig
Expand All @@ -81,6 +99,7 @@ initPostgresAuth sess db = makeSnaplet "PostgresAuth" desc Nothing $ do
}
where
desc = "A PostgreSQL backend for user authentication"
datadir = Just $ liftM (++"/resources/auth") getDataDir


------------------------------------------------------------------------------
Expand Down Expand Up @@ -209,7 +228,7 @@ data AuthTable
defAuthTable :: AuthTable
defAuthTable
= AuthTable
{ tblName = "user"
{ tblName = "snap_auth_user"
, colId = "uid"
, colLogin = "login"
, colPassword = "password"
Expand Down
8 changes: 5 additions & 3 deletions src/Snap/Snaplet/PostgresqlSimple.hs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ import Database.PostgreSQL.Simple.QueryParams
import Database.PostgreSQL.Simple.QueryResults
import qualified Database.PostgreSQL.Simple as P
import Snap.Snaplet
import Paths_snaplet_postgresql_simple



------------------------------------------------------------------------------
Expand All @@ -85,7 +87,6 @@ class (MonadCatchIO m, MonadState Postgres m) => HasPostgres m where
getPostgresState :: m Postgres


--logErr :: Monad m => String -> Maybe a -> m (Either String a)
logErr :: MonadIO m
=> t -> IO (Maybe a) -> WriterT [t] m (Maybe a)
logErr err m = do
Expand All @@ -94,9 +95,9 @@ logErr err m = do
return res

------------------------------------------------------------------------------
-- | Initialise the snaplet
-- | Initialize the snaplet
pgsInit :: SnapletInit b Postgres
pgsInit = makeSnaplet "postgresql-simple" description Nothing $ do
pgsInit = makeSnaplet "postgresql-simple" description datadir $ do
config <- getSnapletUserConfig
(mci,errs) <- runWriterT $ do
host <- logErr "Must specify postgres host" $ C.lookup config "host"
Expand All @@ -111,6 +112,7 @@ pgsInit = makeSnaplet "postgresql-simple" description Nothing $ do
return $ Postgres pool Nothing
where
description = "PostgreSQL abstraction"
datadir = Just $ liftM (++"/resources/db") getDataDir


------------------------------------------------------------------------------
Expand Down

0 comments on commit 6fbf493

Please sign in to comment.