From 7a7e144a0d79767f726ca9988a7b8fc752f998ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Hoguin?= Date: Sun, 8 Sep 2013 19:50:31 +0200 Subject: Convert the REST streaming example to a release --- examples/rest_stream_response/Makefile | 14 ++++++++ examples/rest_stream_response/README.md | 37 ++++++++++++++-------- examples/rest_stream_response/rebar.config | 4 --- examples/rest_stream_response/relx.config | 2 ++ .../src/rest_stream_response.erl | 15 --------- .../src/rest_stream_response_app.erl | 3 +- .../rest_stream_response/src/toppage_handler.erl | 3 +- examples/rest_stream_response/start.sh | 3 -- 8 files changed, 42 insertions(+), 39 deletions(-) create mode 100644 examples/rest_stream_response/Makefile delete mode 100644 examples/rest_stream_response/rebar.config create mode 100644 examples/rest_stream_response/relx.config delete mode 100644 examples/rest_stream_response/src/rest_stream_response.erl delete mode 100755 examples/rest_stream_response/start.sh diff --git a/examples/rest_stream_response/Makefile b/examples/rest_stream_response/Makefile new file mode 100644 index 0000000..dfea603 --- /dev/null +++ b/examples/rest_stream_response/Makefile @@ -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 diff --git a/examples/rest_stream_response/README.md b/examples/rest_stream_response/README.md index 70e1ba7..90004e6 100644 --- a/examples/rest_stream_response/README.md +++ b/examples/rest_stream_response/README.md @@ -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: -``` -$ rebar get-deps compile -``` +To build the example, run the following command: -You can then start the Erlang node with the following command: +``` bash +$ make ``` -./start.sh + +To start the release in the foreground: + +``` 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 record at a time in CSV format. It also uses a constraint to ensure that the 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 $ 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 $ 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 $ curl -i localhost:8080/foo @@ -63,4 +71,5 @@ connection: keep-alive server: Cowboy date: Sun, 10 Feb 2013 19:36:16 GMT content-length: 0 + ``` diff --git a/examples/rest_stream_response/rebar.config b/examples/rest_stream_response/rebar.config deleted file mode 100644 index 6ad3062..0000000 --- a/examples/rest_stream_response/rebar.config +++ /dev/null @@ -1,4 +0,0 @@ -{deps, [ - {cowboy, ".*", - {git, "git://github.com/extend/cowboy.git", "master"}} -]}. diff --git a/examples/rest_stream_response/relx.config b/examples/rest_stream_response/relx.config new file mode 100644 index 0000000..024b947 --- /dev/null +++ b/examples/rest_stream_response/relx.config @@ -0,0 +1,2 @@ +{release, {rest_stream_response_example, "1"}, [rest_stream_response]}. +{extended_start_script, true}. diff --git a/examples/rest_stream_response/src/rest_stream_response.erl b/examples/rest_stream_response/src/rest_stream_response.erl deleted file mode 100644 index 31e9a36..0000000 --- a/examples/rest_stream_response/src/rest_stream_response.erl +++ /dev/null @@ -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). diff --git a/examples/rest_stream_response/src/rest_stream_response_app.erl b/examples/rest_stream_response/src/rest_stream_response_app.erl index a382d29..46857f4 100644 --- a/examples/rest_stream_response/src/rest_stream_response_app.erl +++ b/examples/rest_stream_response/src/rest_stream_response_app.erl @@ -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). diff --git a/examples/rest_stream_response/src/toppage_handler.erl b/examples/rest_stream_response/src/toppage_handler.erl index 5052038..224446a 100644 --- a/examples/rest_stream_response/src/toppage_handler.erl +++ b/examples/rest_stream_response/src/toppage_handler.erl @@ -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}) -> diff --git a/examples/rest_stream_response/start.sh b/examples/rest_stream_response/start.sh deleted file mode 100755 index f5efcb0..0000000 --- a/examples/rest_stream_response/start.sh +++ /dev/null @@ -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\")." -- cgit v1.2.3