@@ -1,4 +1,3 @@
1−<!-- Generated from README.lhs, do not modify! -->
2WAI: Web Application Interface
1WAI: Web Application Interface
3==============================
2==============================
@@ -7,25 +6,27 @@ Getting started
8You want a minimal example? Here it is!
7You want a minimal example? Here it is!
9+~~~ {.haskell .literate}
10− {-# LANGUAGE OverloadedStrings #-}
10+{-# LANGUAGE OverloadedStrings #-}
11− import Network.Wai
11+import Network.Wai
12− import Network.HTTP.Types
12+import Network.HTTP.Types
13− import Network.Wai.Handler.Warp (run)
13+import Network.Wai.Handler.Warp (run)
14− import Data.ByteString.Lazy.Char8 () -- Just for an orphan instance
14+import Data.ByteString.Lazy.Char8 () -- Just for an orphan instance
15− import Control.Monad.IO.Class (liftIO)
15+import Control.Monad.IO.Class (liftIO)
17− app :: Application
17+app :: Application
18− app _ = do
18+app _ = do
19− liftIO $ putStrLn "I've done some IO here"
19+ liftIO $ putStrLn "I've done some IO here"
20− return $ responseLBS
20+ return $ responseLBS
21− status200
21+ status200
22− [("Content-Type", "text/plain")]
22+ [("Content-Type", "text/plain")]
23− "Hello, Web!"
23+ "Hello, Web!"
25− main :: IO ()
25+main :: IO ()
26− main = do
26+main = do
27− putStrLn $ "http://localhost:8080/"
27+ putStrLn $ "http://localhost:8080/"
28− run 8080 app
28+ run 8080 app
30Put that code into a file named _hello.hs_ and install [wai] and [warp] from Hackage:
31Put that code into a file named _hello.hs_ and install [wai] and [warp] from Hackage:
@@ -49,14 +50,16 @@ We can modify our previous example to serve static content. For this create a fi
50Now we redefine `responseBody` to refer to that file:
51Now we redefine `responseBody` to refer to that file:
53+~~~ {.haskell .literate}
52− app2 :: Application
54+app2 :: Application
53− app2 _ = return index
55+app2 _ = return index
55− index = ResponseFile
57+index = ResponseFile
56− status200
58+ status200
57− [("Content-Type", "text/html")]
59+ [("Content-Type", "text/html")]
58− "index.html"
60+ "index.html"
62Basic dispatching
65Basic dispatching
@@ -69,22 +72,24 @@ An `Application` maps `Request`s to `Response`s:
70Depending on the path info provided with each `Request` we can serve different `Response`s:
73Depending on the path info provided with each `Request` we can serve different `Response`s:
75+~~~ {.haskell .literate}
72− app3 :: Application
76+app3 :: Application
73− app3 request = case rawPathInfo request of
77+app3 request = case rawPathInfo request of
74− "/" -> return index
78+ "/" -> return index
75− "/raw/" -> return plainIndex
79+ "/raw/" -> return plainIndex
76− _ -> return notFound
80+ _ -> return notFound
78− plainIndex = ResponseFile
82+plainIndex = ResponseFile
79− status200
83+ status200
80− [("Content-Type", "text/plain")]
84+ [("Content-Type", "text/plain")]
81− "index.html"
85+ "index.html"
84− notFound = responseLBS
88+notFound = responseLBS
85− status404
89+ status404
86− [("Content-Type", "text/plain")]
90+ [("Content-Type", "text/plain")]
87− "404 - Not Found"
91+ "404 - Not Found"
90Doing without overloaded strings
95Doing without overloaded strings
@@ -92,14 +97,16 @@ Doing without overloaded strings
93For 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:
98For 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:
95− import qualified Data.ByteString.Char8 as B8
101+import qualified Data.ByteString.Char8 as B8
96− import qualified Data.ByteString.Lazy.Char8 as LB8
102+import qualified Data.ByteString.Lazy.Char8 as LB8
97− import Data.CaseInsensitive (mk)
103+import Data.CaseInsensitive (mk)
99− notFound = responseLBS
105+notFound = responseLBS
100− status404
106+ status404
101− [(mk $ B8.pack "Content-Type", B8.pack "text/plain")]
107+ [(mk $ B8.pack "Content-Type", B8.pack "text/plain")]
102− (LB8.pack "404 - Not Found")
108+ (LB8.pack "404 - Not Found")
105 [wai]: http://hackage.haskell.org/package/wai
112 [wai]: http://hackage.haskell.org/package/wai