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 handlerA 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 ... endHigh-level HTTP client.
module Server : sig ... endHTTP server runtime.
module Router : sig ... endPath router with method matching, parameters, wildcards, route scopes, and per-route plug pipelines.
module Route : sig ... endTyped, 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 ... endRequest/response middleware.
module Pipeline : sig ... endOrdered collections of plugs.
module Endpoint : sig ... endGlobal request entry point.
module Pool : sig ... endHTTP client connection-pool support.
module Tls_config : sig ... endTLS configuration helpers for servers and clients.
module Request : sig ... endLazy request accessors.
module Response : sig ... endHTTP response type and constructors.
module H1_client : sig ... endHTTP/1.1-specific client.
module H2_client : sig ... endHTTP/2-specific client.
module Websocket : sig ... endWebSocket upgrade and frame I/O support.
module Sse : sig ... endServer-Sent Events response helpers.
module Sse_client : sig ... endLow-level single-connection SSE client.
module Event_source : sig ... endHigh-level reconnecting SSE client.
module Codec : sig ... endRequest/response body codecs.
module Log : sig ... endStructured request logging helpers.
module Stream : sig ... endSynchronous and Eio-oriented streaming helpers.
module Multipart : sig ... endMultipart form-data parsing.
module Http : sig ... endHTTP request builder DSL.
module Method : sig ... endHCS HTTP method type and conversion helpers.
module Status : sig ... endHCS HTTP status type and conversion helpers.
module Headers = Http_core.HeadersHeader collection type from the underlying HTTP codec.
module Assigns : sig ... endType-safe per-request storage.