webcrank-dispatch: A simple request dispatcher.

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

A simple request dispatcher.


[Skip to Readme]

Modules

[Index]

Downloads

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), hvect (>=0.1), mtl (>=2.0), path-pieces (>=0.1), reroute (>=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>
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-12-10T22:20:28Z
Distributions
Reverse Dependencies 2 direct, 1 indirect [details]
Downloads 1543 total (2 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs uploaded by user
Build status unknown [no reports yet]

Readme for webcrank-dispatch-0.2

[back to package description]

Webcrank Dispatch TravisCI Hackage Dependencies

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.