changeset 2:2b869930dcfc

add discription for routing
author Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
date Sat, 06 Jul 2013 05:19:32 +0900
parents a69b147cd82e
children 50842df0402f
files haskell.html
diffstat 1 files changed, 62 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/haskell.html	Sat Jul 06 04:28:18 2013 +0900
+++ b/haskell.html	Sat Jul 06 05:19:32 2013 +0900
@@ -473,14 +473,72 @@
 
 			<article>
         <h3>
-          さすがにこれは簡単すぎる…
+          Routing
+        </h3>
+        <p>
+          次に単純な Routing を行う Web Site 実装してみたいと思います。
+        </p>
+        <p>
+          http://localhost:3000/ の後の URL の Path によって 出力する結果を変更してみます。
+        </p>
+        <p>
+          Application が受け取る Request には、clientが送る様々な情報が含まれています。<br>
+          その中には pathInfo という、どこの path へアクセスしてきたかの情報があります。
+        </p>
+			</article>
+
+			<article>
+        <h3>
+          Request に含まれる情報
         </h3>
         <p>
-          こんなの30秒で書けるよという皆様のために、次に単純な Routing っぽいものを実装してみたいと思います。
+          <ul>
+            <li>requestMethod :: Method
+            <li>httpVersion :: HttpVersion
+            <li>rawPathInfo :: ByteString
+            <li>rawQueryString :: ByteString
+            <li>serverName :: ByteString
+            <li>serverPort :: Int
+            <li>requestHeaders :: RequestHeaders
+            <li>isSecure :: Bool
+            <li>remoteHost :: SockAddr
+            <li>pathInfo :: [Text]
+            <li>queryString :: Query
+          </ul>
         </p>
+
+			</article>
+
+			<article class="smaller">
+        <h3>
+          作成するプログラム全容
+        </h3>
         <p>
-          Application が受け取る Request には、様々な情報が含まれています。<br>
-          その中には pathInfo という、どこの path へアクセスしてきたかの情報があります。
+          <a href="https://gist.github.com/amothic/5933808">Source Code</a>
+<pre>
+application request = return $
+    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 = do
+    responseLBS status404 [("Content-type", "text/html")] $ "404 - File Not Found"
+index = do
+    responseLBS status200 [("Content-type", "text/html")] $ "index page"
+hello = do
+    responseLBS status200 [("Content-type", "text/html")] $ "hello, my name is Tom"
+world = do
+    responseLBS status200 [("Content-type", "text/html")] $ "Welcome to Underground"
+
+main = run 3000 application
+</pre>
         </p>
 			</article>