leostera/httpkit

⚡️ High-level, High-performance HTTP(S) Clients/Servers in Reason/OCaml

OCaml

204

126 commits

updated Dec 19, 2019

See the code

README

⚡️HttpKit — high-level, high-performance HTTP1.1/2 clients/servers in Reason

NOTE: under heavy reconstruction. Latest stable version was 660d1c8

HttpKit is a high-level library for building and consuming web servers over HTTP, HTTPS, and HTTP2.

It serves as a thin layer over h2 and http/af, and when it can it allows you to seamlessly transition from one to the other.

  1. Roadmap
  2. Getting Started
  3. Running the Examples

Roadmap

FeatureHTTP/1.1HTTPS/1.1HTTP/2HTTPS/2
Listen as ServerYesNoYesNo
Send RequestYesYesNoNo
Server Push--NoNo

Getting Started

Usage

httpkit can be used both to build servers and to make requests as a client.

Documentation is still a work-in-progress, but there's examples in the examples section that can give you a better idea of how to use the libraries. In short:

For making a request:

open Lwt_result.Infix;

module Httpkit = Httpkit_lwt_unix_httpaf;

let req =
  Httpkit.Request.create(
    ~headers=[("User-Agent", "Reason HttpKit")],
    `GET,
    Uri.of_string("http://api.github.com/repos/ostera/httpkit"),
  );

/* Send over HTTP */
req
|> Httpkit.Client.Http.send
>>= Httpkit.Client.Response.body
|> Lwt_main.run

/* Send over HTTPS */
req
|> Httpkit.Client.Https.send
>>= Httpkit.Client.Response.body
|> Lwt_main.run

For making a server:

module Httpkit = Httpkit_lwt_unix_httpaf;

let port = 8080;

let on_start = (~hoststring) =>
  Logs.app(m => m("Running on %s", hoststring));

let handler: Httpkit.Server.handler =
  (req, reply, kill_server) => {
    let method = req |> Httpkit.Request.meth |> H2.Method.to_string;
    let path = req |> Httpkit.Request.path;
    Logs.app(m => m("%s %s", method, path));
    reply(200, "hi");
    kill_server();
  };

/* Start server over HTTP */
Httpkit.Server.Http.listen(~port, ~address=`Any, ~handler, ~on_start)
|> Lwt_main.run;

/* Start server over HTTPS */
Httpkit.Server.Http.listen(~port, ~address=`Any, ~handler, ~on_start)
|> Lwt_main.run;

Installing with esy

You can install by dropping the following dependencies in your package.json:

{
  "dependencies": {
    "@opam/httpkit": "*",
    "@opam/httpkit-lwt-unix-httpaf": "*",
    "@opam/logs": "*",
    "@opam/fmt": "*",
    // ...
  },
  "resolutions": {
    "@opam/httpkit": "ostera/httpkit:httpkit.opam#f738417",
    "@opam/httpkit-lwt-unix-httpaf": "ostera/httpkit:httpkit-lwt-unix-httpaf.opam#f738417",
  }
}

NOTE: For httpkit make sure you're using the latest commit hash!

Running the Examples

All of the examples are runnable as binaries after compilation, so you can either run esy build and find them within ./_esy/default/build/default/examples/*.exe or you can ask dune to run them for you:

ostera/httpkit λ esy dune exec ./examples/Request.exe
client
developer-experience
http
http-framework
https
middleware
native
ocaml
reasonml
server

Contributors

leostera

117 commits

emmenko

2 commits

hannesm

2 commits

ulrikstrid

2 commits

leostera/httpkit

⚡️ High-level, High-performance HTTP(S) Clients/Servers in Reason/OCaml

OCaml

204

126 commits

updated Dec 19, 2019

See the code

README

⚡️HttpKit — high-level, high-performance HTTP1.1/2 clients/servers in Reason

NOTE: under heavy reconstruction. Latest stable version was 660d1c8

HttpKit is a high-level library for building and consuming web servers over HTTP, HTTPS, and HTTP2.

It serves as a thin layer over h2 and http/af, and when it can it allows you to seamlessly transition from one to the other.

  1. Roadmap
  2. Getting Started
  3. Running the Examples

Roadmap

FeatureHTTP/1.1HTTPS/1.1HTTP/2HTTPS/2
Listen as ServerYesNoYesNo
Send RequestYesYesNoNo
Server Push--NoNo

Getting Started

Usage

httpkit can be used both to build servers and to make requests as a client.

Documentation is still a work-in-progress, but there's examples in the examples section that can give you a better idea of how to use the libraries. In short:

For making a request:

open Lwt_result.Infix;

module Httpkit = Httpkit_lwt_unix_httpaf;

let req =
  Httpkit.Request.create(
    ~headers=[("User-Agent", "Reason HttpKit")],
    `GET,
    Uri.of_string("http://api.github.com/repos/ostera/httpkit"),
  );

/* Send over HTTP */
req
|> Httpkit.Client.Http.send
>>= Httpkit.Client.Response.body
|> Lwt_main.run

/* Send over HTTPS */
req
|> Httpkit.Client.Https.send
>>= Httpkit.Client.Response.body
|> Lwt_main.run

For making a server:

module Httpkit = Httpkit_lwt_unix_httpaf;

let port = 8080;

let on_start = (~hoststring) =>
  Logs.app(m => m("Running on %s", hoststring));

let handler: Httpkit.Server.handler =
  (req, reply, kill_server) => {
    let method = req |> Httpkit.Request.meth |> H2.Method.to_string;
    let path = req |> Httpkit.Request.path;
    Logs.app(m => m("%s %s", method, path));
    reply(200, "hi");
    kill_server();
  };

/* Start server over HTTP */
Httpkit.Server.Http.listen(~port, ~address=`Any, ~handler, ~on_start)
|> Lwt_main.run;

/* Start server over HTTPS */
Httpkit.Server.Http.listen(~port, ~address=`Any, ~handler, ~on_start)
|> Lwt_main.run;

Installing with esy

You can install by dropping the following dependencies in your package.json:

{
  "dependencies": {
    "@opam/httpkit": "*",
    "@opam/httpkit-lwt-unix-httpaf": "*",
    "@opam/logs": "*",
    "@opam/fmt": "*",
    // ...
  },
  "resolutions": {
    "@opam/httpkit": "ostera/httpkit:httpkit.opam#f738417",
    "@opam/httpkit-lwt-unix-httpaf": "ostera/httpkit:httpkit-lwt-unix-httpaf.opam#f738417",
  }
}

NOTE: For httpkit make sure you're using the latest commit hash!

Running the Examples

All of the examples are runnable as binaries after compilation, so you can either run esy build and find them within ./_esy/default/build/default/examples/*.exe or you can ask dune to run them for you:

ostera/httpkit λ esy dune exec ./examples/Request.exe
client
developer-experience
http
http-framework
https
middleware
native
ocaml
reasonml
server

Contributors

leostera

117 commits

emmenko

2 commits

hannesm

2 commits

ulrikstrid

2 commits

Languages

OCaml

94.0%

C

3.4%

Shell

2.6%