Passing a MongoDB connection to Servant

Viewed 120

I'm having a little bit of trouble using Servant with the MongoDB library. I am pretty new to Haskell and Servant, so I suspect my technique is wrong. I have two endpoints one to fetch a [Event] and one to create a new Event. So the two signatures are:

handleNewEvent :: Event -> Handler PostResult
handleListEvents :: Handler [Event]

Similar to examples in Postgres, I have added a database connection to my server. By running something like this:

db :: MonadIO m => Action m a -> m a
let db = access pipe master dbName

run port $ serve api $ server db

So now I can create a server function:

server :: MonadIO m => (Action m a -> m a) -> Server API                              
server runAction = handleFindEvents runAction :<|> handleNewEvent runAction

My problem is I need the runAction function to be Action IO () -> IO () in handleNewEvent but Action IO [Event] -> IO [Event] in handleListEvent. I suspect I'm approaching the problem from the wrong perspective (this would be how I would tackle the problem in another language). How is this meant to work using Servant? For reference here are my handler functions. I hardcoded the list handler to ensure the code compiles and runs.

newEvent :: DBContext -> Event -> Handler PostResult           
newEvent context event = do                                    
    liftIO $ context $ insert_ "events" $ toBson event         
    return $ PostResult { message = "success" , success = True}
                                                               
findEvents :: DBContext  -> Handler [Event]                    
findEvents run = return [Event "example" "event"]                  
0 Answers
Related