The webcrank-dispatch package

[Tags:bsd3, library]

A simple request dispatcher.

[Skip to Readme]


Versions 0.1, 0.2
Dependencies base (>=4.6 && <5), hvect (>=0.1), mtl (>=2.0), path-pieces (>=0.1), reroute (>=0.3), text (>=0.11), unordered-containers (>=0.2) [details]
License BSD3
Copyright (c) 2015 Richard Wallace
Author Richard Wallace <>
Maintainer Richard Wallace <>
Stability Unknown
Category Web
Home page
Bug tracker
Source repository head: git clone
Uploaded Thu Dec 10 22:20:28 UTC 2015 by purefn
Distributions NixOS:0.2
Downloads 286 total (3 in the last 30 days)
0 []
Status Docs uploaded by user
Build status unknown [no reports yet]




Maintainer's Corner

For package maintainers and hackage trustees

Readme for webcrank-dispatch

Readme for webcrank-dispatch-0.2

Webcrank Dispatch TravisCI Hackage Dependencies

A type-safe request dispatcher and path renderer.

{-# LANGUAGE DataKinds #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeOperators #-}

import Webcrank.Dispatch


Building Paths

The simplest Path is root, which is equivalent to /.

Other routes can be built with </>:

docsPath = "package" \<\/> "webcrank-dispatch-0.1" \<\/> "docs"

Paths can contain parameters. To create a parameterized path, use param as a path component:

docsPath :: Path '[String]
docsPath = "package" </> param </> "docs"

Paths can contain as many parameters of varying types as needed:

wat :: Path '[String, Int, Bool, Int, String]
wat :: "this" </> param </> param </> "crazyness" </> param </> "ends" </> param </> param

Path parameters can be of any type that have instances for Typeable and PathPiece.

Rendering Paths

Paths can be rendered using renderPath and params.

>>> renderPath root params
>>> renderPath docsPath $ params "webcrank-dispatch-0.1"
["package", "webcrank-dispatch-0.1", "docs"]
>>> renderPath wat $ params "down is up" 42 False 7 "up is down"
["this", "down is up", "42", "crazyness", "False", "ends", "7", "up is down"]

Note in the last example that no encoding is done by @renderPath@.


An elementary Dispatcher can be built using ==>.

disp = root ==> \"Dispatched\"

Dispatchers form a Monoid, so more interesting dispatchers can be built with <> or mconcat.

disp = mconcat
  [ root ==> "Welcome!"
  , "echo" </> param ==> id

Dispatching requests is done with dispatch. It turns a Dispatcher into a function from a list of decoded path components to a possible handler.

>>> dispatch (root ==> "Welcome!") [""]
Just "Welcome!"
>>> dispatch (root ==> "Welcome!") ["echo", "Goodbye!"]
>>> dispatch (root ==> "Welcome!" <> "echo" </> param ==> id) ["echo", "Goodbye!"]
Just "Goodbye!"

For more examples see examples/Main.hs.