@@ -1,4 +1,3 @@
1−<!-- Generated from README.lhs, do not modify! -->
21WAI: Web Application Interface
32==============================
43
@@ -7,25 +6,27 @@ Getting started
76
87You want a minimal example? Here it is!
98
10− {-# LANGUAGE OverloadedStrings #-}
11− import Network.Wai
12− import Network.HTTP.Types
13− import Network.Wai.Handler.Warp (run)
14− import Data.ByteString.Lazy.Char8 () -- Just for an orphan instance
15− import Control.Monad.IO.Class (liftIO)
16−
17− app :: Application
18− app _ = do
19− liftIO $ putStrLn "I've done some IO here"
20− return $ responseLBS
21− status200
22− [("Content-Type", "text/plain")]
23− "Hello, Web!"
24−
25− main :: IO ()
26− main = do
27− putStrLn $ "http://localhost:8080/"
28− run 8080 app
9+~~~ {.haskell .literate}
10+{-# LANGUAGE OverloadedStrings #-}
11+import Network.Wai
12+import Network.HTTP.Types
13+import Network.Wai.Handler.Warp (run)
14+import Data.ByteString.Lazy.Char8 () -- Just for an orphan instance
15+import Control.Monad.IO.Class (liftIO)
16+
17+app :: Application
18+app _ = do
19+ liftIO $ putStrLn "I've done some IO here"
20+ return $ responseLBS
21+ status200
22+ [("Content-Type", "text/plain")]
23+ "Hello, Web!"
24+
25+main :: IO ()
26+main = do
27+ putStrLn $ "http://localhost:8080/"
28+ run 8080 app
29+~~~
2930
3031Put that code into a file named _hello.hs_ and install [wai] and [warp] from Hackage:
3132
@@ -49,14 +50,16 @@ We can modify our previous example to serve static content. For this create a fi
4950
5051Now we redefine `responseBody` to refer to that file:
5152
52− app2 :: Application
53− app2 _ = return index
53+~~~ {.haskell .literate}
54+app2 :: Application
55+app2 _ = return index
5456
55− index = ResponseFile
56− status200
57− [("Content-Type", "text/html")]
58− "index.html"
59− Nothing
57+index = ResponseFile
58+ status200
59+ [("Content-Type", "text/html")]
60+ "index.html"
61+ Nothing
62+~~~
6063
6164
6265Basic dispatching
@@ -69,22 +72,24 @@ An `Application` maps `Request`s to `Response`s:
6972
7073Depending on the path info provided with each `Request` we can serve different `Response`s:
7174
72− app3 :: Application
73− app3 request = case rawPathInfo request of
74− "/" -> return index
75− "/raw/" -> return plainIndex
76− _ -> return notFound
75+~~~ {.haskell .literate}
76+app3 :: Application
77+app3 request = case rawPathInfo request of
78+ "/" -> return index
79+ "/raw/" -> return plainIndex
80+ _ -> return notFound
7781
78− plainIndex = ResponseFile
79− status200
80− [("Content-Type", "text/plain")]
81− "index.html"
82− Nothing
82+plainIndex = ResponseFile
83+ status200
84+ [("Content-Type", "text/plain")]
85+ "index.html"
86+ Nothing
8387
84− notFound = responseLBS
85− status404
86− [("Content-Type", "text/plain")]
87− "404 - Not Found"
88+notFound = responseLBS
89+ status404
90+ [("Content-Type", "text/plain")]
91+ "404 - Not Found"
92+~~~
8893
8994
9095Doing without overloaded strings
@@ -92,14 +97,16 @@ Doing without overloaded strings
9297
9398For the sake of efficiency, WAI uses the [bytestring] package. We used GHCs [overloaded strings] to almost hide this fact. But we can easily do without. What follows is a more verbose definition of `notFound`, that works without GHC extensions:
9499
95− import qualified Data.ByteString.Char8 as B8
96− import qualified Data.ByteString.Lazy.Char8 as LB8
97− import Data.CaseInsensitive (mk)
100+~~~ {.haskell}
101+import qualified Data.ByteString.Char8 as B8
102+import qualified Data.ByteString.Lazy.Char8 as LB8
103+import Data.CaseInsensitive (mk)
98104
99− notFound = responseLBS
100− status404
101− [(mk $ B8.pack "Content-Type", B8.pack "text/plain")]
102− (LB8.pack "404 - Not Found")
105+notFound = responseLBS
106+ status404
107+ [(mk $ B8.pack "Content-Type", B8.pack "text/plain")]
108+ (LB8.pack "404 - Not Found")
109+~~~
103110
104111
105112 [wai]: http://hackage.haskell.org/package/wai