Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Extending ServerPartT
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  4 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Lambda Dusk  
View profile  
 More options Apr 1 2012, 4:58 am
From: Lambda Dusk <lambdad...@gmail.com>
Date: Sun, 1 Apr 2012 01:58:11 -0700 (PDT)
Local: Sun, Apr 1 2012 4:58 am
Subject: Extending ServerPartT

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.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
MightyByte  
View profile  
 More options Apr 1 2012, 12:43 pm
From: MightyByte <mightyb...@gmail.com>
Date: Sun, 1 Apr 2012 12:43:14 -0400
Local: Sun, Apr 1 2012 12:43 pm
Subject: Re: Extending ServerPartT
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 must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
dag.odenhall@gmail.com  
View profile  
 More options Apr 1 2012, 2:42 pm
From: "dag.odenh...@gmail.com" <dag.odenh...@gmail.com>
Date: Sun, 1 Apr 2012 20:42:47 +0200
Local: Sun, Apr 1 2012 2:42 pm
Subject: Re: Extending ServerPartT

On 1 April 2012 10:58, Lambda Dusk <lambdad...@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

<goog_1591544991>
http://hackage.haskell.org/packages/archive/base/4.5.0.0/doc/html/Con...


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
dag.odenhall@gmail.com  
View profile  
 More options Apr 1 2012, 2:54 pm
From: "dag.odenh...@gmail.com" <dag.odenh...@gmail.com>
Date: Sun, 1 Apr 2012 20:54:40 +0200
Local: Sun, Apr 1 2012 2:54 pm
Subject: Re: Extending ServerPartT

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.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »