Eio HTTP server with static file serving and route handlers
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

OCaml 95.1%
Dune 1.6%
Other 3.3%
27 1 0

Clone this repository

https://tangled.org/gazagnaire.org/ocaml-respond https://tangled.org/did:plc:jhift2vwcxhou52p3sewcrpx/ocaml-respond
git@git.recoil.org:gazagnaire.org/ocaml-respond git@git.recoil.org:did:plc:jhift2vwcxhou52p3sewcrpx/ocaml-respond

For self-hosted knots, clone URLs may differ based on your setup.

Download tar.gz
README.md

respond#

Eio HTTP server with static file serving and route handlers.

Serves files from a document root with MIME detection, ETag conditional requests, and directory index. Supports custom route handlers for API endpoints. Reuses HTTP types from the requests library.

Installation#

Install with opam:

$ opam install respond

If opam cannot find the package, it may not yet be released in the public opam-repository. Add the overlay repository, then install it:

$ opam repo add samoht https://tangled.org/gazagnaire.org/opam-overlay.git
$ opam update
$ opam install respond

Usage#

let run () =
  Eio_main.run @@ fun env ->
  let routes =
    Respond.
      [
        get "/api/health" (fun _req -> Response.json {|{"ok":true}|});
        post "/api/echo" (fun req -> Response.text req.body);
      ]
  in
  Respond.run ~net:(Eio.Stdenv.net env) ~port:8080 ~root:(Eio.Stdenv.cwd env)
    routes

API Overview#

Routes#

Routes are created using get and post combinators:

  • Respond.get path handler -- Create a GET route. Handler receives a get_request.
  • Respond.post path handler -- Create a POST route. Handler receives a post_request.

Handlers receive typed request records (with params, headers, and for POST also body), not bare parameter lists.

Response Constructors#

  • Response.json, Response.text, Response.html -- Content responses
  • Response.not_found, Response.bad_request, Response.redirect -- Error/redirect responses
  • Response.method_not_allowed, Response.internal_server_error -- HTTP error responses

Server#

  • Respond.run -- Start serving on a port with static files + route handlers. Routes are passed as the last positional argument.

License#

ISC