changeset 8:3ee3f33bc368

fix
author Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
date Sat, 06 Jul 2013 12:38:28 +0900
parents eea79db7cd9e
children 95fa8bea3364
files prog/counter.hs prog/routes.hs
diffstat 2 files changed, 21 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/prog/counter.hs	Sat Jul 06 11:53:05 2013 +0900
+++ b/prog/counter.hs	Sat Jul 06 12:38:28 2013 +0900
@@ -2,7 +2,7 @@
 import Network.Wai
 import Network.HTTP.Types (status200)
 import Network.Wai.Handler.Warp (run)
-import Control.Monad.Trans (liftIO, lift)
+import Control.Monad.Trans (lift)
 import Data.IORef (newIORef, atomicModifyIORef)
 import Data.ByteString.Lazy.UTF8 (fromString)
 
--- a/prog/routes.hs	Sat Jul 06 11:53:05 2013 +0900
+++ b/prog/routes.hs	Sat Jul 06 12:38:28 2013 +0900
@@ -2,9 +2,15 @@
 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 request = return $
-    routes $ pathInfo request
+application counter request = 
+    let
+      function = routes $ pathInfo request
+    in
+      function counter 
 
 routes path = findRoute path routeSetting
 
@@ -17,17 +23,23 @@
                 (["hello"],          hello),
                 (["welcome","world"],world)]
 
-notFound = 
+notFound _ = return $
     responseLBS status404 [("Content-type", "text/html")] $ "404 - File Not Found"
 
-index =
+index _ = return $
     responseLBS status200 [("Content-type", "text/html")] $ "index page"
 
-hello =
+hello _ = return $
     responseLBS status200 [("Content-type", "text/html")] $ "hello, my name is Tom"
 
-world =
-    responseLBS status200 [("Content-type", "text/html")] $ "Welcome to Underground"
+world counter = do
+    count <- lift $ incCount counter
+    return $ responseLBS status200 [("Content-type", "text/html")] $ 
+        fromString $ show count
 
-main = run 3000 application
+incCount counter = atomicModifyIORef counter (\c -> (c+1, c))
 
+main = do 
+  counter <- newIORef 0
+  run 3000 $ application counter
+