The webcrank-dispatch package

[ Tags: bsd3, library, web ] [ Propose Tags ]

A simple request dispatcher.


[Skip to Readme]

Properties

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 <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 repository head: git clone https://github.com/webcrank/webcrank-dispatch.hs.git
Uploaded Thu Dec 10 22:20:28 UTC 2015 by purefn
Distributions NixOS:0.2
Downloads 326 total (3 in the last 30 days)
Rating (no votes yet) [estimated by rule of succession]
Your Rating
  • λ
  • λ
  • λ
Status Docs uploaded by user
Build status unknown [no reports yet]
Hackage Matrix CI

Modules

[Index]

Downloads

Maintainer's Corner

For package maintainers and hackage trustees


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.