Oct 7, 2009

Making Web Application with Haskell #1

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






Today I make the application without any library.
I use standard input and output.


  1. import System.Environment  
  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. getName :: String -> String  
  12. getName q = drop 5 q  
  13.   
  14. main = do   
  15.   putStrLn "Content-type: text/html; charset = UTF-8¥n"  
  16.   putStrLn "<html>"  
  17.   putStrLn "<head><title>Hello</title></head>"  
  18.   putStrLn "<body>"  
  19.   script <- getEnv "SCRIPT_NAME"  
  20.   query <- getEnv "QUERY_STRING"  
  21.   case query of  
  22.     "" -> putStrLn $ inputForm script  
  23.     _  -> putStrLn $ concat ["<h1>Hello, ", getName query, "!</h1>"]  
  24.   putStrLn "</body>"  
  25.   putStrLn "</html>"  



Complete!

No comments: