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 <- loo开发者_如何学编程k "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:
Explicitly call outmethodM GET
rather than just have it be the fallthough.Pull out the redundant
ok $ toResponse
and have that only in one place.Preferably have the POST return HTML as well.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 ++ ...
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
精彩评论