Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
seliopou committed Oct 14, 2015
0 parents commit d64ba07
Show file tree
Hide file tree
Showing 20 changed files with 8,351 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.*.sw[pon]
_build/
setup.log
setup.data
*.native
*.byte
*.docdir
2 changes: 2 additions & 0 deletions .merlin
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
S lib
B _build/**
7 changes: 7 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
language: c
install: wget https://raw.githubusercontent.com/ocaml/ocaml-travisci-skeleton/master/.travis-opam.sh
script: bash -ex .travis-opam.sh
sudo: required
env:
- OCAML_VERSION=4.02 PACKAGE=dispatch
- OCAML_VERSION=4.01 PACKAGE=dispatch
30 changes: 30 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
Copyright (c) 2015, Inhabited Type LLC

All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:

1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.

3. Neither the name of the author nor the names of his contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND ANY EXPRESS
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
41 changes: 41 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# OASIS_START
# DO NOT EDIT (digest: a3c674b4239234cbbe53afe090018954)

SETUP = ocaml setup.ml

build: setup.data
$(SETUP) -build $(BUILDFLAGS)

doc: setup.data build
$(SETUP) -doc $(DOCFLAGS)

test: setup.data build
$(SETUP) -test $(TESTFLAGS)

all:
$(SETUP) -all $(ALLFLAGS)

install: setup.data
$(SETUP) -install $(INSTALLFLAGS)

uninstall: setup.data
$(SETUP) -uninstall $(UNINSTALLFLAGS)

reinstall: setup.data
$(SETUP) -reinstall $(REINSTALLFLAGS)

clean:
$(SETUP) -clean $(CLEANFLAGS)

distclean:
$(SETUP) -distclean $(DISTCLEANFLAGS)

setup.data:
$(SETUP) -configure $(CONFIGUREFLAGS)

configure:
$(SETUP) -configure $(CONFIGUREFLAGS)

.PHONY: build doc test all install uninstall reinstall clean distclean configure

# OASIS_STOP
47 changes: 47 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# ocaml-dispatch

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

[![Build Status](https://travis-ci.org/inhabitedtype/ocaml-dispatch.svg?branch=master)](https://travis-ci.org/inhabitedtype/ocaml-dispatch)

## Installation

Install the library and its depenencies via [OPAM][opam]:

[opam]: http://opam.ocaml.org/

```bash
opam install dispatch
```

## Development

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

```bash
opam pin add .
```

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

```bash
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:

```bash
opam install oUnit
./configure --enable-tests
make && make test
```

## License

BSD3, see LICENSE file for its text.
38 changes: 38 additions & 0 deletions _oasis
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
OASISFormat: 0.4
Name: dispatch
Version: 0.1.0
Synopsis: Path-based dispatching for client- and server-side applications.
Description: Dispatch provides a basic mechanism for dispatching a request to a
handler based on a conventionally heirarhical path name found in URIs. It can
be used both for dispatching requests in a server, as well as handing changes
to heirarchical fragments in a client-side application.
Authors: Spiros Eliopoulos <[email protected]>
Maintainers: Spiros Eliopoulos <[email protected]>
Homepage: https://github.com/inhabitedtype/ocaml-dispatch
Copyrights: (C) 2015 Inhabited Type LLC
License: BSD-3-clause
Plugins: META (0.4), DevFiles (0.4)
BuildTools: ocamlbuild

Library dispatch
Path: lib
Modules: Dispatch
BuildDepends: result

Executable test_dispatch
Path: lib_test
MainIs: test_dispatch.ml
Build$: flag(tests)
CompiledObject: best
Install: false
BuildDepends: dispatch, result, oUnit (>= 1.0.2)

Test test_dispatch
Run$: flag(tests)
Command: $test_dispatch
WorkingDirectory: lib_test

SourceRepository master
Type: git
Location: https://github.com/inhabitedtype/ocaml-dispatch.git
Browser: https://github.com/inhabitedtype/ocaml-dispatch
27 changes: 27 additions & 0 deletions _tags
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# OASIS_START
# DO NOT EDIT (digest: 9181962109f15d1f26383b04c21d92ca)
# Ignore VCS directories, you can use the same kind of rule outside
# OASIS_START/STOP if you want to exclude directories that contains
# useless stuff for the build process
true: annot, bin_annot
<**/.svn>: -traverse
<**/.svn>: not_hygienic
".bzr": -traverse
".bzr": not_hygienic
".hg": -traverse
".hg": not_hygienic
".git": -traverse
".git": not_hygienic
"_darcs": -traverse
"_darcs": not_hygienic
# Library dispatch
"lib/dispatch.cmxs": use_dispatch
<lib/*.ml{,i,y}>: pkg_result
# Executable test_dispatch
<lib_test/test_dispatch.{native,byte}>: pkg_oUnit
<lib_test/test_dispatch.{native,byte}>: pkg_result
<lib_test/test_dispatch.{native,byte}>: use_dispatch
<lib_test/*.ml{,i,y}>: pkg_oUnit
<lib_test/*.ml{,i,y}>: pkg_result
<lib_test/*.ml{,i,y}>: use_dispatch
# OASIS_STOP
27 changes: 27 additions & 0 deletions configure
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/bin/sh

# OASIS_START
# DO NOT EDIT (digest: dc86c2ad450f91ca10c931b6045d0499)
set -e

FST=true
for i in "$@"; do
if $FST; then
set --
FST=false
fi

case $i in
--*=*)
ARG=${i%%=*}
VAL=${i##*=}
set -- "$@" "$ARG" "$VAL"
;;
*)
set -- "$@" "$i"
;;
esac
done

ocaml setup.ml -configure "$@"
# OASIS_STOP
13 changes: 13 additions & 0 deletions lib/META
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# OASIS_START
# DO NOT EDIT (digest: a864e4fcc5dc2fd179ba9b68392f7cb0)
version = "0.1.0"
description =
"Path-based dispatching for client- and server-side applications."
requires = "result"
archive(byte) = "dispatch.cma"
archive(byte, plugin) = "dispatch.cma"
archive(native) = "dispatch.cmxa"
archive(native, plugin) = "dispatch.cmxs"
exists_if = "dispatch.cma"
# OASIS_STOP

142 changes: 142 additions & 0 deletions lib/dispatch.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
(*----------------------------------------------------------------------------
Copyright (c) 2015 Inhabited Type LLC.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of the author nor the names of his contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND ANY EXPRESS
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
----------------------------------------------------------------------------*)

open Result

type tag =
[ `Lit | `Var ]

type typ =
[ `Prefix | `Exact ]

type assoc = (string * string) list
type 'a route = (tag * string) list * typ * 'a

let path_split path =
(* NOTE(seliopou): This was implemented manually to minimize dependencies for
* js_of_ocaml. Ain't nobody got time for another regular expression library
* in their browser. *)
let rec loop i acc =
try
let j = String.index_from path i '/' in
loop (j + 1) (String.(sub path i (j - i))::acc)
with Not_found ->
let len = String.length path in
let result =
if i >= len
then acc
else (String.sub path i (len - i))::acc
in
List.rev result
in
if String.get path 0 = '/'
then loop 1 []
else loop 0 []

let to_dsl (ms, typ) =
let start =
String.concat "/" (List.map (function
| (`Lit, x) -> x
| (`Var, x) -> ":" ^ x)
ms)
in
match typ with
| `Exact -> start
| `Prefix -> start ^ "/*"

let of_dsl str =
let star, rev_parts =
match List.rev (path_split str) with
| "*"::ps' -> `Prefix, ps'
| ps' -> `Exact , ps'
in
let parts =
List.fold_left (fun acc p ->
let len = String.length p in
if len > 0 && String.get p 0 = ':' then
(`Var, String.sub p 1 (len - 1))::acc
else
(`Lit, p)::acc)
[] rev_parts
in
parts, star

let path_match ps0 ms0 =
let rec loop ps ms acc =
match ps, ms with
| [] , [] -> `Exact acc
| _ , [] -> `Partial (acc, ps)
| [] , _ -> `Failure (Printf.sprintf
"unmatched pattern suffix: %s" (to_dsl (ms, `Exact)))
| p::ps', (`Lit, l)::ms' ->
if p = l then loop ps' ms' acc else `Failure (Printf.sprintf
"pattern mismatch: expected '%s' but got '%s'" l p)
| p::ps', (`Var, m)::ms' ->
loop ps' ms' ((m, p) :: acc)
in
loop ps0 ms0 []

let dispatch routes path =
let ps0 = path_split path in
let rec loop = function
| [] -> Error "no matching routes found"
| (ms, t, x)::xs ->
begin match t, path_match ps0 ms with
| #typ , `Exact assoc -> Ok(x, assoc, None)
| `Prefix, `Partial(assoc, ps) -> Ok(x, assoc, Some (String.concat "/" ps))
| `Exact , `Partial _ -> loop xs
| _ , `Failure _ -> loop xs
end
in
loop routes

let dispatch_exn routes path =
match dispatch routes path with
| Ok(x, assoc, ps) -> (x, assoc, ps)
| Error msg -> failwith msg

module DSL = struct
type 'a route = string * 'a

let convert routes =
List.map (fun (m, x) ->
let ts, t = of_dsl m in
ts, t, x)
routes

let dispatch routes =
dispatch (convert routes)

let dispatch_exn routes =
dispatch_exn (convert routes)
end
4 changes: 4 additions & 0 deletions lib/dispatch.mldylib
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# OASIS_START
# DO NOT EDIT (digest: 5a628d4cc7426c31799aa35e0fbcf45e)
Dispatch
# OASIS_STOP
Loading

0 comments on commit d64ba07

Please sign in to comment.