From 81843414b00f3d1829db5f105206396985f1196c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Hoguin?= Date: Sun, 8 Sep 2013 12:22:30 +0200 Subject: Convert the REST hello world example to a release --- examples/rest_hello_world/Makefile | 14 +++++++ examples/rest_hello_world/README.md | 48 ++++++++++++---------- examples/rest_hello_world/rebar.config | 4 -- examples/rest_hello_world/relx.config | 2 + examples/rest_hello_world/src/rest_hello_world.erl | 15 ------- examples/rest_hello_world/start.sh | 6 --- 6 files changed, 42 insertions(+), 47 deletions(-) create mode 100644 examples/rest_hello_world/Makefile delete mode 100644 examples/rest_hello_world/rebar.config create mode 100644 examples/rest_hello_world/relx.config delete mode 100644 examples/rest_hello_world/src/rest_hello_world.erl delete mode 100755 examples/rest_hello_world/start.sh diff --git a/examples/rest_hello_world/Makefile b/examples/rest_hello_world/Makefile new file mode 100644 index 0000000..031d26e --- /dev/null +++ b/examples/rest_hello_world/Makefile @@ -0,0 +1,14 @@ +PROJECT = rest_hello_world + +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_hello_world/README.md b/examples/rest_hello_world/README.md index 7072e5e..98af501 100644 --- a/examples/rest_hello_world/README.md +++ b/examples/rest_hello_world/README.md @@ -1,24 +1,27 @@ -Cowboy Rest Hello World -======================= +REST hello world 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/hello_world_example console ``` -Then run any given command or point your browser to the indicated URL. +Then point your browser at [http://localhost:8080](http://localhost:8080). -Examples --------- +Example output +-------------- -### Get HTML +Request HTML: ``` bash $ curl -i http://localhost:8080 @@ -27,8 +30,8 @@ connection: keep-alive server: Cowboy date: Fri, 28 Sep 2012 04:15:52 GMT content-length: 136 -Content-Type: text/html -Vary: Accept +content-type: text/html +vary: Accept @@ -41,7 +44,7 @@ Vary: Accept ``` -### Get JSON +Request JSON: ``` bash $ curl -i -H "Accept: application/json" http://localhost:8080 @@ -50,13 +53,13 @@ connection: keep-alive server: Cowboy date: Fri, 28 Sep 2012 04:16:46 GMT content-length: 24 -Content-Type: application/json -Vary: Accept +content-type: application/json +vary: Accept {"rest": "Hello World!"} ``` -### Get text +Request plain text: ``` bash $ curl -i -H "Accept: text/plain" http://localhost:8080 @@ -65,13 +68,14 @@ connection: keep-alive server: Cowboy date: Fri, 28 Sep 2012 04:18:35 GMT content-length: 25 -Content-Type: text/plain -Vary: Accept +content-type: text/plain +vary: Accept REST Hello World as text! ``` -### Get a 406 +Request a non acceptable content-type: + ``` bash $ curl -i -H "Accept: text/css" http://localhost:8080 HTTP/1.1 406 Not Acceptable diff --git a/examples/rest_hello_world/rebar.config b/examples/rest_hello_world/rebar.config deleted file mode 100644 index 6ad3062..0000000 --- a/examples/rest_hello_world/rebar.config +++ /dev/null @@ -1,4 +0,0 @@ -{deps, [ - {cowboy, ".*", - {git, "git://github.com/extend/cowboy.git", "master"}} -]}. diff --git a/examples/rest_hello_world/relx.config b/examples/rest_hello_world/relx.config new file mode 100644 index 0000000..a6152d4 --- /dev/null +++ b/examples/rest_hello_world/relx.config @@ -0,0 +1,2 @@ +{release, {rest_hello_world_example, "1"}, [rest_hello_world]}. +{extended_start_script, true}. diff --git a/examples/rest_hello_world/src/rest_hello_world.erl b/examples/rest_hello_world/src/rest_hello_world.erl deleted file mode 100644 index afc221f..0000000 --- a/examples/rest_hello_world/src/rest_hello_world.erl +++ /dev/null @@ -1,15 +0,0 @@ -%% Feel free to use, reuse and abuse the code in this file. - --module(rest_hello_world). - -%% 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_hello_world). diff --git a/examples/rest_hello_world/start.sh b/examples/rest_hello_world/start.sh deleted file mode 100755 index 0533474..0000000 --- a/examples/rest_hello_world/start.sh +++ /dev/null @@ -1,6 +0,0 @@ -#!/bin/sh -erl -pa ebin deps/*/ebin -s rest_hello_world \ - -eval "io:format(\"Get HTML: curl -i http://localhost:8080~n\")." \ - -eval "io:format(\"Get JSON: curl -i -H \\\"Accept: application/json\\\" http://localhost:8080~n\")." \ - -eval "io:format(\"Get text: curl -i -H \\\"Accept: text/plain\\\" http://localhost:8080~n\")." \ - -eval "io:format(\"Get a 406: curl -i -H \\\"Accept: text/css\\\" http://localhost:8080~n\")." -- cgit v1.2.3