Debian Codegraph @

test/handlers/streamed_result_h.erl

raw
1 -module(streamed_result_h).
2
3 -export([init/2]).
4
5 init(Req, Opts) ->
6 N = list_to_integer(binary_to_list(cowboy_req:binding(n, Req))),
7 Interval = list_to_integer(binary_to_list(cowboy_req:binding(interval, Req))),
8 chunked(N, Interval, Req, Opts).
9
10 chunked(N, Interval, Req0, Opts) ->
11 Req = cowboy_req:stream_reply(200, Req0),
12 {ok, loop(N, Interval, Req), Opts}.
13
14 loop(0, _Interval, Req) ->
15 ok = cowboy_req:stream_body("Finished!\n", fin, Req),
16 Req;
17 loop(N, Interval, Req) ->
18 ok = cowboy_req:stream_body(iolist_to_binary([integer_to_list(N), <<"\n">>]), nofin, Req),
19 timer:sleep(Interval),
20 loop(N-1, Interval, Req).