package routes
Install
Dune Dependency
Authors
Maintainers
Sources
sha256=bdb6fd72e5fa778c8fa7bc86503087322353617ee3a80b92484e1a0fa7d892ed
sha512=853eb7aa214e4a0ec429bd55ea9d1f43fcdfc6d9605f2daebf5b71901581b32ba2716a2a147374ab06273c275a9209fa8954996b22077830d9d3079e2b3e9c4d
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.
Published: 01 May 2019
README
Routes
This library will help with adding typed routes to OCaml applications.
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.
The core library in the current state isn't tied to any particular library or framework. It should be usable in the current state, but the idea would be to provide sub-libraries with tighter integration with the remaining ecosystem. Future work would also include working on client side routing for use with js_of_ocaml
libraries like incr_dom or ocaml-vdom.
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>
# 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"
Installation
routes
has not been published on OPAM yet. It can be pinned via opam to use locally:
opam pin add routes git+https://github.com/anuragsoni/routes.git
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.