mirror of
https://github.com/aantron/dream.git
synced 2025-12-30 00:26:32 -05:00
j-stream
In example 6-echo, we echoed POST requests by reading
their whole bodies into memory, and then writing them. Here, we echo request
bodies chunk by chunk:
let echo request response =
let rec loop () =
match%lwt Dream.read request with
| None ->
Dream.close_stream response
| Some chunk ->
let%lwt () = Dream.write chunk response in
let%lwt () = Dream.flush response in
loop ()
in
loop ()
let () =
Dream.run
@@ Dream.logger
@@ Dream.router [
Dream.post "/echo" (fun request ->
Dream.stream (echo request));
]
@@ Dream.not_found
$ dune exec --root . ./stream.exe
You can test it by running
curl -X POST http://localhost:8080/echo -T -
You will see the server responding immediately to each line on STDIN.
See Streaming in the API docs.
Next steps:
k-websocketsends and receives messages over a WebSocket.l-httpsenables HTTPS, which is very easy with Dream.