package cohttp-server-lwt-unix

  1. Overview
  2. Docs
Legend:
Library
Module
Module type
Parameter
Class
Class type

High performance lwt server

This module is an alternative to the server offered in Cohttp_lwt_unix.Server. It's a simplified implementation that has less functionality but offers more control and better performance. The differences are as follows:

  • Vastly improved performance due to optimized buffer handling
  • No dependency on conduit
  • No builtin logging

An example server:

open Lwt.Syntax

let server_callback ctx =
  Lwt.join
    [
      Cohttp_server_lwt_unix.ontext.discard_body ctx;
      Cohttp_server_lwt_unix.ontext.respond ctx (Http.Response.make ())
        (Cohttp_server_lwt_unix.Body.string "hello world");
    ]

let main () =
  let* _server =
    let listen_address = Unix.(ADDR_INET (inet_addr_loopback, 8080)) in
    let server = Cohttp_server_lwt_unix.create server_callback in
    Lwt_io.establish_server_with_client_address ~backlog:10_000
      listen_address (fun _addr ch ->
        Cohttp_server_lwt_unix.handle_connection server ch)
  in
  let forever, _ = Lwt.wait () in
  forever

let () = ignore (Lwt_main.run (main ()))
module Body : sig ... end
module Context : sig ... end
type t

The type of an HTTP server able to handle requests.

val create : ?on_exn:(exn -> unit) -> (Context.t -> unit Lwt.t) -> t

create ?on_exn f creates an HTTP server that will handle every incoming request with f concurrently.

on_exn will be called on exceptions not caught in f or raised by the server itself. If on_exn isn't provided Lwt.async_exception_hook will be used.

val handle_connection : t -> (Lwt_io.input_channel * Lwt_io.output_channel) -> unit Lwt.t

handle_connection t (i, o) will handle all HTTP requests incoming from i and write them to o.

This function should be used with Lwt_io.establish_server_with_client_address to setup a running HTTP server.

OCaml

Innovation. Community. Security.