package ocamlapi

  1. Overview
  2. Docs

Instantiate a Router module with custom default values.

Example using Cohttp_async:

open Cohttp_async

module Custom_config = struct

    let default_exn_handler ?vars:_ exn =
        let status, body =
            match exn with
            | Exceptions.Bad_request msg -> `Bad_request, Body.empty
            | _ -> `Internal_server_error,
                    Body.of_string "An unexpected error occurred" in
        Io.return (Response.make ~status (), body)

    let default_fallback ?vars:_ _req _body =
        Io.return (Response.make ~status:`Not_found (), Body.empty)
end

module Router = Make (Request) (Body) (Response) (Io) (Custom_config)

Parameters

module Body : sig ... end
module IO : sig ... end
module Config : sig ... end

Signature

type callback = ?vars:string Core.String.Table.t -> Req.t -> Body.t -> (Resp.t * Body.t) IO.t

A function of this type is called whenever a request matches a route. The vars argument is a table where the keys are the variable names declared in dynamic path segments, and the values are their values as appearing in the route that this request matched against.

For example, if a request made to the path "/user/david" matches against a route "/user/<name>", then the corresponding callback will be given a vars with a single key "name", and the associated value will be "david".

type exn_handler = ?vars:string Core.String.Table.t -> exn -> (Resp.t * Body.t) IO.t

A function of this type is called whenever a callback throws an exception.

type route = string * (Cohttp.Code.meth * callback) list

Represents a url path with a list of supported http methods.

module type Routes = sig ... end

If a module declares a list of routes named routes, it can be passed as a first-class value to the create_from_modules functions below.

val default_exn_handler : exn_handler

If no exception handler is given while creating a router, default_exn_handler gets used.

val default_fallback : callback

If no fallback callback is given while creating a router, default_fallback gets used.

type t

The type of a request router.

val create : ?exn_handler:exn_handler -> ?fallback_response:callback -> route list -> (t, exn) Core.Result.t

Create a router

val create_exn : ?exn_handler:exn_handler -> ?fallback_response:callback -> route list -> t
val create_from_modules : ?exn_handler:exn_handler -> ?fallback_response:callback -> (module Routes) list -> (t, exn) Core.Result.t

A convenience function to create a router from a list of modules, each of which declare a routes function.

val create_from_modules_exn : ?exn_handler:exn_handler -> ?fallback_response:callback -> (module Routes) list -> t
val dispatch : t -> Req.t -> Body.t -> (Resp.t * Body.t) IO.t

Dispatch a request to the appropriate route. If no matching route is found, fallback_response is called. If an exception is thrown when executing the callback registered to the matching route, exn_handler is called.

OCaml

Innovation. Community. Security.