package routes

  1. Overview
  2. Docs
Typed routing for OCaml applications

Install

Dune Dependency

Authors

Maintainers

Sources

routes-0.6.0.tbz
sha256=ca9e19a6cc3c7b0a107ba65ef698080aab0e3f8193b4a98e4b8695b2a7dfd27b
sha512=a40e78e065dd2857d9df250951e240ba3dd6ed147e60ef5590a7dd3af5bc4a9bc2126b7e44ce381e93e4c318fb2e1a30657b2e2d509d89fffcbcd31731896c2a

Description

routes provides combinators for adding typed routing to OCaml applications. The core library will be independent of any particular web framework or runtime. It does path based dispatch from a target url to a user provided handler.

Tags

router http

Published: 20 Dec 2019

README

Routes  

This library will help with adding typed routes to OCaml applications. The goal is to have a easy to use portable library with reasonable performance See benchmark folder.

Users can create a list of routes, and handler function to work on the extracted entities using the combinators provided by the library. To perform URL matching one would just need to forward the URL's path and query to the matcher.

Example
# #require "routes";;
# open Routes;;
# open Infix;;
# type req = {target: string};;
type req = { target : string; }

# let idx (_ : req) = "root";;
val idx : req -> string = <fun>

# let get_user (id: int) (req : req) = Printf.sprintf "Received request from %s to fetch id: %d" req.target id
val get_user : int -> req -> string = <fun>

# let search_user (name: string) (city : string) (_req : req) = "search for user";;
val search_user : string -> string -> req -> string = <fun>

# let routes =
  with_method [ `GET, idx <$ s "" (* matches the index route "/" *)
  ; `GET, get_user <$> s "user" *> int (* matches "/user/<int>" *)
  ; `POST, search_user <$> s "user" *> str </> str (*  matches "/user/<str>/<str>" *)
  ]
val routes : (req -> string) router = <abstr>

# pp_router Format.str_formatter routes
- : unit = ()

# print_endline (Format.flush_str_formatter ())
Routes:
> GET
> GET user/<int>
> POST user/<string>/<string>

- : unit = ()

# let req = { target = "/user/12" };;
val req : req = {target = "/user/12"}

# match Routes.match_with_method routes ~target:"/some/url" ~meth:`GET with None -> "No match" | Some r -> r req;;
- : string = "No match"

# match Routes.match_with_method routes ~target:req.target ~meth:`GET with None -> "No match" | Some r -> r req;;
- : string = "Received request from /user/12 to fetch id: 12"

It is possible to define custom patterns that can be used for route matching.

# type shape = Point | Circle
type shape = Point | Circle

# let shape_of_string = function "point" -> Some Point | "circle" -> Some Circle | _ -> None
val shape_of_string : string -> shape option = <fun>

# let shape_to_string = function Point -> "point" | Circle -> "circle"
val shape_to_string : shape -> string = <fun>

# let shape = pattern shape_of_string "<shape>"
val shape : shape t = <abstr>

# let process_shape (s : shape) = shape_to_string s
val process_shape : shape -> string = <fun>

# let route = one_of [ process_shape <$> s "shape" *> shape ]
val route : string router = <abstr>

# match' route "/shape/circle"
- : string option = Some "circle"

# match' route "/shape/point"
- : string option = Some "point"

# pattern_of_route (s "shape" *> shape)
- : string = "shape/<shape>"

Installation

To use the version published on opam:
opam install routes
For development version:
opam pin add routes git+https://github.com/anuragsoni/routes.git

Example use inside other libraries:

Related Work

The combinators are influenced by Rudi Grinberg's wonderful blogpost about type safe routing done via an EDSL using GADTs + an interpreted for the DSL.

Also thanks to Gabriel Radanne for feedback and for the blog post showing the technique used in printf like functions.

Dependencies (2)

  1. dune >= "1.7"
  2. ocaml >= "4.05"

Dev Dependencies (2)

  1. mdx with-test & < "2.0"
  2. alcotest with-test

Used by

None

Conflicts

None

OCaml

Innovation. Community. Security.