문제

I'm trying to implement a simple request handler using Happstack:

main :: IO ()
main = simpleHTTP nullConf app

app :: ServerPart Response
app = msum [
             dir "hello" $ method GET  >> helloGet
           , dir "hello" $ method POST >> helloPost
           ]

How can I achieve something similar without repeating the dir "hello"?

This,

app :: ServerPart Response
app = msum [
             dir "hello" $ do
                method GET  >> helloGet
                method POST >> helloPost
           , okResponse home
           ]

will only "fall through" to the default part.

도움이 되었습니까?

해결책

app :: ServerPart Response
app = msum [
             dir "hello" $ (method GET >> helloGet) <|> (method POST >> helloPost)
           , okResponse home
           ]

.. Assuming ServerPart has the appropriate Alternative instance. If it's missing for some reason, you can replace (<|>) with mplus. The main idea here is that you're just nesting one routing combinator inside another.

다른 팁

This is pretty close already:

app :: ServerPart Response
app = msum [
             dir "hello" $ do
                method GET  >> helloGet
                method POST >> helloPost
           , okResponse home
           ]

You just need an extra nested msum:

app :: ServerPart Response
app = msum [
             dir "hello" $
                msum [ method GET  >> helloGet
                     , method POST >> helloPost
                     ]
           , okResponse home
           ]

As someone else suggested you can also use <|> or mplus or other functions related to Alternative and MonadPlus.

  • jeremy
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top