I have just published an initial release for a path based routing library. The library has minimal dependencies and isn’t tied to any particular HTTP or UI framework.
It will offer the user to declare routes via the provided combinators, that in-turn extract and forward the parameters to a request handler. Since the route definitions are just simple functions, it should be straightforward to reuse and compose smaller routing pieces to construct the final routing list.
Example usage can be like below:
module Request = struct type t ... end module Response = struct type t ... end let get_user (id: int) () = ... let search_user (name: string) (city : string) () = ... let routes = let open Routes in [ s "" ==> fun () - ... (* matches the index route "/" *) ; (method' (Some `GET)) (s "user" </> int) ==> get_user (* matches "/user/<int>" *) ; method' None </> (s "user" </> str </> str) ==> search_user (* matches "/user/<str>/<str>" *) ] match Routes.match' routes ~target:"/some/url" ~meth:`GET = | None -> (* No route matched. Alternative could be to provide default routes *) | Some r -> (* Match found. Do something further with handler response *)```
Git repository: https://github.com/anuragsoni/routes
Example using Httpaf: https://github.com/anuragsoni/routes/blob/master/example/main.ml
This is not published on
opam yet so it can be pinned locally via:
opam pin add routes git+https://github.com/anuragsoni/routes.git