Question

I'm trying to get the GET and the POST from the Happstack tutorial into one handler function so it's always together, which I've kind of achieved, but it seems ugly.

login :: ServerPart Response
login = msum [ 
    do methodM POST
       user <- look "user"
       pass <- look "pass"
       success <- query $ CheckPassword user pass
       ok $ toResponse (user ++ ", " ++ pass ++ ": " ++ (if success then "Valid" else "Invalid")),
    ok $ toResponse $ html $ do
      B.head $ do
        title "Login Form"
      B.body $ do
        form ! enctype "multipart/form-data" ! B.method "POST" $ do
             B.label "user: " >> input ! type_ "text" ! name "user" ! size "10"
             B.label "pass: " >> input ! type_ "text" ! name "pass" ! size "10"
             input ! type_ "submit" ! name "upload"]

Things I'd like to change are:

  1. Explicitly call out methodM GET rather than just have it be the fallthough.

  2. Pull out the redundant ok $ toResponse and have that only in one place.

  3. Preferably have the POST return HTML as well.

  4. Anything else that looks "off" to anyone with more experience. Any ideas?

UPDATE: figured out #1; adding do methodM GET above the ok $ toResponse $ ... works fine, but the thing for newbies like me to note is that must line up vertically, i.e., the m in methodM needs to be directly above the o in ok. Hopefully this saves someone hours of frustration.

UPDATE 2: #3 was fairly easy -- just update the last line of the POST to be ok $ toResponse $ html $ do B.body $ toHtml $ user ++ ...

Was it helpful?

Solution

Look up formlets (they work with Happstack) and/or digestive-functors (which work with Snap and maybe even Happstack):

http://hackage.haskell.org/package/formlets

http://hackage.haskell.org/package/digestive-functors

I haven't investigated how digestive-functors are better than formlets but it's newer package and might be simpler than the old one.

There are some examples

There's even a library in F# that compiles to JavaScript and does similar thing on client side. It allowes checking things like login availability from JS while still being written in nice formlet/functional style. It's called WebSharper: WebSharper

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top