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 aget_request.Respond.post path handler-- Create a POST route. Handler receives apost_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 responsesResponse.not_found,Response.bad_request,Response.redirect-- Error/redirect responsesResponse.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