MyNixOS website logo
Description

A simple request dispatcher.

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.

Metadata

Version

0.2

Platforms (75)

    Darwin
    FreeBSD 13
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd13
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd13
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows