Skip to content

Commit f212caf

Browse files
committed
Add a REST Hello World example
1 parent 6ea32d6 commit f212caf

8 files changed

+141
-0
lines changed

examples/rest_hello_world/README.md

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
Cowboy Hello World
2+
==================
3+
4+
To compile this example you need rebar in your PATH.
5+
6+
Type the following command:
7+
```
8+
$ rebar get-deps compile
9+
```
10+
11+
You can then start the Erlang node with the following command:
12+
```
13+
./start.sh
14+
```
15+
16+
Then run any given command or point your browser to the indicated URL.
+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{deps, [
2+
{cowboy, ".*",
3+
{git, "git://github.com/extend/cowboy.git", "master"}}
4+
]}.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
%% Feel free to use, reuse and abuse the code in this file.
2+
3+
{application, rest_hello_world, [
4+
{description, "Cowboy REST Hello World example."},
5+
{vsn, "1"},
6+
{modules, []},
7+
{registered, []},
8+
{applications, [
9+
kernel,
10+
stdlib,
11+
cowboy
12+
]},
13+
{mod, {rest_hello_world_app, []}},
14+
{env, []}
15+
]}.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
%% Feel free to use, reuse and abuse the code in this file.
2+
3+
-module(rest_hello_world).
4+
5+
%% API.
6+
-export([start/0]).
7+
8+
%% API.
9+
10+
start() ->
11+
ok = application:start(cowboy),
12+
ok = application:start(rest_hello_world).
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
%% Feel free to use, reuse and abuse the code in this file.
2+
3+
%% @private
4+
-module(rest_hello_world_app).
5+
-behaviour(application).
6+
7+
%% API.
8+
-export([start/2]).
9+
-export([stop/1]).
10+
11+
%% API.
12+
13+
start(_Type, _Args) ->
14+
Dispatch = [
15+
{'_', [
16+
{[], toppage_handler, []}
17+
]}
18+
],
19+
{ok, _} = cowboy:start_listener(http, 100,
20+
cowboy_tcp_transport, [{port, 8080}],
21+
cowboy_http_protocol, [{dispatch, Dispatch}]
22+
),
23+
rest_hello_world_sup:start_link().
24+
25+
stop(_State) ->
26+
ok.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
%% Feel free to use, reuse and abuse the code in this file.
2+
3+
%% @private
4+
-module(rest_hello_world_sup).
5+
-behaviour(supervisor).
6+
7+
%% API.
8+
-export([start_link/0]).
9+
10+
%% supervisor.
11+
-export([init/1]).
12+
13+
%% API.
14+
15+
-spec start_link() -> {ok, pid()}.
16+
start_link() ->
17+
supervisor:start_link({local, ?MODULE}, ?MODULE, []).
18+
19+
%% supervisor.
20+
21+
init([]) ->
22+
Procs = [],
23+
{ok, {{one_for_one, 10, 10}, Procs}}.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
%% Feel free to use, reuse and abuse the code in this file.
2+
3+
%% @doc Hello world handler.
4+
-module(toppage_handler).
5+
6+
-export([init/3]).
7+
-export([content_types_provided/2]).
8+
-export([hello_to_html/2]).
9+
-export([hello_to_json/2]).
10+
-export([hello_to_text/2]).
11+
12+
init(_Transport, _Req, []) ->
13+
{upgrade, protocol, cowboy_http_rest}.
14+
15+
content_types_provided(Req, State) ->
16+
{[
17+
{<<"text/html">>, hello_to_html},
18+
{<<"application/json">>, hello_to_json},
19+
{<<"text/plain">>, hello_to_text}
20+
], Req, State}.
21+
22+
hello_to_html(Req, State) ->
23+
Body = <<"<html>
24+
<head>
25+
<meta charset=\"utf-8\">
26+
<title>REST Hello World!</title>
27+
</head>
28+
<body>
29+
<p>REST Hello World as HTML!</p>
30+
</body>
31+
</html>">>,
32+
{Body, Req, State}.
33+
34+
hello_to_json(Req, State) ->
35+
Body = <<"{\"rest\": \"Hello World!\"}">>,
36+
{Body, Req, State}.
37+
38+
hello_to_text(Req, State) ->
39+
{<<"REST Hello World as text!">>, Req, State}.

examples/rest_hello_world/start.sh

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/bin/sh
2+
erl -pa ebin deps/*/ebin -s rest_hello_world \
3+
-eval "io:format(\"Get HTML: curl -i http://localhost:8080~n\")." \
4+
-eval "io:format(\"Get JSON: curl -i -H \\\"Accept: application/json\\\" http://localhost:8080~n\")." \
5+
-eval "io:format(\"Get text: curl -i -H \\\"Accept: text/plain\\\" http://localhost:8080~n\")." \
6+
-eval "io:format(\"Get a 406: curl -i -H \\\"Accept: text/css\\\" http://localhost:8080~n\")."

0 commit comments

Comments
 (0)