webcrank-dispatch: A simple request dispatcher.

[ bsd3, library, web ] [ Propose Tags ]

A simple request dispatcher.


[Skip to Readme]

Modules

[Index]

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.1, 0.2
Dependencies base (>=4.6 && <5), bytestring (>=0.10), mtl (>=2.0), path-pieces (>=0.1), reroute (>=0.1 && <0.3), text (>=0.11), unordered-containers (>=0.2) [details]
License BSD-3-Clause
Copyright (c) 2015 Richard Wallace
Author Richard Wallace <rwallace@thewallacepack.net>
Maintainer Richard Wallace <rwallace@thewallacepack.net>
Revised Revision 1 made by purefn at 2015-07-27T01:57:20Z
Category Web
Home page https://github.com/webcrank/webcrank-dispatch.hs
Bug tracker https://github.com/webcrank/webcrank-dispatch.hs/issues
Source repo head: git clone https://github.com/webcrank/webcrank-dispatch.hs.git
Uploaded by purefn at 2015-03-06T22:15:20Z
Distributions
Reverse Dependencies 2 direct, 1 indirect [details]
Downloads 1529 total (5 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs available [build log]
Last success reported on 2015-03-07 [all 1 reports]

Readme for webcrank-dispatch-0.1

[back to package description]

Travis Build Status

A type-safe request dispatcher and path renderer.

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

import Webcrank.Dispatch

Paths

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

Dispatching

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!"]
Nothing
>>> dispatch (root ==> "Welcome!" <> "echo" </> param ==> id) ["echo", "Goodbye!"]
Just "Goodbye!"

For more examples see examples/Main.hs.