Hcs.Plug

hcs · API reference

Request/response middleware.

A plug wraps a handler and can inspect or rewrite the request, short-circuit with a response, add headers, enforce security checks, recover exceptions, or perform cross-cutting behavior. Built-in plugs include sessions, CSRF, CORS, compression, static files, request IDs, logging, rate limiting, retry, circuit breaking, and authentication helpers.

Plug-based middleware composition.

A plug is a function that wraps a handler, allowing pre/post processing of requests and responses.

Usage

open Hcs

let pipeline =
  Plug.Logger.create ~clock logger
  @> Plug.Compress.create ()
  @> Plug.Timeout.create ~clock 30.0
  @> Plug.identity

let handler = Plug.apply pipeline my_handler
type request = Server.request
type response = Server.response
type handler = request -> response
type t = handler -> handler
val identity : t
val compose : t -> t -> t
val compose_all : t list -> t
val (@>) : t -> t -> t
val apply : ('a -> 'b) -> 'a -> 'b
val run : t list -> handler -> handler
module Logger : sig ... end
module Request_id : sig ... end
module Head : sig ... end
module Timeout : sig ... end
module Recover : sig ... end
module Cors : sig ... end
module Rate_limit : sig ... end
module Etag : sig ... end
module Cache_control : sig ... end
module Static : sig ... end

Static file serving plug.

module Compress : sig ... end

Response compression plug.

module Circuit_breaker : sig ... end
module Retry : sig ... end

Retry plug with configurable backoff.

module Basic_auth : sig ... end

Basic HTTP authentication plug.

module Csrf : sig ... end

CSRF protection plug.

module Negotiate : sig ... end

Content negotiation plug.

module Token : sig ... end

Signed and encrypted tokens for authentication.

module Session : sig ... end

Cookie-based session management with fiber-local isolation.