2

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 ++ ...

Dax Fohl
  • 10,654
  • 6
  • 46
  • 90
  • Are you just looking for stylistic suggestions? If so, I'd suggest naming the two cases, even if only in a `where` clause. They are certainly beyond the level of complexity where a locally defined name is more descriptive than their code. – Chris Smith Aug 22 '11 at 15:42

1 Answers1

2

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

Matvey Aksenov
  • 3,802
  • 3
  • 23
  • 45
  • You're talking about Resharper? I was looking into that a while back but ended up using GWT instead. (And then the project got canceled regardless and I ended up using neither). – Dax Fohl Aug 22 '11 at 22:40
  • digestive-functors is a cleaner reimplementation of formlets. The formlets library is deprecated and no longer maintained. digestive-functors has many improvements such as the ability for the ` – stepcut Aug 24 '11 at 21:11
  • Having labels, inline errors and all of that is really nice. Those were exactly the things that made me write my own things on top of formlets' ones. – Marek Sieradzki Aug 27 '11 at 21:06