aboutsummaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
Diffstat (limited to 'examples')
-rw-r--r--examples/README.md6
-rw-r--r--examples/basic_auth/README.md43
-rw-r--r--examples/basic_auth/rebar.config4
-rw-r--r--examples/basic_auth/src/basic_auth.app.src15
-rw-r--r--examples/basic_auth/src/basic_auth.erl14
-rw-r--r--examples/basic_auth/src/basic_auth_app.erl25
-rw-r--r--examples/basic_auth/src/basic_auth_sup.erl23
-rw-r--r--examples/basic_auth/src/toppage_handler.erl32
-rwxr-xr-xexamples/basic_auth/start.sh4
-rw-r--r--examples/chunked_hello_world/src/chunked_hello_world_app.erl8
-rw-r--r--examples/chunked_hello_world/src/toppage_handler.erl4
-rw-r--r--examples/compress_response/README.md62
-rw-r--r--examples/compress_response/rebar.config4
-rw-r--r--examples/compress_response/src/compress_response.app.src15
-rw-r--r--examples/compress_response/src/compress_response.erl14
-rw-r--r--examples/compress_response/src/compress_response_app.erl26
-rw-r--r--examples/compress_response/src/compress_response_sup.erl23
-rw-r--r--examples/compress_response/src/toppage_handler.erl31
-rwxr-xr-xexamples/compress_response/start.sh3
-rw-r--r--examples/cookie/src/cookie_app.erl6
-rw-r--r--examples/cookie/src/toppage_handler.erl4
-rw-r--r--examples/echo_get/src/echo_get_app.erl8
-rw-r--r--examples/echo_get/src/toppage_handler.erl4
-rw-r--r--examples/echo_post/src/echo_post_app.erl8
-rw-r--r--examples/echo_post/src/toppage_handler.erl10
-rw-r--r--examples/hello_world/src/hello_world_app.erl8
-rw-r--r--examples/hello_world/src/toppage_handler.erl4
-rw-r--r--examples/rest_hello_world/src/rest_hello_world_app.erl8
-rw-r--r--examples/static/README.md5
-rw-r--r--examples/static/priv/small.mp4bin0 -> 383631 bytes
-rw-r--r--examples/static/priv/small.ogvbin0 -> 872453 bytes
-rw-r--r--examples/static/priv/video.html11
-rw-r--r--examples/static/src/static_app.erl8
-rwxr-xr-xexamples/static/start.sh3
34 files changed, 402 insertions, 41 deletions
diff --git a/examples/README.md b/examples/README.md
index c0e1f41..f2b0c64 100644
--- a/examples/README.md
+++ b/examples/README.md
@@ -1,9 +1,15 @@
Cowboy Examples
===============
+ * [basic_auth](./examples/basic_auth):
+ basic HTTP authorization with REST
+
* [chunked_hello_world](./examples/chunked_hello_world):
demonstrates chunked data transfer with two one-second delays
+ * [compress_response](./examples/compress_response)
+ send a response body compressed if the client supports it
+
* [cookie](./examples/cookie):
set cookies from server and client side
diff --git a/examples/basic_auth/README.md b/examples/basic_auth/README.md
new file mode 100644
index 0000000..38ae9a2
--- /dev/null
+++ b/examples/basic_auth/README.md
@@ -0,0 +1,43 @@
+Cowboy Basic Authorization Rest Hello World
+===========================================
+
+To compile this example you need rebar in your PATH.
+
+Type the following command:
+```
+$ rebar get-deps compile
+```
+
+You can then start the Erlang node with the following command:
+```
+./start.sh
+```
+
+Then run any given command or point your browser to the indicated URL.
+
+Examples
+--------
+
+### Get 401
+``` bash
+$ curl -i http://localhost:8080
+HTTP/1.1 401 Unauthorized
+connection: keep-alive
+server: Cowboy
+date: Sun, 20 Jan 2013 14:10:27 GMT
+content-length: 0
+www-authenticate: Restricted
+```
+
+### Get 200
+``` bash
+$ curl -i -u "Alladin:open sesame" http://localhost:8080
+HTTP/1.1 200 OK
+connection: keep-alive
+server: Cowboy
+date: Sun, 20 Jan 2013 14:11:12 GMT
+content-length: 16
+content-type: text/plain
+
+Hello, Alladin!
+```
diff --git a/examples/basic_auth/rebar.config b/examples/basic_auth/rebar.config
new file mode 100644
index 0000000..6ad3062
--- /dev/null
+++ b/examples/basic_auth/rebar.config
@@ -0,0 +1,4 @@
+{deps, [
+ {cowboy, ".*",
+ {git, "git://github.com/extend/cowboy.git", "master"}}
+]}.
diff --git a/examples/basic_auth/src/basic_auth.app.src b/examples/basic_auth/src/basic_auth.app.src
new file mode 100644
index 0000000..cbf4ea1
--- /dev/null
+++ b/examples/basic_auth/src/basic_auth.app.src
@@ -0,0 +1,15 @@
+%% Feel free to use, reuse and abuse the code in this file.
+
+{application, basic_auth, [
+ {description, "Cowboy Basic HTTP Authorization example."},
+ {vsn, "1"},
+ {modules, []},
+ {registered, []},
+ {applications, [
+ kernel,
+ stdlib,
+ cowboy
+ ]},
+ {mod, {basic_auth_app, []}},
+ {env, []}
+]}.
diff --git a/examples/basic_auth/src/basic_auth.erl b/examples/basic_auth/src/basic_auth.erl
new file mode 100644
index 0000000..9294c77
--- /dev/null
+++ b/examples/basic_auth/src/basic_auth.erl
@@ -0,0 +1,14 @@
+%% Feel free to use, reuse and abuse the code in this file.
+
+-module(basic_auth).
+
+%% API.
+-export([start/0]).
+
+%% API.
+
+start() ->
+ ok = application:start(crypto),
+ ok = application:start(ranch),
+ ok = application:start(cowboy),
+ ok = application:start(basic_auth).
diff --git a/examples/basic_auth/src/basic_auth_app.erl b/examples/basic_auth/src/basic_auth_app.erl
new file mode 100644
index 0000000..24c766e
--- /dev/null
+++ b/examples/basic_auth/src/basic_auth_app.erl
@@ -0,0 +1,25 @@
+%% Feel free to use, reuse and abuse the code in this file.
+
+%% @private
+-module(basic_auth_app).
+-behaviour(application).
+
+%% API.
+-export([start/2]).
+-export([stop/1]).
+
+%% API.
+
+start(_Type, _Args) ->
+ Dispatch = cowboy_router:compile([
+ {'_', [
+ {"/", toppage_handler, []}
+ ]}
+ ]),
+ {ok, _} = cowboy:start_http(http, 100, [{port, 8080}], [
+ {env, [{dispatch, Dispatch}]}
+ ]),
+ basic_auth_sup:start_link().
+
+stop(_State) ->
+ ok.
diff --git a/examples/basic_auth/src/basic_auth_sup.erl b/examples/basic_auth/src/basic_auth_sup.erl
new file mode 100644
index 0000000..6219b5f
--- /dev/null
+++ b/examples/basic_auth/src/basic_auth_sup.erl
@@ -0,0 +1,23 @@
+%% Feel free to use, reuse and abuse the code in this file.
+
+%% @private
+-module(basic_auth_sup).
+-behaviour(supervisor).
+
+%% API.
+-export([start_link/0]).
+
+%% supervisor.
+-export([init/1]).
+
+%% API.
+
+-spec start_link() -> {ok, pid()}.
+start_link() ->
+ supervisor:start_link({local, ?MODULE}, ?MODULE, []).
+
+%% supervisor.
+
+init([]) ->
+ Procs = [],
+ {ok, {{one_for_one, 10, 10}, Procs}}.
diff --git a/examples/basic_auth/src/toppage_handler.erl b/examples/basic_auth/src/toppage_handler.erl
new file mode 100644
index 0000000..94383d4
--- /dev/null
+++ b/examples/basic_auth/src/toppage_handler.erl
@@ -0,0 +1,32 @@
+%% Feel free to use, reuse and abuse the code in this file.
+
+%% @doc Basic authorization Hello world handler.
+-module(toppage_handler).
+
+-export([init/3]).
+-export([content_types_provided/2]).
+-export([is_authorized/2]).
+-export([hello_to_text/2]).
+
+init(_Transport, _Req, []) ->
+ {upgrade, protocol, cowboy_rest}.
+
+
+is_authorized(Req, S) ->
+ {ok, Auth, Req1} = cowboy_req:parse_header(<<"authorization">>, Req),
+ case Auth of
+ {<<"basic">>, {User = <<"Alladin">>, <<"open sesame">>}} ->
+ {true, Req1, User};
+ _ ->
+ {{false, <<"Restricted">>}, Req1, S}
+ end.
+
+content_types_provided(Req, State) ->
+ {[
+ {<<"text/plain">>, hello_to_text}
+ ], Req, State}.
+
+
+hello_to_text(Req, User) ->
+ {<< <<"Hello, ">>/binary, User/binary, <<"!\n">>/binary >>, Req, User}.
+
diff --git a/examples/basic_auth/start.sh b/examples/basic_auth/start.sh
new file mode 100755
index 0000000..9e8a30b
--- /dev/null
+++ b/examples/basic_auth/start.sh
@@ -0,0 +1,4 @@
+#!/bin/sh
+erl -pa ebin deps/*/ebin -s basic_auth \
+ -eval "io:format(\"Get 401: curl -i http://localhost:8080~n\")." \
+ -eval "io:format(\"Get 200: curl -i -u \\\"Alladin:open sesame\\\" http://localhost:8080~n\")."
diff --git a/examples/chunked_hello_world/src/chunked_hello_world_app.erl b/examples/chunked_hello_world/src/chunked_hello_world_app.erl
index 41efd06..0032d01 100644
--- a/examples/chunked_hello_world/src/chunked_hello_world_app.erl
+++ b/examples/chunked_hello_world/src/chunked_hello_world_app.erl
@@ -11,13 +11,13 @@
%% API.
start(_Type, _Args) ->
- Dispatch = [
+ Dispatch = cowboy_router:compile([
{'_', [
- {[], toppage_handler, []}
+ {"/", toppage_handler, []}
]}
- ],
+ ]),
{ok, _} = cowboy:start_http(http, 100, [{port, 8080}], [
- {dispatch, Dispatch}
+ {env, [{dispatch, Dispatch}]}
]),
chunked_hello_world_sup:start_link().
diff --git a/examples/chunked_hello_world/src/toppage_handler.erl b/examples/chunked_hello_world/src/toppage_handler.erl
index 0838832..b6f2d04 100644
--- a/examples/chunked_hello_world/src/toppage_handler.erl
+++ b/examples/chunked_hello_world/src/toppage_handler.erl
@@ -5,7 +5,7 @@
-export([init/3]).
-export([handle/2]).
--export([terminate/2]).
+-export([terminate/3]).
init(_Transport, Req, []) ->
{ok, Req, undefined}.
@@ -19,5 +19,5 @@ handle(Req, State) ->
ok = cowboy_req:chunk("Chunked!\r\n", Req2),
{ok, Req2, State}.
-terminate(_Req, _State) ->
+terminate(_Reason, _Req, _State) ->
ok.
diff --git a/examples/compress_response/README.md b/examples/compress_response/README.md
new file mode 100644
index 0000000..8afbe65
--- /dev/null
+++ b/examples/compress_response/README.md
@@ -0,0 +1,62 @@
+Cowboy Compress Response
+========================
+
+To compile this example you need rebar in your PATH.
+
+Type the following command:
+```
+$ rebar get-deps compile
+```
+
+You can then start the Erlang node with the following command:
+```
+./start.sh
+```
+
+Then point your browser to the indicated URL.
+
+Example
+-------
+
+``` bash
+$ curl -i http://localhost:8080
+HTTP/1.1 200 OK
+connection: keep-alive
+server: Cowboy
+date: Mon, 07 Jan 2013 18:42:29 GMT
+content-length: 909
+
+A cowboy is an animal herder who tends cattle on ranches in North America,
+traditionally on horseback, and often performs a multitude of other ranch-
+related tasks. The historic American cowboy of the late 19th century arose
+from the vaquero traditions of northern Mexico and became a figure of special
+significance and legend. A subtype, called a wrangler, specifically tends the
+horses used to work cattle. In addition to ranch work, some cowboys work for
+or participate in rodeos. Cowgirls, first defined as such in the late 19th
+century, had a less-well documented historical role, but in the modern world
+have established the ability to work at virtually identical tasks and obtained
+considerable respect for their achievements. There are also cattle handlers
+in many other parts of the world, particularly South America and Australia,
+who perform work similar to the cowboy in their respective nations.
+
+$ curl -i --compressed http://localhost:8080
+HTTP/1.1 200 OK
+connection: keep-alive
+server: Cowboy
+date: Mon, 07 Jan 2013 18:42:30 GMT
+content-encoding: gzip
+content-length: 510
+
+A cowboy is an animal herder who tends cattle on ranches in North America,
+traditionally on horseback, and often performs a multitude of other ranch-
+related tasks. The historic American cowboy of the late 19th century arose
+from the vaquero traditions of northern Mexico and became a figure of special
+significance and legend. A subtype, called a wrangler, specifically tends the
+horses used to work cattle. In addition to ranch work, some cowboys work for
+or participate in rodeos. Cowgirls, first defined as such in the late 19th
+century, had a less-well documented historical role, but in the modern world
+have established the ability to work at virtually identical tasks and obtained
+considerable respect for their achievements. There are also cattle handlers
+in many other parts of the world, particularly South America and Australia,
+who perform work similar to the cowboy in their respective nations.
+```
diff --git a/examples/compress_response/rebar.config b/examples/compress_response/rebar.config
new file mode 100644
index 0000000..6ad3062
--- /dev/null
+++ b/examples/compress_response/rebar.config
@@ -0,0 +1,4 @@
+{deps, [
+ {cowboy, ".*",
+ {git, "git://github.com/extend/cowboy.git", "master"}}
+]}.
diff --git a/examples/compress_response/src/compress_response.app.src b/examples/compress_response/src/compress_response.app.src
new file mode 100644
index 0000000..3512084
--- /dev/null
+++ b/examples/compress_response/src/compress_response.app.src
@@ -0,0 +1,15 @@
+%% Feel free to use, reuse and abuse the code in this file.
+
+{application, compress_response, [
+ {description, "Cowboy Compress Response example."},
+ {vsn, "1"},
+ {modules, []},
+ {registered, []},
+ {applications, [
+ kernel,
+ stdlib,
+ cowboy
+ ]},
+ {mod, {compress_response_app, []}},
+ {env, []}
+]}.
diff --git a/examples/compress_response/src/compress_response.erl b/examples/compress_response/src/compress_response.erl
new file mode 100644
index 0000000..ac2636c
--- /dev/null
+++ b/examples/compress_response/src/compress_response.erl
@@ -0,0 +1,14 @@
+%% Feel free to use, reuse and abuse the code in this file.
+
+-module(compress_response).
+
+%% API.
+-export([start/0]).
+
+%% API.
+
+start() ->
+ ok = application:start(crypto),
+ ok = application:start(ranch),
+ ok = application:start(cowboy),
+ ok = application:start(compress_response).
diff --git a/examples/compress_response/src/compress_response_app.erl b/examples/compress_response/src/compress_response_app.erl
new file mode 100644
index 0000000..b36dcbd
--- /dev/null
+++ b/examples/compress_response/src/compress_response_app.erl
@@ -0,0 +1,26 @@
+%% Feel free to use, reuse and abuse the code in this file.
+
+%% @private
+-module(compress_response_app).
+-behaviour(application).
+
+%% API.
+-export([start/2]).
+-export([stop/1]).
+
+%% API.
+
+start(_Type, _Args) ->
+ Dispatch = cowboy_router:compile([
+ {'_', [
+ {"/", toppage_handler, []}
+ ]}
+ ]),
+ {ok, _} = cowboy:start_http(http, 100, [{port, 8080}], [
+ {compress, true},
+ {env, [{dispatch, Dispatch}]}
+ ]),
+ compress_response_sup:start_link().
+
+stop(_State) ->
+ ok.
diff --git a/examples/compress_response/src/compress_response_sup.erl b/examples/compress_response/src/compress_response_sup.erl
new file mode 100644
index 0000000..d1bc312
--- /dev/null
+++ b/examples/compress_response/src/compress_response_sup.erl
@@ -0,0 +1,23 @@
+%% Feel free to use, reuse and abuse the code in this file.
+
+%% @private
+-module(compress_response_sup).
+-behaviour(supervisor).
+
+%% API.
+-export([start_link/0]).
+
+%% supervisor.
+-export([init/1]).
+
+%% API.
+
+-spec start_link() -> {ok, pid()}.
+start_link() ->
+ supervisor:start_link({local, ?MODULE}, ?MODULE, []).
+
+%% supervisor.
+
+init([]) ->
+ Procs = [],
+ {ok, {{one_for_one, 10, 10}, Procs}}.
diff --git a/examples/compress_response/src/toppage_handler.erl b/examples/compress_response/src/toppage_handler.erl
new file mode 100644
index 0000000..3558a9c
--- /dev/null
+++ b/examples/compress_response/src/toppage_handler.erl
@@ -0,0 +1,31 @@
+%% Feel free to use, reuse and abuse the code in this file.
+
+%% @doc Compress response handler.
+-module(toppage_handler).
+
+-export([init/3]).
+-export([handle/2]).
+-export([terminate/3]).
+
+init(_Transport, Req, []) ->
+ {ok, Req, undefined}.
+
+handle(Req, State) ->
+ BigBody =
+<<"A cowboy is an animal herder who tends cattle on ranches in North America,
+traditionally on horseback, and often performs a multitude of other ranch-
+related tasks. The historic American cowboy of the late 19th century arose
+from the vaquero traditions of northern Mexico and became a figure of special
+significance and legend. A subtype, called a wrangler, specifically tends the
+horses used to work cattle. In addition to ranch work, some cowboys work for
+or participate in rodeos. Cowgirls, first defined as such in the late 19th
+century, had a less-well documented historical role, but in the modern world
+have established the ability to work at virtually identical tasks and obtained
+considerable respect for their achievements. There are also cattle handlers
+in many other parts of the world, particularly South America and Australia,
+who perform work similar to the cowboy in their respective nations.\n">>,
+ {ok, Req2} = cowboy_req:reply(200, [], BigBody, Req),
+ {ok, Req2, State}.
+
+terminate(_Reason, _Req, _State) ->
+ ok.
diff --git a/examples/compress_response/start.sh b/examples/compress_response/start.sh
new file mode 100755
index 0000000..2e79031
--- /dev/null
+++ b/examples/compress_response/start.sh
@@ -0,0 +1,3 @@
+#!/bin/sh
+erl -pa ebin deps/*/ebin -s compress_response \
+ -eval "io:format(\"Point your browser at http://localhost:8080~n\")."
diff --git a/examples/cookie/src/cookie_app.erl b/examples/cookie/src/cookie_app.erl
index 195d6b6..91d1b95 100644
--- a/examples/cookie/src/cookie_app.erl
+++ b/examples/cookie/src/cookie_app.erl
@@ -11,13 +11,13 @@
%% API.
start(_Type, _Args) ->
- Dispatch = [
+ Dispatch = cowboy_router:compile([
{'_', [
{'_', toppage_handler, []}
]}
- ],
+ ]),
{ok, _} = cowboy:start_http(http, 100, [{port, 8080}], [
- {dispatch, Dispatch}
+ {env, [{dispatch, Dispatch}]}
]),
cookie_sup:start_link().
diff --git a/examples/cookie/src/toppage_handler.erl b/examples/cookie/src/toppage_handler.erl
index 783cda6..b107d5a 100644
--- a/examples/cookie/src/toppage_handler.erl
+++ b/examples/cookie/src/toppage_handler.erl
@@ -5,7 +5,7 @@
-export([init/3]).
-export([handle/2]).
--export([terminate/2]).
+-export([terminate/3]).
init(_Transport, Req, []) ->
{ok, Req, undefined}.
@@ -25,5 +25,5 @@ handle(Req, State) ->
Body, Req4),
{ok, Req5, State}.
-terminate(_Req, _State) ->
+terminate(_Reason, _Req, _State) ->
ok.
diff --git a/examples/echo_get/src/echo_get_app.erl b/examples/echo_get/src/echo_get_app.erl
index b9551f0..d661e9c 100644
--- a/examples/echo_get/src/echo_get_app.erl
+++ b/examples/echo_get/src/echo_get_app.erl
@@ -11,13 +11,13 @@
%% API.
start(_Type, _Args) ->
- Dispatch = [
+ Dispatch = cowboy_router:compile([
{'_', [
- {[], toppage_handler, []}
+ {"/", toppage_handler, []}
]}
- ],
+ ]),
{ok, _} = cowboy:start_http(http, 100, [{port, 8080}], [
- {dispatch, Dispatch}
+ {env, [{dispatch, Dispatch}]}
]),
echo_get_sup:start_link().
diff --git a/examples/echo_get/src/toppage_handler.erl b/examples/echo_get/src/toppage_handler.erl
index 86433cb..c604bae 100644
--- a/examples/echo_get/src/toppage_handler.erl
+++ b/examples/echo_get/src/toppage_handler.erl
@@ -5,7 +5,7 @@
-export([init/3]).
-export([handle/2]).
--export([terminate/2]).
+-export([terminate/3]).
init(_Transport, Req, []) ->
{ok, Req, undefined}.
@@ -25,5 +25,5 @@ echo(_, _, Req) ->
%% Method not allowed.
cowboy_req:reply(405, Req).
-terminate(_Req, _State) ->
+terminate(_Reason, _Req, _State) ->
ok.
diff --git a/examples/echo_post/src/echo_post_app.erl b/examples/echo_post/src/echo_post_app.erl
index 93f3bd5..7d86c53 100644
--- a/examples/echo_post/src/echo_post_app.erl
+++ b/examples/echo_post/src/echo_post_app.erl
@@ -11,13 +11,13 @@
%% API.
start(_Type, _Args) ->
- Dispatch = [
+ Dispatch = cowboy_router:compile([
{'_', [
- {[], toppage_handler, []}
+ {"/", toppage_handler, []}
]}
- ],
+ ]),
{ok, _} = cowboy:start_http(http, 100, [{port, 8080}], [
- {dispatch, Dispatch}
+ {env, [{dispatch, Dispatch}]}
]),
echo_post_sup:start_link().
diff --git a/examples/echo_post/src/toppage_handler.erl b/examples/echo_post/src/toppage_handler.erl
index 808ba8e..6831c78 100644
--- a/examples/echo_post/src/toppage_handler.erl
+++ b/examples/echo_post/src/toppage_handler.erl
@@ -5,16 +5,16 @@
-export([init/3]).
-export([handle/2]).
--export([terminate/2]).
+-export([terminate/3]).
init(_Transport, Req, []) ->
{ok, Req, undefined}.
handle(Req, State) ->
{Method, Req2} = cowboy_req:method(Req),
- {HasBody, Req3} = cowboy_req:has_body(Req2),
- {ok, Req4} = maybe_echo(Method, HasBody, Req3),
- {ok, Req4, State}.
+ HasBody = cowboy_req:has_body(Req2),
+ {ok, Req3} = maybe_echo(Method, HasBody, Req2),
+ {ok, Req3, State}.
maybe_echo(<<"POST">>, true, Req) ->
{ok, PostVals, Req2} = cowboy_req:body_qs(Req),
@@ -32,5 +32,5 @@ echo(Echo, Req) ->
cowboy_req:reply(200,
[{<<"content-encoding">>, <<"utf-8">>}], Echo, Req).
-terminate(_Req, _State) ->
+terminate(_Reason, _Req, _State) ->
ok.
diff --git a/examples/hello_world/src/hello_world_app.erl b/examples/hello_world/src/hello_world_app.erl
index 1cb15ab..eb938d3 100644
--- a/examples/hello_world/src/hello_world_app.erl
+++ b/examples/hello_world/src/hello_world_app.erl
@@ -11,13 +11,13 @@
%% API.
start(_Type, _Args) ->
- Dispatch = [
+ Dispatch = cowboy_router:compile([
{'_', [
- {[], toppage_handler, []}
+ {"/", toppage_handler, []}
]}
- ],
+ ]),
{ok, _} = cowboy:start_http(http, 100, [{port, 8080}], [
- {dispatch, Dispatch}
+ {env, [{dispatch, Dispatch}]}
]),
hello_world_sup:start_link().
diff --git a/examples/hello_world/src/toppage_handler.erl b/examples/hello_world/src/toppage_handler.erl
index 55b5323..4124b5a 100644
--- a/examples/hello_world/src/toppage_handler.erl
+++ b/examples/hello_world/src/toppage_handler.erl
@@ -5,7 +5,7 @@
-export([init/3]).
-export([handle/2]).
--export([terminate/2]).
+-export([terminate/3]).
init(_Transport, Req, []) ->
{ok, Req, undefined}.
@@ -14,5 +14,5 @@ handle(Req, State) ->
{ok, Req2} = cowboy_req:reply(200, [], <<"Hello world!">>, Req),
{ok, Req2, State}.
-terminate(_Req, _State) ->
+terminate(_Reason, _Req, _State) ->
ok.
diff --git a/examples/rest_hello_world/src/rest_hello_world_app.erl b/examples/rest_hello_world/src/rest_hello_world_app.erl
index 510dbb1..a662c3d 100644
--- a/examples/rest_hello_world/src/rest_hello_world_app.erl
+++ b/examples/rest_hello_world/src/rest_hello_world_app.erl
@@ -11,13 +11,13 @@
%% API.
start(_Type, _Args) ->
- Dispatch = [
+ Dispatch = cowboy_router:compile([
{'_', [
- {[], toppage_handler, []}
+ {"/", toppage_handler, []}
]}
- ],
+ ]),
{ok, _} = cowboy:start_http(http, 100, [{port, 8080}], [
- {dispatch, Dispatch}
+ {env, [{dispatch, Dispatch}]}
]),
rest_hello_world_sup:start_link().
diff --git a/examples/static/README.md b/examples/static/README.md
index ef46312..78f5338 100644
--- a/examples/static/README.md
+++ b/examples/static/README.md
@@ -42,3 +42,8 @@ $ curl -sLO http://localhost:8080/test.txt
$ cat test.txt
If you read this then the static file server works!
```
+
+HTML5 Video Example
+-------------------
+
+Open http://localhost:8080/video.html in your favorite browser.
diff --git a/examples/static/priv/small.mp4 b/examples/static/priv/small.mp4
new file mode 100644
index 0000000..1fc4788
--- /dev/null
+++ b/examples/static/priv/small.mp4
Binary files differ
diff --git a/examples/static/priv/small.ogv b/examples/static/priv/small.ogv
new file mode 100644
index 0000000..6409d6e
--- /dev/null
+++ b/examples/static/priv/small.ogv
Binary files differ
diff --git a/examples/static/priv/video.html b/examples/static/priv/video.html
new file mode 100644
index 0000000..eca63ee
--- /dev/null
+++ b/examples/static/priv/video.html
@@ -0,0 +1,11 @@
+<!DOCTYPE html>
+<html>
+<body>
+ <h1>HTML5 Video Example</h1>
+ <video controls>
+ <source src="small.ogv" type="video/ogg"/>
+ <source src="small.mp4" type="video/mp4"/>
+ </video>
+ <p>Videos taken from <a href="http://techslides.com/sample-webm-ogg-and-mp4-video-files-for-html5/">TechSlides</a></p>
+</body>
+</html>
diff --git a/examples/static/src/static_app.erl b/examples/static/src/static_app.erl
index 16ef554..a2b9c31 100644
--- a/examples/static/src/static_app.erl
+++ b/examples/static/src/static_app.erl
@@ -11,16 +11,16 @@
%% API.
start(_Type, _Args) ->
- Dispatch = [
+ Dispatch = cowboy_router:compile([
{'_', [
- {['...'], cowboy_static, [
+ {"/[...]", cowboy_static, [
{directory, {priv_dir, static, []}},
{mimetypes, {fun mimetypes:path_to_mimes/2, default}}
]}
]}
- ],
+ ]),
{ok, _} = cowboy:start_http(http, 100, [{port, 8080}], [
- {dispatch, Dispatch}
+ {env, [{dispatch, Dispatch}]}
]),
static_sup:start_link().
diff --git a/examples/static/start.sh b/examples/static/start.sh
index ab15739..bc67846 100755
--- a/examples/static/start.sh
+++ b/examples/static/start.sh
@@ -1,3 +1,4 @@
#!/bin/sh
erl -pa ebin deps/*/ebin -s static \
- -eval "io:format(\"Point your browser at http://localhost:8080/test.txt~n\")."
+ -eval "io:format(\"Point your browser at http://localhost:8080/test.txt~n\")." \
+ -eval "io:format(\"Point your browser at http://localhost:8080/video.html~n\")."