Giter Club home page Giter Club logo

ocaml-dispatch's Introduction

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.

Build Status

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 alcotest package and reconfigure the build process to enable tests:

opam install alcotest
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 routes = 
    DSL.create 
      [ "/"           , hello_handler
      ; "/hello/:who/", hello_handler
      ] 
  in
  match dispatch routes request.path with
  | Some handler -> handler request
  | None         -> "Not found!"
;;

let _ =
  Server.start handler

License

BSD3, see LICENSE file for its text.

ocaml-dispatch's People

Contributors

anuragsoni avatar avsm avatar seliopou avatar zoggy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

ocaml-dispatch's Issues

Specialized support for cohttp

In a cohttp subpackage, there should be code that sets up a dispatcher to handle Cohttp.Request.t requests. This code could make some assumptions about how HTTP-bases requests are processed and make the library a bit easier apply to that use-case.

Specialized support for js_of_ocaml

In a js_of_ocaml subpackage, there should be code that sets up a dispatcher to handle URI fragment changes. This code should also detect the fragment at startup and dispatch accordingly. Using Lwt_js_events.onhashchanges as a basis for this is probably a good idea.

dispatch.ml isn't a specific enough name

I have a dispatch.ml file in my project, and while trying out webmachine I lost a bunch of time trying to figure out why I was getting this error:

