# HG changeset patch # User Daichi TOMA # Date 1373090697 -32400 # Node ID bf2da4395b5fee690a090ae7f93e3867ec398624 # Parent 95fa8bea3364315d41fd93534aa1a95e744862b9 add example diff -r 95fa8bea3364 -r bf2da4395b5f prog/example.hs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/prog/example.hs Sat Jul 06 15:04:57 2013 +0900 @@ -0,0 +1,43 @@ +{-# LANGUAGE OverloadedStrings #-} +import Network.Wai +import Network.HTTP.Types (status200, status404) +import Network.Wai.Handler.Warp (run) +import Control.Monad.Trans (lift) +import Data.IORef (newIORef, atomicModifyIORef) +import Data.ByteString.Lazy.UTF8 (fromString) + +application counter request = function counter + where + function = routes $ pathInfo request + +routes path = findRoute path routeSetting + +findRoute path [] = notFound +findRoute path ((p,f):xs) + | path == p = f + | otherwise = findRoute path xs + +routeSetting = [([], index), + (["hello"], hello), + (["welcome","world"],world)] + +notFound _ = return $ + responseLBS status404 [("Content-type", "text/html")] $ "404 - File Not Found" + +index _ = return $ + responseLBS status200 [("Content-type", "text/html")] $ "index page" + +hello _ = return $ + responseLBS status200 [("Content-type", "text/html")] $ "hello, my name is Tom" + +world counter = do + count <- lift $ incCount counter + return $ responseLBS status200 [("Content-type", "text/html")] $ + fromString $ show count + +incCount counter = atomicModifyIORef counter (\c -> (c+1, c)) + +main = do + counter <- newIORef 0 + run 3000 $ application counter + diff -r 95fa8bea3364 -r bf2da4395b5f prog/routes.hs --- a/prog/routes.hs Sat Jul 06 15:04:02 2013 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,45 +0,0 @@ -{-# LANGUAGE OverloadedStrings #-} -import Network.Wai -import Network.HTTP.Types (status200, status404) -import Network.Wai.Handler.Warp (run) -import Control.Monad.Trans (lift) -import Data.IORef (newIORef, atomicModifyIORef) -import Data.ByteString.Lazy.UTF8 (fromString) - -application counter request = - let - function = routes $ pathInfo request - in - function counter - -routes path = findRoute path routeSetting - -findRoute path [] = notFound -findRoute path ((p,f):xs) - | path == p = f - | otherwise = findRoute path xs - -routeSetting = [([], index), - (["hello"], hello), - (["welcome","world"],world)] - -notFound _ = return $ - responseLBS status404 [("Content-type", "text/html")] $ "404 - File Not Found" - -index _ = return $ - responseLBS status200 [("Content-type", "text/html")] $ "index page" - -hello _ = return $ - responseLBS status200 [("Content-type", "text/html")] $ "hello, my name is Tom" - -world counter = do - count <- lift $ incCount counter - return $ responseLBS status200 [("Content-type", "text/html")] $ - fromString $ show count - -incCount counter = atomicModifyIORef counter (\c -> (c+1, c)) - -main = do - counter <- newIORef 0 - run 3000 $ application counter -