webcrank-dispatch-0.2: A simple request dispatcher.

Copyright(C) 2015 Richard Wallace
LicenseBSD-style (see the file LICENSE)
MaintainerRichard Wallace <rwallace@thewallacepack.net>
Stabilityprovisional
Safe HaskellNone
LanguageHaskell98

Webcrank.Dispatch

Contents

Description

 

Synopsis

Paths

Building Paths

root :: Path `[]` Source

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

(</>) :: Path as -> Path bs -> Path (Append as bs) Source

Other routes can be built with /:

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

param :: (Typeable a, PathPiece a) => Path (a : `[]`) Source

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.

data Path as :: [*] -> *

Instances

(~) [*] a ([] *) => IsString (Path a) 

Rendering Paths

renderPath :: Path l -> HVect l -> [Text] Source

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.

params :: HBuild' `[]` r => r Source

class HBuild' l r where Source

Methods

hBuild' :: HVect l -> r Source

Instances

(~) [*] l' (ReverseLoop l ([] *)) => HBuild' l (HVect l') Source 
HBuild' ((:) * a l) r => HBuild' l (a -> r) Source 

Dispatching

(==>) :: Path as -> HVectElim as a -> Dispatcher a infixr 8 Source

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
  ]

dispatch :: Dispatcher a -> [Text] -> Maybe a Source

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