Search code examples
haskellhappstackhdbc

What's the syntax of using multiple handlers in Happstack?


Sorry for my basic question, but I'm new to Haskell.

I'm following this example to receive some values from a request body, but my server also serves static files from a directory using the following code:

fileServing :: ServerPart Response
fileServing = serveDirectory EnableBrowsing ["index.html"] "./Path/"

mainFunc = simpleHTTP nullConf $ msum [ 
                                        fileServing                                     
                                      ]

I added the below code to my library but I'm not sure where to use the handlers function, because I already have an msum in the mainFunc.

handlers :: ServerPart Response
handlers =
    do decodeBody myPolicy
       msum [ 
               myGetData
            ]

myGetData :: ServerPart Response
myGetData =
    do method POST
       username <- look "username"
       password <- look "password"
       ok $ toResponse (username ++ ", " ++ password)

Solution

  • fileServing, myGetData, msum [fileServing], msum [myGetData] and handlers all have ServerPart Response type, which is the type of what you are passing to simpleHTTP nullConf in mainFunc. That being so, you probably want...

    mainFunc = simpleHTTP nullConf handlers
    
    -- etc.
    
    handlers :: ServerPart Response
    handlers =
        do decodeBody myPolicy
           msum [ fileServing
                , myGetData
                ]
    

    msum here combines a list of handlers into a single handler (note that also means msum on a list with a single handler is redundant).