Browse Source

Convert the REST streaming example to a release

Loïc Hoguin 11 years ago
parent
commit
7a7e144a0d

+ 14 - 0
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

+ 23 - 14
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
+
 ```

+ 0 - 4
examples/rest_stream_response/rebar.config

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

+ 2 - 0
examples/rest_stream_response/relx.config

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

+ 0 - 15
examples/rest_stream_response/src/rest_stream_response.erl

@@ -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).

+ 2 - 1
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).

+ 1 - 2
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}) ->

+ 0 - 3
examples/rest_stream_response/start.sh

@@ -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\")."