$ make
ocamlbuild -use-ocamlfind -pkgs aws,conduit.mirage,containers,functoria.runtime,js_of_ocaml,js_of_ocaml.ocamlbuild,js_of_ocaml.ppx,lambdasoup,lwt.syntax,magic-mime,mirage-clock-unix,mirage-console.unix,mirage-fs-unix,mirage-http,mirage-net-macosx,mirage-types.lwt,mirage.runtime,nocrypto.lwt,omd,opium,sequence,sexplib,tcpip.arpv4,tcpip.ethif,tcpip.ipv4,tcpip.stack-direct,tcpip.tcp,tcpip.udp,tls.mirage,tyxml,webmachine -tags "syntax(camlp4o),annot,bin_annot,strict_sequence,principal,thread" -tag-line "<static*.*>: -syntax(camlp4o)" -cflag -g -lflags -g,-linkpkg -I liquid -use-menhir -plugin-tag "package(js_of_ocaml.ocamlbuild)" main.native
Warning: option -plugin-tag(s) has no effect in absence of plugin file "myocamlbuild.ml"
+ ocamlfind ocamlc -c -g -annot -bin-annot -safe-string -short-paths -principal -keep-locs -thread -strict-sequence -w +A-4-6-9-40-42-44-48 -package webmachine -package tyxml -package tls.mirage -package tcpip.udp -package tcpip.tcp -package tcpip.stack-direct -package tcpip.ipv4 -package tcpip.ethif -package tcpip.arpv4 -package sexplib -package sequence -package opium -package omd -package nocrypto.lwt -package mirage.runtime -package mirage-types.lwt -package mirage-net-macosx -package mirage-http -package mirage-fs-unix -package mirage-console.unix -package mirage-clock-unix -package magic-mime -package lwt.syntax -package lambdasoup -package js_of_ocaml.ppx -package js_of_ocaml.ocamlbuild -package js_of_ocaml -package functoria.runtime -package containers -package conduit.mirage -package aws -syntax camlp4o -I liquid -o key_gen.cmo key_gen.ml
findlib: [WARNING] Interface dispatch.cmi occurs in several directories: ., /Users/s/.opam/riseos/lib/dispatch
+ ocamlfind ocamlc -c -g -annot -bin-annot -principal -thread -strict-sequence -package webmachine -package tyxml -package tls.mirage -package tcpip.udp -package tcpip.tcp -package tcpip.stack-direct -package tcpip.ipv4 -package tcpip.ethif -package tcpip.arpv4 -package sexplib -package sequence -package opium -package omd -package nocrypto.lwt -package mirage.runtime -package mirage-types.lwt -package mirage-net-macosx -package mirage-http -package mirage-fs-unix -package mirage-console.unix -package mirage-clock-unix -package magic-mime -package lwt.syntax -package lambdasoup -package js_of_ocaml.ppx -package js_of_ocaml.ocamlbuild -package js_of_ocaml -package functoria.runtime -package containers -package conduit.mirage -package aws -syntax camlp4o -I liquid -o main.cmo main.ml
findlib: [WARNING] Interface dispatch.cmi occurs in several directories: ., /Users/s/.opam/riseos/lib/dispatch
+ ocamlfind ocamlc -c -g -annot -bin-annot -safe-string -short-paths -principal -keep-locs -thread -strict-sequence -w +A-4-6-9-40-42-44-48 -package webmachine -package tyxml -package tls.mirage -package tcpip.udp -package tcpip.tcp -package tcpip.stack-direct -package tcpip.ipv4 -package tcpip.ethif -package tcpip.arpv4 -package sexplib -package sequence -package opium -package omd -package nocrypto.lwt -package mirage.runtime -package mirage-types.lwt -package mirage-net-macosx -package mirage-http -package mirage-fs-unix -package mirage-console.unix -package mirage-clock-unix -package magic-mime -package lwt.syntax -package lambdasoup -package js_of_ocaml.ppx -package js_of_ocaml.ocamlbuild -package js_of_ocaml -package functoria.runtime -package containers -package conduit.mirage -package aws -syntax camlp4o -I liquid -o liquid/liquid.cmo liquid/liquid.ml
findlib: [WARNING] Interface dispatch.cmi occurs in several directories: ., /Users/s/.opam/riseos/lib/dispatch
+ ocamlfind ocamlopt -c -g -annot -bin-annot -safe-string -short-paths -principal -keep-locs -thread -strict-sequence -w +A-4-6-9-40-42-44-48 -package webmachine -package tyxml -package tls.mirage -package tcpip.udp -package tcpip.tcp -package tcpip.stack-direct -package tcpip.ipv4 -package tcpip.ethif -package tcpip.arpv4 -package sexplib -package sequence -package opium -package omd -package nocrypto.lwt -package mirage.runtime -package mirage-types.lwt -package mirage-net-macosx -package mirage-http -package mirage-fs-unix -package mirage-console.unix -package mirage-clock-unix -package magic-mime -package lwt.syntax -package lambdasoup -package js_of_ocaml.ppx -package js_of_ocaml.ocamlbuild -package js_of_ocaml -package functoria.runtime -package containers -package conduit.mirage -package aws -syntax camlp4o -I liquid -o key_gen.cmx key_gen.ml
findlib: [WARNING] Interface dispatch.cmi occurs in several directories: ., /Users/s/.opam/riseos/lib/dispatch
+ ocamlfind ocamlopt -c -g -annot -bin-annot -safe-string -short-paths -principal -keep-locs -thread -strict-sequence -w +A-4-6-9-40-42-44-48 -package webmachine -package tyxml -package tls.mirage -package tcpip.udp -package tcpip.tcp -package tcpip.stack-direct -package tcpip.ipv4 -package tcpip.ethif -package tcpip.arpv4 -package sexplib -package sequence -package opium -package omd -package nocrypto.lwt -package mirage.runtime -package mirage-types.lwt -package mirage-net-macosx -package mirage-http -package mirage-fs-unix -package mirage-console.unix -package mirage-clock-unix -package magic-mime -package lwt.syntax -package lambdasoup -package js_of_ocaml.ppx -package js_of_ocaml.ocamlbuild -package js_of_ocaml -package functoria.runtime -package containers -package conduit.mirage -package aws -syntax camlp4o -I liquid -o liquid/liquid.cmx liquid/liquid.ml
findlib: [WARNING] Interface dispatch.cmi occurs in several directories: ., /Users/s/.opam/riseos/lib/dispatch
+ ocamlfind ocamlopt -c -g -annot -bin-annot -safe-string -short-paths -principal -keep-locs -thread -strict-sequence -w +A-4-6-9-40-42-44-48 -package webmachine -package tyxml -package tls.mirage -package tcpip.udp -package tcpip.tcp -package tcpip.stack-direct -package tcpip.ipv4 -package tcpip.ethif -package tcpip.arpv4 -package sexplib -package sequence -package opium -package omd -package nocrypto.lwt -package mirage.runtime -package mirage-types.lwt -package mirage-net-macosx -package mirage-http -package mirage-fs-unix -package mirage-console.unix -package mirage-clock-unix -package magic-mime -package lwt.syntax -package lambdasoup -package js_of_ocaml.ppx -package js_of_ocaml.ocamlbuild -package js_of_ocaml -package functoria.runtime -package containers -package conduit.mirage -package aws -syntax camlp4o -I liquid -o dispatch.cmx dispatch.ml
findlib: [WARNING] Interface dispatch.cmi occurs in several directories: ., /Users/s/.opam/riseos/lib/dispatch
File "dispatch.ml", line 354, characters 2-55:
Warning 7: the method allowed_methods is overridden.
File "dispatch.ml", line 1:
Error: The files /Users/s/.opam/riseos/lib/webmachine/webmachine.cmi
       and dispatch.cmi make inconsistent assumptions over interface Dispatch
Command exited with code 2.
Compilation unsuccessful after building 12 targets (0 cached) in 00:00:01.
make: *** [main.native] Error 10

Turns out that because there's a dispatch.ml here and one in my project, they can't coexist.

A fuller name for this project would be nice since dispatch is likely to be used in lots of other places too.

Self contained example

Can you provide a self contained example using the dispatch.js package?

EDIT: Nvm, figured it out.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.