0
Fork 0
mirror of https://github.com/ninenines/cowboy.git synced 2025-07-14 20:30:23 +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

@ -0,0 +1,14 @@
PROJECT = rest_stream_response
DEPS = cowboy
dep_cowboy = pkg://cowboy master
.PHONY: release clean-release
release: clean-release all
relx
clean-release:
rm -rf _rel
include ../../erlang.mk

View file

@ -1,26 +1,34 @@
Cowboy REST Streaming Responses REST streaming example
=============================== ======================
To compile this example you need rebar in your PATH. To try this example, you need GNU `make`, `git` and
[relx](https://github.com/erlware/relx) in your PATH.
Type the following command: To build the example, run the following command:
```
$ rebar get-deps compile ``` bash
$ make
``` ```
You can then start the Erlang node with the following command: To start the release in the foreground:
```
./start.sh ``` bash
$ ./_rel/bin/rest_stream_response_example console
``` ```
Then point your browser at [http://localhost:8080](http://localhost:8080).
About
-----
This example simulates streaming a large amount of data from a data store one This example simulates streaming a large amount of data from a data store one
record at a time in CSV format. It also uses a constraint to ensure that the record at a time in CSV format. It also uses a constraint to ensure that the
last segment of the route is an integer. last segment of the route is an integer.
Examples Example output
-------- --------------
### Get records with a field 2 value of 1 Fetch records with the second field with value 1:
``` bash ``` bash
$ curl -i localhost:8080 $ curl -i localhost:8080
@ -37,7 +45,7 @@ DAYEFxER,1,18
... ...
``` ```
### Get records with a field 2 value of 4 Fetch records with the second field with value 4:
``` bash ``` bash
$ curl -i localhost:8080/4 $ curl -i localhost:8080/4
@ -54,7 +62,7 @@ CA8BBhYD,4,10
... ...
``` ```
### Get a 404 Fail to use a proper integer and get an error:
``` bash ``` bash
$ curl -i localhost:8080/foo $ curl -i localhost:8080/foo
@ -63,4 +71,5 @@ connection: keep-alive
server: Cowboy server: Cowboy
date: Sun, 10 Feb 2013 19:36:16 GMT date: Sun, 10 Feb 2013 19:36:16 GMT
content-length: 0 content-length: 0
``` ```

View file

@ -1,4 +0,0 @@
{deps, [
{cowboy, ".*",
{git, "git://github.com/extend/cowboy.git", "master"}}
]}.

View file

@ -0,0 +1,2 @@
{release, {rest_stream_response_example, "1"}, [rest_stream_response]}.
{extended_start_script, true}.

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) -> stop(_State) ->
ok. ok.
generate_rows(_Table, 0) -> ok; generate_rows(_Table, 0) ->
ok;
generate_rows(Table, N) -> generate_rows(Table, N) ->
ets:insert(Table, {key(), val(), val()}), ets:insert(Table, {key(), val(), val()}),
generate_rows(Table, N - 1). generate_rows(Table, N - 1).

View file

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

View file

@ -1,3 +0,0 @@
#!/bin/sh
erl -pa ebin deps/*/ebin -s rest_stream_response \
-eval "io:format(\"Streaming results: curl -i http://localhost:8080~n\")."