Readme for wai-routes-0.5.1

Wai Routes (wai-routes-0.5.1)

Build Status

This package provides typesafe URLs for Wai applications.

Features:

It depends on yesod-routes package for the TH functionality (but not the rest of yesod). The aim is to provide a similar level of typesafe URL functionality to Wai applications as is available to Yesod applications.

Example Usage

The following builds a simple JSON service (using Aeson for JSON conversion)

{-# LANGUAGE OverloadedStrings, TypeFamilies #-}

import Network.Wai
import Network.Wai.Middleware.Routes

import Data.IORef

-- The Site Argument
data MyRoute = MyRoute (IORef DB)

-- Generate Routes
mkRoute MyRoute [parseRoutes|
/             UsersR         GET
/user/#Int    UserR:
  /              UserRootR   GET
  /delete        UserDeleteR POST
|]

-- Define Handlers
-- All Users Page
getUsersR :: Handler MyRoute
getUsersR (MyRoute dbref) request = ...
-- Single User Page
getUserRootR :: Int -> Handler MyRoute
getUserRootR userid = ...
-- Delete Single User
postUserDeleteR :: Int -> Handler MyRoute
postUserDeleteR userid = ...

-- Define Application using RouteM Monad
myApp = do
  db <- liftIO $ newIORef mydb
  route (MyRoute db)
  setDefaultAction $ staticApp $ defaultFileServerSettings "static"

-- Run the application
main :: IO ()
main = toWaiApp myApp >>= run 8080

Changelog