简体   繁体   English

在 Happstack 中使用多个处理程序的语法是什么?

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

Sorry for my basic question, but I'm new to Haskell.抱歉我的基本问题,但我是 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函数,因为我已经在msum中有一个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)

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 . fileServingmyGetDatamsum [fileServing]msum [myGetData]handlers都具有ServerPart Response类型,这是您在mainFunc中传递给simpleHTTP nullConfmainFunc 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). msum此处将处理程序列表合并为一个处理程序(请注意,这也意味着具有单个处理程序的列表中的msum是多余的)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM