wai-routes: Typesafe URLs for Wai applications.

[ library, mit, network ] [ Propose Tags ]

Provides easy to use typesafe URLs for Wai Applications.

Sample usage follows (See examples/Example.hs in the source bundle for the full code) -

{-# 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 (MyRoute dbref) request = ...
-- Delete Single User
postUserDeleteR :: Int -> Handler MyRoute
postUserDeleteR userid (MyRoute dbref) request = ...

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

[Skip to Readme]

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

  • No Candidates
Versions [RSS] 0.1, 0.2, 0.2.1, 0.2.2, 0.2.3, 0.2.4, 0.3.0, 0.3.1, 0.3.2, 0.3.3, 0.3.4, 0.4.0, 0.4.1, 0.5.0, 0.5.1, 0.6.0, 0.6.1, 0.6.2, 0.7.0, 0.7.1, 0.7.2, 0.7.3, 0.8.0, 0.8.1, 0.9.0, 0.9.1, 0.9.2, 0.9.3, 0.9.4, 0.9.5, 0.9.6, 0.9.7, 0.9.8, 0.9.9, 0.9.10, 0.10.0, 0.10.1, 0.10.2, 0.10.3, 0.10.4
Dependencies aeson (>=0.7.0.1 && <0.8), base (>=4.6.0.1 && <4.7), blaze-builder (>=0.3.3.2 && <0.4), bytestring (>=0.10.0.2 && <0.11), http-types (>=0.8.3 && <0.9), mtl (>=2.1.2 && <2.2), template-haskell (>=2.8.0.0 && <2.9), text (>=0.11.3.1 && <0.12), wai (>=2.0.0 && <2.2), yesod-routes (>=1.2.0.6 && <1.3) [details]
License MIT
Author Anupam Jain
Maintainer ajnsit@gmail.com
Category Network
Home page https://github.com/ajnsit/wai-routes
Source repo head: git clone http://github.com/ajnsit/wai-routes
this: git clone http://github.com/ajnsit/wai-routes/tree/v0.5.0(tag v0.5.0)
Uploaded by AnupamJain at 2014-04-02T05:14:45Z
Distributions
Reverse Dependencies 2 direct, 0 indirect [details]
Downloads 26135 total (44 in the last 30 days)
Rating 2.0 (votes: 1) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs available [build log]
Successful builds reported [all 1 reports]

Readme for wai-routes-0.5.0

[back to package description]

Wai Routes (wai-routes-0.5.0)

Build Status

This package provides typesafe URLs for Wai applications.

Features:

  • Automatic generation of Route boilerplate using TH
  • Easy Nested Routes
  • Sitewide Master datatype which is passed to all handlers and can be used for persistent data (like DB connections)
  • RouteM monad that makes it easy to compose an application with multiple routes and middleware.
  • HandlerM monad that makes it easy to build a Handler with access to Request data and Master datatype
  • Handlers can abort processing and pass control to the next application in the wai stack

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

  • 0.1 : Intial release
  • 0.2 : Updated functionality based on yesod-routes package
  • 0.2.1 : Changed license to MIT
  • 0.2.2 : Fixed license information in hs and cabal files
  • 0.2.3 : Implemented a better showRoute function. Added blaze-builder as a dependency
  • 0.2.4 : Put an upper bound on yesod-routes version as 1.2 breaks API compatibility
  • 0.3.0 : yesod-routes 1.2 compatibility. Abstracted request data. Created runNext which skips to the next app in the wai stack
  • 0.3.1 : Removed internal 'App' synonym which only muddied the types. Added common content types for convenience.
  • 0.3.2 : Added HandlerM Monad which makes it easier to build Handlers
  • 0.3.3 : Better exports from the Network.Wai.Middleware.Routes module
  • 0.3.4 : Added 'liftResourceT' to lift a ResourceT into HandlerM
  • 0.4.0 : Wai 2 compatibility. Replaced 'liftResourceT' with 'lift'
  • 0.4.1 : showRoute now returns "/" instead of ""
  • 0.5.0 : Added raw,text,html,json helpers. Update to wai-2.1.