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.


  1. import Network.CGI  
  2.   
  3. inputForm :: String -> String  
  4. inputForm script = concat [  
  5.   "<form action=\"", script, "\" method=\"GET\">",  
  6.   "<p>Who are you?</p>",  
  7.   "<input type=\"text\" name=\"name\" />",  
  8.   "<input type=\"submit\" value=\"send\" />",  
  9.   "</form>"]  
  10.   
  11. putName :: String -> String  
  12. putName name = concat ["<h1>Hello, ", name, "!</h1>"]  
  13.   
  14. body :: Maybe String -> String -> String  
  15. body name script =   
  16.   case name of   
  17.     Just x  -> putName x  
  18.     Nothing -> inputForm script  
  19.   
  20. html :: Maybe String -> String -> String  
  21. html name script = concat [  
  22.   "<html>",  
  23.   "<head><title>Hello</title></head>",  
  24.   "<body>",  
  25.   body name script,  
  26.   "</body>",  
  27.   "</html>"]  
  28.   
  29. cgiMain :: CGI CGIResult  
  30. cgiMain = do  
  31.   setHeader "Content-type" "text/html; charset = UTF-8"  
  32.   script <- scriptName  
  33.   name <- getInput "name"  
  34.   output $ html name script  
  35.   
  36. main :: IO ()  
  37. main = runCGI (handleErrors cgiMain)  



Complete!

No comments: