0
Fork 0
mirror of https://github.com/ninenines/cowboy.git synced 2025-07-15 12:40:25 +00:00

Convert the REST streaming example to a release

This commit is contained in:
Loïc Hoguin 2013-09-08 19:50:31 +02:00
parent 630a805541
commit 7a7e144a0d
8 changed files with 42 additions and 39 deletions

View file

@ -1,15 +0,0 @@
%% Feel free to use, reuse and abuse the code in this file.
-module(rest_stream_response).
%% API.
-export([start/0]).
%% API.
start() ->
ok = application:start(crypto),
ok = application:start(cowlib),
ok = application:start(ranch),
ok = application:start(cowboy),
ok = application:start(rest_stream_response).

View file

@ -26,7 +26,8 @@ start(_Type, _Args) ->
stop(_State) ->
ok.
generate_rows(_Table, 0) -> ok;
generate_rows(_Table, 0) ->
ok;
generate_rows(Table, N) ->
ets:insert(Table, {key(), val(), val()}),
generate_rows(Table, N - 1).

View file

@ -22,12 +22,11 @@ content_types_provided(Req, State) ->
streaming_csv(Req, Table) ->
{N, Req1} = cowboy_req:binding(v1, Req, 1),
MS = [{{'$1', '$2', '$3'}, [{'==', '$2', N}], ['$$']}],
{{stream, result_streamer(Table, MS)}, Req1, Table}.
result_streamer(Table, MS) ->
fun (Socket, Transport) ->
send_records(Socket, Transport, ets:select(Table, MS, 1))
send_records(Socket, Transport, ets:select(Table, MS, 1))
end.
send_records(Socket, Transport, {[Rec], Cont}) ->