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_handlertype request = Server.requesttype response = Server.responsetype handler = request -> responsetype t = handler -> handlerval identity : tval compose : t -> t -> tval compose_all : t list -> tval (@>) : t -> t -> tval apply : ('a -> 'b) -> 'a -> 'bval run : t list -> handler -> handlermodule Logger : sig ... endmodule Request_id : sig ... endmodule Head : sig ... endmodule Timeout : sig ... endmodule Recover : sig ... endmodule Cors : sig ... endmodule Rate_limit : sig ... endmodule Etag : sig ... endmodule Cache_control : sig ... endmodule Static : sig ... endStatic file serving plug.
module Compress : sig ... endResponse compression plug.
module Circuit_breaker : sig ... endmodule Retry : sig ... endRetry plug with configurable backoff.
module Basic_auth : sig ... endBasic HTTP authentication plug.
module Csrf : sig ... endCSRF protection plug.
module Negotiate : sig ... endContent negotiation plug.
module Token : sig ... endSigned and encrypted tokens for authentication.
module Session : sig ... endCookie-based session management with fiber-local isolation.