package http_async

  1. Overview
  2. Docs
Async web toolkit

Install

Dune Dependency

Authors

Maintainers

Sources

http_async-0.0.3.tbz
sha256=fd0da06524318709cbad869956c3071d5c2fba948de16806a3a44b1aa76f3a2c
sha512=c4679be8d92901209f111139622575a529fc9b54ad72b87f0a5ae02e7d5d5d9417c78a385f2be048c2d7ea64a349282bf1252e90877015c058931eb2bf324f77

Description

Tags

http-server http http1.1 async

Published: 10 Apr 2022

README

Http_async

HTTP 1.1 server for async applications.

Getting Started

This library isn't published to the opam repository yet so it requires pinning to the development version of dependencies.

opam pin add -n http_async.dev git+https://github.com/anuragsoni/http_async.git
opam install http_async

Hello World

open! Core
open! Async
open Http_async

let () =
  Command_unix.run
    (Server.run_command ~summary:"Hello world HTTP Server" (fun request ->
         let%bind () =
           Pipe.iter_without_pushback
             ~f:(fun chunk -> Log.Global.info "%s" chunk)
             (Service.body request)
         in
         Service.respond_string "Hello World"))
;;

Routing?

Http_async doesn't ship with a router. There are multiple routing libraries available on opam and using Http_async with them should be fairly easy. As an example, integration with ocaml-dispatch can be done as so:

open! Core
open! Async
open Http_async

let routes =
  let open Dispatch in
  DSL.create
    [ ( "/hello/:name"
      , fun params _rest ->
          let name = List.Assoc.find_exn params ~equal:String.equal "name" in
          Service.respond_string (sprintf "Hello, %s" name) )
    ; ("/", fun _params _rest -> Service.respond_string "Hello World")
    ]
;;

let service request =
  let path = Service.resource request in
  match Dispatch.dispatch routes path with
  | Some response -> response
  | None -> Service.respond_string ~status:`Not_found "Route not found"
;;

let () = Command_unix.run (Server.run_command ~summary:"Hello world HTTP Server" service)

Dependencies (4)

  1. ppxlib >= "0.23.0"
  2. shuttle >= "0.4.0" & < "0.5.0"
  3. ocaml >= "4.11.0"
  4. dune >= "2.9"

Dev Dependencies (1)

  1. odoc with-doc

Used by

None

Conflicts

None

OCaml

Innovation. Community. Security.