Hcs

hcs · API reference

HCS: HTTP client/server tools for OCaml 5 and Eio.

HCS provides HTTP servers, HTTP clients, routing, request/response helpers, middleware-style plugs, WebSocket support, and Server-Sent Events support. It intentionally does not own application runtime concerns such as pubsub, workers, supervision, or clustering; use the hive library for those.

A minimal server:

Eio_main.run @@ fun env ->
Eio.Switch.run @@ fun sw ->
let net = Eio.Stdenv.net env in
let clock = Eio.Stdenv.clock env in
let handler _req = Hcs.Response.text "hello\n" in
Hcs.Server.run ~sw ~net ~clock handler

A small routed endpoint:

let router = Hcs.Router.empty () in
Hcs.Router.add_route router ~method_:(Some `GET) ~path:"/"
  ~plugs:Hcs.Pipeline.empty ~handler:(fun _params _req ->
    Hcs.Response.html "<h1>Hello</h1>");

let endpoint =
  Hcs.Endpoint.create Hcs.Endpoint.default_config
  |> Hcs.Endpoint.router router
in
Hcs.Server.run ~sw ~net ~clock (Hcs.Endpoint.to_handler endpoint)

For real-time fanout, combine HCS transports with Hive pubsub:

let bus = Hive.Pubsub.local () in
bus.publish ~topic:"updates" ~payload:"changed"

For clustered fanout, wire Hive_cluster.Pubsub in the application composition root and keep HCS focused on HTTP/SSE/WebSocket I/O.

module Client : sig ... end

High-level HTTP client.

module Server : sig ... end

HTTP server runtime.

module Router : sig ... end

Path router with method matching, parameters, wildcards, route scopes, and per-route plug pipelines.

module Route : sig ... end

Typed, bidirectional routes: build a path once from combinators and use it both to register a handler (which receives typed captures) and to build URLs with Route.link. Compile-time-verified paths that compile down to Router.

module Plug : sig ... end

Request/response middleware.

module Pipeline : sig ... end

Ordered collections of plugs.

module Endpoint : sig ... end

Global request entry point.

module Pool : sig ... end

HTTP client connection-pool support.

module Tls_config : sig ... end

TLS configuration helpers for servers and clients.

module Request : sig ... end

Lazy request accessors.

module Response : sig ... end

HTTP response type and constructors.

module H1_client : sig ... end

HTTP/1.1-specific client.

module H2_client : sig ... end

HTTP/2-specific client.

module Websocket : sig ... end

WebSocket upgrade and frame I/O support.

module Sse : sig ... end

Server-Sent Events response helpers.

module Sse_client : sig ... end

Low-level single-connection SSE client.

module Event_source : sig ... end

High-level reconnecting SSE client.

module Codec : sig ... end

Request/response body codecs.

module Log : sig ... end

Structured request logging helpers.

module Stream : sig ... end

Synchronous and Eio-oriented streaming helpers.

module Multipart : sig ... end

Multipart form-data parsing.

module Http : sig ... end

HTTP request builder DSL.

module Method : sig ... end

HCS HTTP method type and conversion helpers.

module Status : sig ... end

HCS HTTP status type and conversion helpers.

module Headers = Http_core.Headers

Header collection type from the underlying HTTP codec.

module Assigns : sig ... end

Type-safe per-request storage.