package dispatch

  1. Overview
  2. Docs
Path-based dispatching for client- and server-side applications

Install

Dune Dependency

Authors

Maintainers

Sources

0.4.1.tar.gz
sha256=26dc203146dd956d9fe53f1ee934411550ca9e6ad343ec9787ca53897c406354
md5=646dc67647f12db38bce7a761a75b3bc

Description

ocaml-dispatch provides a basic mechanism for dispatching a request to a handler based on hierarchical path names conventionally found in URIs. It can be used both for dispatching requests in a server, as well as handing changes to hierarchical fragments in a client-side application.

Published: 10 Jul 2019

README

ocaml-dispatch

ocaml-dispatch provides a basic mechanism for dispatching a request to a handler based on hierarchical path names conventionally found in URIs. It can be used both for dispatching requests in a server, as well as handing changes to hierarchical fragments in a client-side application.

Installation

Install the library and its depenencies via OPAM:

opam install dispatch

Development

To install development versions of the library, pin the package from the root of the repository:

opam pin add .

You can install the latest changes by commiting them to the local git repository and running:

opam upgrade dispatch

For building and running the tests during development, you will need to install the oUnit package and reconfigure the build process to enable tests:

opam install oUnit
dune runtest

Usage

Dispatch is designed to work with whatever sort of handler you care to use, whether it's synchronous, Lwt-based, or Async-based. Here's a simple example of using the Dispatch.DSL module to setup routing for a "Hello, World!" server. The example assumes a Server module and request type, and that handlers should return strings that will be interpreted as the body of the response.

open Dispatch

let hello_handler keys rest request =
  let who = try List.assoc "who" keys with Not_found -> "World" in
  Printf.sprintf "Hello, %s!" who
;;

let handler request =
  let table = [
      "/"           , hello_handler
    ; "/hello/:who/", hello_handler
  ] in
  match DSL.dispatch table request.path with
  | Result.Ok handler -> handler request
  | Result.Error _    -> "Not found!"
;;

let _ =
  Server.start handler

License

BSD3, see LICENSE file for its text.

Dependencies (3)

  1. result
  2. dune >= "1.0"
  3. ocaml >= "4.03.0"

Dev Dependencies (1)

  1. alcotest with-test

Used by (2)

  1. dispatch-js
  2. webmachine >= "0.2.3" & < "0.7.0"

Conflicts

None

OCaml

Innovation. Community. Security.