Combining GET and POST components in Happstack route filters

I am 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 like this without repeating dir "hello" ?

it

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

will only β€œfail” to the default part.

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

.. Assuming ServerPart has a corresponding Alternative instance. If for some reason it is missing, you can replace (<|>) with mplus . The basic idea here is that you simply insert one routing combiner inside another.

+2
source

This is pretty close:

 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 ] 

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

  • Jeremy
+1
source

Source: https://habr.com/ru/post/1488587/


All Articles