Oct 9, 2009

Making Web Application with Haskell #2

I make a web application with Haskell.
It's a simple application.
You input your name, then the application output your name.
See below.






The other day I made the application without any library.
Today I make it with Network.CGI wich is included in GHC.



import Network.CGI

inputForm :: String -> String
inputForm script = concat [
"<form action=\"", script, "\" method=\"GET\">",
"<p>Who are you?</p>",
"<input type=\"text\" name=\"name\" />",
"<input type=\"submit\" value=\"send\" />",
"</form>"]

putName :: String -> String
putName name = concat ["<h1>Hello, ", name, "!</h1>"]

body :: Maybe String -> String -> String
body name script =
case name of
Just x -> putName x
Nothing -> inputForm script

html :: Maybe String -> String -> String
html name script = concat [
"<html>",
"<head><title>Hello</title></head>",
"<body>",
body name script,
"</body>",
"</html>"]

cgiMain :: CGI CGIResult
cgiMain = do
setHeader "Content-type" "text/html; charset = UTF-8"
script <- scriptName
name <- getInput "name"
output $ html name script

main :: IO ()
main = runCGI (handleErrors cgiMain)



Complete!

No comments: