0
Fork 0
mirror of https://github.com/ninenines/cowboy.git synced 2025-07-14 12:20:24 +00:00

Convert the REST hello world example to a release

This commit is contained in:
Loïc Hoguin 2013-09-08 12:22:30 +02:00
parent 49acfce8b9
commit 81843414b0
6 changed files with 42 additions and 47 deletions

View file

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

View file

@ -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: 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/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 ``` bash
$ curl -i http://localhost:8080 $ curl -i http://localhost:8080
@ -27,8 +30,8 @@ connection: keep-alive
server: Cowboy server: Cowboy
date: Fri, 28 Sep 2012 04:15:52 GMT date: Fri, 28 Sep 2012 04:15:52 GMT
content-length: 136 content-length: 136
Content-Type: text/html content-type: text/html
Vary: Accept vary: Accept
<html> <html>
<head> <head>
@ -41,7 +44,7 @@ Vary: Accept
</html> </html>
``` ```
### Get JSON Request JSON:
``` bash ``` bash
$ curl -i -H "Accept: application/json" http://localhost:8080 $ curl -i -H "Accept: application/json" http://localhost:8080
@ -50,13 +53,13 @@ connection: keep-alive
server: Cowboy server: Cowboy
date: Fri, 28 Sep 2012 04:16:46 GMT date: Fri, 28 Sep 2012 04:16:46 GMT
content-length: 24 content-length: 24
Content-Type: application/json content-type: application/json
Vary: Accept vary: Accept
{"rest": "Hello World!"} {"rest": "Hello World!"}
``` ```
### Get text Request plain text:
``` bash ``` bash
$ curl -i -H "Accept: text/plain" http://localhost:8080 $ curl -i -H "Accept: text/plain" http://localhost:8080
@ -65,13 +68,14 @@ connection: keep-alive
server: Cowboy server: Cowboy
date: Fri, 28 Sep 2012 04:18:35 GMT date: Fri, 28 Sep 2012 04:18:35 GMT
content-length: 25 content-length: 25
Content-Type: text/plain content-type: text/plain
Vary: Accept vary: Accept
REST Hello World as text! REST Hello World as text!
``` ```
### Get a 406 Request a non acceptable content-type:
``` bash ``` bash
$ curl -i -H "Accept: text/css" http://localhost:8080 $ curl -i -H "Accept: text/css" http://localhost:8080
HTTP/1.1 406 Not Acceptable HTTP/1.1 406 Not Acceptable

View file

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

View file

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

View file

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

View file

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