Extending ServerPartT

31 views
Skip to first unread message

Lambda Dusk

unread,
Apr 1, 2012, 4:58:11 AM4/1/12
to ha...@googlegroups.com
Hi,

I am new to Happstack. Not so new to Haskell, but I have always had a big trouble designing monadic data types. It always seems simple, but I have some kind of mind block there.

I have some server settings (for now, a database connection pool), and I want to put them into a ReaderT monad, that is then wrapped in the ServerPartT. Specifically, I have these two functions:

withMongo :: (MongoPool -> IO a) -> IO ()
withMongo f = do
pool <- dbPool
f pool
killAll pool

runDB :: (MonadIO m) => MongoPool -> Action IO a -> m (Either Failure a)
runDB pool f = liftIO $ do
pipe <- runIOE $ aResource pool
access pipe master dbName f

I would love to change "withMongo" in a way that I can drop it in before my "simpleHTTP" call. The type of runDB could be something like "Action IO a -> ServerPartT m (Either Failure a)" so it can be used in any handler without having to carry the pool with me all the time.

I know this is not exactly a Happstack question, but I'd thought the chances are high someone on here has already done this specifically.

Thanks.

MightyByte

unread,
Apr 1, 2012, 12:43:14 PM4/1/12
to ha...@googlegroups.com
Hi,

We have a solution for exactly this problem in the Snap Framework.
It's called snaplets and it makes it really easy to do what you're
talking about. In fact, there are already two different snaplets
available on hackage that provide MongoDB support, so you probably
wouldn't even need to do it yourself. Snap's low-level API is very
similar to that of Happstack, so you shouldn't have too much trouble
converting if you already have code.

> --
> You received this message because you are subscribed to the Google Groups
> "HAppS" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/happs/-/cAJ2wiO-VgEJ.
> To post to this group, send email to ha...@googlegroups.com.
> To unsubscribe from this group, send email to
> happs+un...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/happs?hl=en.

dag.od...@gmail.com

unread,
Apr 1, 2012, 2:42:47 PM4/1/12
to ha...@googlegroups.com
On 1 April 2012 10:58, Lambda Dusk <lambd...@gmail.com> wrote:
I would love to change "withMongo" in a way that I can drop it in before my "simpleHTTP" call.

import Control.Exception (bracket)
withMongo = bracket dbPool killall

dag.od...@gmail.com

unread,
Apr 1, 2012, 2:54:40 PM4/1/12
to ha...@googlegroups.com
Sorry, I misread your question... Though bracket is still useful here. :)

You want something like this:

runServer :: MongoPool -> IO ()
runServer pool =
    simpleHTTP nullConf $
      mapServerPartT (`runReaderT` pool) handlers

-- all handlers should have this type
type Handler = ServerPartT (ReaderT MongoPool IO) Response

handlers :: Handler
handlers = msum [...]

withMongo :: (MongoPool -> IO ()) -> IO ()

withMongo = bracket dbPool killall

main :: IO ()
main = withMongo runServer

I haven't tested this particular code, but it's similar to what I do in most apps.
Reply all
Reply to author
Forward
0 new messages