sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>
On This Page
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)
module Req : Cohttp.S.Request
module Body : sig ... end
module Resp : Cohttp.S.Response
module IO : sig ... end
module Config : sig ... end
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.
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