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.



import System.Environment

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>"]

getName :: String -> String
getName q = drop 5 q

main = do
putStrLn "Content-type: text/html; charset = UTF-8¥n"
putStrLn "<html>"
putStrLn "<head><title>Hello</title></head>"
putStrLn "<body>"
script <- getEnv "SCRIPT_NAME"
query <- getEnv "QUERY_STRING"
case query of
"" -> putStrLn $ inputForm script
_ -> putStrLn $ concat ["<h1>Hello, ", getName query, "!</h1>"]
putStrLn "</body>"
putStrLn "</html>"



Complete!

No comments: