packages feed

Spock 0.4.2.0 → 0.4.2.1

raw patch · 3 files changed

+39/−14 lines, 3 files

Files

Spock.cabal view
@@ -1,5 +1,5 @@ name:                Spock-version:             0.4.2.0+version:             0.4.2.1 synopsis:            Another Haskell web toolkit based on scotty description:         This toolbox provides everything you need to get a quick start into web hacking with haskell: sessions, database helper, authentication and the power of scotty Homepage:            https://github.com/agrafix/Spock
Web/Spock.hs view
@@ -7,9 +7,9 @@     ( -- * Spock's core       spock, SpockM, SpockAction       -- * Database-    , runQuery, PoolOrConn (..), ConnBuilder (..), PoolCfg (..)-      -- * State-    , getState+    , PoolOrConn (..), ConnBuilder (..), PoolCfg (..)+      -- * Accessing Database and State+    , HasSpock     -- * Authorization     , SessionCfg (..)     , authedUser, unauthCurrent@@ -27,6 +27,8 @@     , status, addHeader, setHeader, redirect     , text, html, file, json, source, raw     , raise, rescue, next+      -- * Internals for extending Spock+    , getSpockHeart, runSpockIO, WebStateM, WebState     ) where 
Web/Spock/Monad.hs view
@@ -1,12 +1,14 @@ {-# LANGUAGE GeneralizedNewtypeDeriving #-} {-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE UndecidableInstances #-}+{-# LANGUAGE MultiParamTypeClasses #-} module Web.Spock.Monad where  import Web.Spock.Types  import Control.Monad import Control.Monad.Reader+import Control.Monad.Trans.Resource import Data.Pool import Data.Time.Clock ( UTCTime(..) ) import Web.Scotty.Trans@@ -19,12 +21,24 @@ webM :: MonadTrans t => WebStateM conn sess st a -> t (WebStateM conn sess st) a webM = lift --- | Give you access to a database connectin from the connection pool. The connection is--- released back to the pool once the function terminates.-runQuery :: MonadTrans t-         => (conn -> IO a) -> t (WebStateM conn sess st) a-runQuery query =-    webM $+class HasSpock conn st m where+    -- | Give you access to a database connectin from the connection pool. The connection is+    -- released back to the pool once the function terminates.+    runQuery :: (conn -> IO a) -> m a+    -- | Read the application's state. If you wish to have mutable state, you could+    -- use a 'TVar' from the STM packge.+    getState :: m st++instance MonadTrans t => HasSpock conn st (t (WebStateM conn sess st)) where+    runQuery a = webM $ runQueryImpl a+    getState = webM $ getStateImpl++instance HasSpock conn st (WebStateM conn sess st) where+    runQuery = runQueryImpl+    getState = getStateImpl++runQueryImpl :: (conn -> IO a) -> WebStateM conn sess st a+runQueryImpl query =     do pool <- asks web_dbConn        let fun =                case pool of@@ -34,10 +48,19 @@                      CP.withResource p        liftIO (fun query) --- | Read the application's state. If you wish to have mutable state, you could--- use a 'TVar' from the STM packge.-getState :: MonadTrans t => t (WebStateM conn sess st) st-getState = webM $ asks web_state+getStateImpl :: WebStateM conn sess st st+getStateImpl = asks web_state++-- | Read the heart of Spock. This is useful if you want to construct your own+-- monads that work with runQuery and getState using "runSpockIO"+getSpockHeart :: MonadTrans t => t (WebStateM conn sess st) (WebState conn sess st)+getSpockHeart = webM ask++-- | Run an action inside of Spocks core monad. This allows you to use runQuery and getState+runSpockIO :: WebState conn sess st -> WebStateM conn sess st a -> IO a+runSpockIO st (WebStateM action) =+    runResourceT $+    runReaderT action st  getSessMgr :: MonadTrans t => t (WebStateM conn sess st) (SessionManager sess) getSessMgr = webM $ asks web_sessionMgr