wai: Web Application Interface.

[ library, mit, web ] [ Propose Tags ] [ Report a vulnerability ]

API docs and the README are available at http://www.stackage.org/package/wai.


[Skip to Readme]

Downloads

Note: This package has metadata revisions in the cabal description newer than included in the tarball. To unpack the package including the revisions, use 'cabal get'.

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

  • No Candidates
Versions [RSS] 0.0.0, 0.0.1, 0.1.0, 0.2.0, 0.2.1, 0.2.2, 0.2.2.1, 0.3.0, 0.3.1, 0.3.2, 0.4.0, 0.4.1, 0.4.2, 0.4.3, 1.0.0, 1.1.0, 1.1.0.1, 1.2.0, 1.2.0.1, 1.2.0.2, 1.2.0.3, 1.3.0, 1.3.0.1, 1.3.0.2, 1.3.0.3, 1.4.0, 1.4.0.1, 1.4.0.2, 1.4.1, 2.0.0, 2.1.0, 2.1.0.1, 2.1.0.2, 2.1.0.3, 3.0.0, 3.0.0.1, 3.0.0.2, 3.0.1, 3.0.1.1, 3.0.2, 3.0.2.1, 3.0.2.2, 3.0.2.3, 3.0.3.0, 3.0.4.0, 3.0.5.0, 3.2.0, 3.2.0.1, 3.2.1, 3.2.1.1, 3.2.1.2, 3.2.2, 3.2.2.1, 3.2.3, 3.2.4
Change log ChangeLog.md
Dependencies base (>=4 && <5), blaze-builder (>=0.4 && <0.5), bytestring (>=0.10), bytestring-builder (>=0.10.4.0 && <0.10.7), http-types (>=0.7), network (>=2.2.1.5), text (>=0.7), transformers (>=0.0), unix-compat (>=0.2), vault (>=0.3 && <0.4) [details]
License MIT
Author Michael Snoyman
Maintainer michael@snoyman.com
Revised Revision 2 made by HerbertValerioRiedel at 2015-10-05T22:07:48Z
Category Web
Home page https://github.com/yesodweb/wai
Source repo head: git clone git://github.com/yesodweb/wai.git
head: git clone git://github.com/yesodweb/wai.git
Uploaded by KazuYamamoto at 2015-09-29T02:12:52Z
Distributions Arch:3.2.4, Debian:3.2.2.1, Fedora:3.2.3, FreeBSD:3.0.3.0, LTSHaskell:3.2.4, NixOS:3.2.4, Stackage:3.2.4, openSUSE:3.2.4
Reverse Dependencies 464 direct, 3311 indirect [details]
Downloads 173247 total (508 in the last 30 days)
Rating 2.5 (votes: 5) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs available [build log]
Last success reported on 2015-09-30 [all 1 reports]

Readme for wai-3.0.4.0

[back to package description]

WAI: Web Application Interface

Getting started

You want a minimal example? Here it is!

{-# LANGUAGE OverloadedStrings #-}
import Network.Wai
import Network.HTTP.Types
import Network.Wai.Handler.Warp (run)

app :: Application
app _ respond = do
    putStrLn "I've done some IO here"
    respond $ responseLBS
        status200
        [("Content-Type", "text/plain")]
        "Hello, Web!"

main :: IO ()
main = do
    putStrLn $ "http://localhost:8080/"
    run 8080 app

Put that code into a file named hello.hs and install wai and warp from Hackage:

cabal install wai warp

Run it:

runhaskell hello.hs

Point your browser to:

http://localhost:8080/

Serving static content

We can modify our previous example to serve static content. For this create a file named index.html:

<p>Hello, Web!</p>

Now we redefine responseBody to refer to that file:

app2 :: Application
app2 _ respond = respond index

index :: Response
index = responseFile
    status200
    [("Content-Type", "text/html")]
    "index.html"
    Nothing

Basic dispatching

An Application maps Requests to Responses:

ghci> :info  Application
type Application = Request -> IO Response

Depending on the path info provided with each Request we can serve different Responses:

app3 :: Application
app3 request respond = respond $ case rawPathInfo request of
    "/"     -> index
    "/raw/" -> plainIndex
    _       -> notFound

plainIndex :: Response
plainIndex = responseFile
    status200
    [("Content-Type", "text/plain")]
    "index.html"
    Nothing

notFound :: Response
notFound = responseLBS
    status404
    [("Content-Type", "text/plain")]
    "404 - Not Found"

Doing without overloaded strings

For 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:

import qualified Data.ByteString.Char8 as B8
import qualified Data.ByteString.Lazy.Char8 as LB8
import           Data.CaseInsensitive (mk)

notFound = responseLBS
    status404
    [(mk $ B8.pack "Content-Type", B8.pack "text/plain")]
    (LB8.pack "404 - Not Found")