package mehari

  1. Overview
  2. Docs

Module type containing all environment-dependent functions.

module IO : sig ... end
type addr

Type for IP address.

type handler = addr request -> response IO.t

Handlers are asynchronous functions from Mehari.request to Mehari.response.

type route

Routes tell router which handler to select for each request. See Routing.

type rate_limiter

Rate limiter. See Rate limit.

type middleware = handler -> handler

Middlewares take a handler, and run some code before or after — producing a “bigger” handler. See Middleware.

type clock

System clock used by rate limiter.

Middleware

val no_middleware : middleware

Does nothing but call its inner handler. Useful for disabling middleware conditionally during application startup:

if development then
  my_middleware
else
  Mehari.no_middleware
val pipeline : middleware list -> middleware

Combines a list of middlewares into one, such that these two lines are equivalent: Mehari.pipeline [ mw1 ; mw2 ] @@ handler mw1 @@ mw2 @@ handler.

Routing

val router : route list -> handler

Creates a router. If none of the routes match the Mehari.request, the router returns Mehari.not_found.

val route : ?rate_limit:rate_limiter -> ?mw:middleware -> ?regex:bool -> string -> handler -> route

route ~rate_limit ~mw ~regex path handler forwards requests for path to handler. path can be a string literal or a regex in Perl style depending of value of regex. If rate limit is in effect, handler is not executed and a respond with Mehari.status Mehari.slow_down is sended.

val scope : ?rate_limit:rate_limiter -> ?mw:middleware -> string -> route list -> route

scope ~rate_limit ~mw prefix routes groups routes under the path prefix, rate_limit and mw.

val no_route : route

A dummy value of type route that is completely ignored by the router. Useful for disabling routes conditionally during application start.

Rate limit

val make_rate_limit : clock -> ?period:int -> int -> [ `Second | `Minute | `Hour | `Day ] -> rate_limiter

make_rate_limit clock ~period n unit creates a rate_limiter which limits client to n request per period * unit. For example,

make_rate_limit ~period:2 5 `Hour

limits client to 5 requests every 2 hours.

Virtual hosting

val virtual_hosts : ?meth:[ `ByURL | `SNI ] -> (string * handler) list -> handler

virtual_hosts ?meth [(domain, handler); ...] produces a handler which enables virtual hosting at the TLS-layer using SNI.

  • meth can be used to choose which source to match the hostnames against. Defaults to `SNI.

Logging

val set_log_lvl : Logs.level -> unit

Set Mehari's logger to the given log level.

val logger : clock -> middleware

Logs and times requests. Time spent logging is included.

val debug : 'a Logs.log
val info : 'a Logs.log
val warning : 'a Logs.log
val error : 'a Logs.log
OCaml

Innovation. Community. Security.