项目作者: anuragsoni

项目描述 :
http toolkit using async
高级语言: OCaml
项目地址: git://github.com/anuragsoni/async-http.git
创建时间: 2020-02-09T20:56:47Z
项目社区:https://github.com/anuragsoni/async-http

开源协议:MIT License

下载


Http_async

HTTP 1.1 server for async applications.

Installation

To use the version published on opam:
  1. opam install http_async
For the development version:
  1. opam pin add http_async.dev git+https://github.com/anuragsoni/http_async.git

Hello World

  1. open! Core
  2. open! Async
  3. open Http_async
  4. let () =
  5. Command_unix.run
  6. (Server.run_command ~summary:"Hello world HTTP Server" (fun addr (request, _body) ->
  7. Log.Global.info
  8. "(%s): %s"
  9. (Socket.Address.Inet.to_string addr)
  10. (Request.path request);
  11. return (Response.create `Ok, Body.Writer.string "Hello World")))
  12. ;;

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:

  1. open! Core
  2. open! Async
  3. open Http_async
  4. let routes =
  5. let open Dispatch in
  6. DSL.create
  7. [ ( "/hello/:name"
  8. , fun params _rest ->
  9. let name = List.Assoc.find_exn params ~equal:String.equal "name" in
  10. return (Response.create `Ok, Body.Writer.string (sprintf "Hello, %s" name)) )
  11. ; ("/", fun _params _rest -> Response.create `Ok, Body.Writer.string "Hello World")
  12. ]
  13. ;;
  14. let service _addr (request, body) =
  15. let path = Request.path request in
  16. match Dispatch.dispatch routes path with
  17. | Some response -> response
  18. | None -> return (Response.create `Not_found, Body.Writer.string "Route not found")
  19. ;;
  20. let () = Command_unix.run (Server.run_command ~summary:"Hello world HTTP Server" service)