1WAI: Web Application Interface
2==============================
7You want a minimal example? Here it is!
9> {-# LANGUAGE OverloadedStrings #-}
11> import Network.HTTP.Types
12> import Network.Wai.Handler.Warp (run)
13> import Data.ByteString.Lazy.Char8 () -- Just for an orphan instance
14> import Control.Monad.IO.Class (liftIO)
18> liftIO $ putStrLn "I've done some IO here"
21> [("Content-Type", "text/plain")]
26> putStrLn $ "http://localhost:8080/"
29Put that code into a file named _hello.hs_ and install [wai] and
32 cabal install wai warp
40 http://localhost:8080/
46We can modify our previous example to serve static content. For this
47create a file named _index.html_:
51Now we redefine `responseBody` to refer to that file:
54> app2 _ = return index
58> [("Content-Type", "text/html")]
66An `Application` maps `Request`s to `Response`s:
68 ghci> :info Application
69 type Application = Request -> IO Response
71Depending on the path info provided with each `Request` we can serve
75> app3 request = case rawPathInfo request of
77> "/raw/" -> return plainIndex
80> plainIndex = ResponseFile
82> [("Content-Type", "text/plain")]
86> notFound = responseLBS
88> [("Content-Type", "text/plain")]
92Doing without overloaded strings
93--------------------------------
95For the sake of efficiency, WAI uses the [bytestring] package. We used
96GHCs [overloaded strings] to almost hide this fact. But we can easily do
97without. What follows is a more verbose definition of `notFound`, that
98works without GHC extensions:
100 import qualified Data.ByteString.Char8 as B8
101 import qualified Data.ByteString.Lazy.Char8 as LB8
102 import Data.CaseInsensitive (mk)
104 notFound = responseLBS
106 [(mk $ B8.pack "Content-Type", B8.pack "text/plain")]
107 (LB8.pack "404 - Not Found")
110 [wai]: http://hackage.haskell.org/package/wai
111 [warp]: http://hackage.haskell.org/package/warp
112 [overloaded strings]: http://www.haskell.org/ghc/docs/latest/html/users_guide/type-class-extensions.html#overloaded-strings
113 [bytestring]: http://hackage.haskell.org/package/bytestring
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list