Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/anuragsoni/http_async
Asynchronous HTTP 1.1 server for OCaml
https://github.com/anuragsoni/http_async
async http http-server http11 ocaml server
Last synced: 8 days ago
JSON representation
Asynchronous HTTP 1.1 server for OCaml
- Host: GitHub
- URL: https://github.com/anuragsoni/http_async
- Owner: anuragsoni
- License: mit
- Created: 2020-02-09T20:56:47.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2022-11-11T18:16:21.000Z (almost 2 years ago)
- Last Synced: 2023-03-01T05:35:52.907Z (over 1 year ago)
- Topics: async, http, http-server, http11, ocaml, server
- Language: OCaml
- Homepage: https://anuragsoni.github.io/http_async/
- Size: 3.77 MB
- Stars: 16
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGES.md
- License: LICENSE
Awesome Lists containing this project
README
# Http_async
HTTP 1.1 server for async applications.
### Installation
###### To use the version published on opam:
```
opam install http_async
```###### For the development version:
```
opam pin add http_async.dev git+https://github.com/anuragsoni/http_async.git
```### Hello World
```ocaml
open! Core
open! Async
open Http_asynclet () =
Command_unix.run
(Server.run_command ~summary:"Hello world HTTP Server" (fun addr (request, _body) ->
Log.Global.info
"(%s): %s"
(Socket.Address.Inet.to_string addr)
(Request.path request);
return (Response.create `Ok, Body.Writer.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](https://github.com/inhabitedtype/ocaml-dispatch) can be done as so:
```ocaml
open! Core
open! Async
open Http_asynclet routes =
let open Dispatch in
DSL.create
[ ( "/hello/:name"
, fun params _rest ->
let name = List.Assoc.find_exn params ~equal:String.equal "name" in
return (Response.create `Ok, Body.Writer.string (sprintf "Hello, %s" name)) )
; ("/", fun _params _rest -> Response.create `Ok, Body.Writer.string "Hello World")
]
;;let service _addr (request, body) =
let path = Request.path request in
match Dispatch.dispatch routes path with
| Some response -> response
| None -> return (Response.create `Not_found, Body.Writer.string "Route not found")
;;let () = Command_unix.run (Server.run_command ~summary:"Hello world HTTP Server" service)
```