aboutsummaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
Diffstat (limited to 'examples')
-rw-r--r--examples/README.asciidoc9
-rw-r--r--examples/file_server/Makefile8
-rw-r--r--examples/file_server/README.asciidoc89
-rw-r--r--examples/file_server/priv/small.mp4 (renamed from examples/static_world/priv/small.mp4)bin383631 -> 383631 bytes
-rw-r--r--examples/file_server/priv/small.ogv (renamed from examples/static_world/priv/small.ogv)bin872453 -> 872453 bytes
-rw-r--r--examples/file_server/priv/test.txt (renamed from examples/static_world/priv/test.txt)0
-rw-r--r--examples/file_server/priv/video.html (renamed from examples/static_world/priv/video.html)2
-rw-r--r--examples/file_server/relx.config2
-rw-r--r--examples/file_server/src/directory_handler.erl (renamed from examples/web_server/src/directory_handler.erl)6
-rw-r--r--examples/file_server/src/directory_lister.erl (renamed from examples/web_server/src/directory_lister.erl)16
-rw-r--r--examples/file_server/src/file_server_app.erl (renamed from examples/web_server/src/web_server_app.erl)14
-rw-r--r--examples/file_server/src/file_server_sup.erl (renamed from examples/web_server/src/web_server_sup.erl)2
-rw-r--r--examples/static_world/Makefile8
-rw-r--r--examples/static_world/README.asciidoc30
-rw-r--r--examples/static_world/priv/index.html1
-rw-r--r--examples/static_world/relx.config2
-rw-r--r--examples/static_world/src/static_world_app.erl27
-rw-r--r--examples/static_world/src/static_world_sup.erl23
-rw-r--r--examples/web_server/Makefile8
-rw-r--r--examples/web_server/README.asciidoc11
-rw-r--r--examples/web_server/priv/small.mp4bin383631 -> 0 bytes
-rw-r--r--examples/web_server/priv/small.ogvbin872453 -> 0 bytes
-rw-r--r--examples/web_server/priv/test.txt1
-rw-r--r--examples/web_server/priv/video.html11
-rw-r--r--examples/web_server/relx.config2
25 files changed, 120 insertions, 152 deletions
diff --git a/examples/README.asciidoc b/examples/README.asciidoc
index 965aba5..7e5c6e0 100644
--- a/examples/README.asciidoc
+++ b/examples/README.asciidoc
@@ -21,6 +21,9 @@
* link:eventsource[]:
eventsource emitter and consumer
+* link:file_server[]:
+ file server with directory listing
+
* link:hello_world[]:
simplest example application
@@ -42,15 +45,9 @@
* link:ssl_hello_world[]:
simplest SSL application
-* link:static_world[]:
- static file handler
-
* link:upload[]:
multipart/form-data upload
-* link:web_server[]:
- serve files with lists directory entries
-
* link:websocket[]:
websocket example
diff --git a/examples/file_server/Makefile b/examples/file_server/Makefile
new file mode 100644
index 0000000..7efa6ef
--- /dev/null
+++ b/examples/file_server/Makefile
@@ -0,0 +1,8 @@
+PROJECT = file_server
+PROJECT_DESCRIPTION = Cowboy file server example with directory listing
+PROJECT_VERSION = 1
+
+DEPS = cowboy jsx
+dep_cowboy_commit = master
+
+include ../../erlang.mk
diff --git a/examples/file_server/README.asciidoc b/examples/file_server/README.asciidoc
new file mode 100644
index 0000000..f6bc61c
--- /dev/null
+++ b/examples/file_server/README.asciidoc
@@ -0,0 +1,89 @@
+= File server example with directory listing
+
+To try this example, you need GNU `make` and `git` in your PATH.
+
+To build and run the example, use the following command:
+
+[source,bash]
+$ make run
+
+Then point your browser to http://localhost:8080
+to browse the contents of the `priv` directory.
+
+Interesting examples include:
+
+* http://localhost:8080/test.txt[Plain text file]
+* http://localhost:8080/video.html[HTML5 video demo]
+
+== HTTP/1.1 example output
+
+[source,bash]
+----
+$ curl -i http://localhost:8080/test.txt
+HTTP/1.1 200 OK
+connection: keep-alive
+server: Cowboy
+date: Mon, 09 Sep 2013 13:49:50 GMT
+content-length: 52
+content-type: text/plain
+last-modified: Fri, 18 Jan 2013 16:33:31 GMT
+
+If you read this then the static file server works!
+----
+
+== HTTP/2 example output
+
+[source,bash]
+----
+$ nghttp -v http://localhost:8080/test.txt
+[ 0.000] Connected
+[ 0.000] send SETTINGS frame <length=12, flags=0x00, stream_id=0>
+ (niv=2)
+ [SETTINGS_MAX_CONCURRENT_STREAMS(0x03):100]
+ [SETTINGS_INITIAL_WINDOW_SIZE(0x04):65535]
+[ 0.000] send PRIORITY frame <length=5, flags=0x00, stream_id=3>
+ (dep_stream_id=0, weight=201, exclusive=0)
+[ 0.000] send PRIORITY frame <length=5, flags=0x00, stream_id=5>
+ (dep_stream_id=0, weight=101, exclusive=0)
+[ 0.000] send PRIORITY frame <length=5, flags=0x00, stream_id=7>
+ (dep_stream_id=0, weight=1, exclusive=0)
+[ 0.000] send PRIORITY frame <length=5, flags=0x00, stream_id=9>
+ (dep_stream_id=7, weight=1, exclusive=0)
+[ 0.000] send PRIORITY frame <length=5, flags=0x00, stream_id=11>
+ (dep_stream_id=3, weight=1, exclusive=0)
+[ 0.000] send HEADERS frame <length=46, flags=0x25, stream_id=13>
+ ; END_STREAM | END_HEADERS | PRIORITY
+ (padlen=0, dep_stream_id=11, weight=16, exclusive=0)
+ ; Open new stream
+ :method: GET
+ :path: /test.txt
+ :scheme: http
+ :authority: localhost:8080
+ accept: */*
+ accept-encoding: gzip, deflate
+ user-agent: nghttp2/1.7.1
+[ 0.001] recv SETTINGS frame <length=0, flags=0x00, stream_id=0>
+ (niv=0)
+[ 0.001] recv SETTINGS frame <length=0, flags=0x01, stream_id=0>
+ ; ACK
+ (niv=0)
+[ 0.001] send SETTINGS frame <length=0, flags=0x01, stream_id=0>
+ ; ACK
+ (niv=0)
+[ 0.007] recv (stream_id=13) :status: 200
+[ 0.007] recv (stream_id=13) content-length: 52
+[ 0.007] recv (stream_id=13) content-type: text/plain
+[ 0.007] recv (stream_id=13) date: Mon, 13 Jun 2016 11:25:20 GMT
+[ 0.007] recv (stream_id=13) etag: "1371478245"
+[ 0.007] recv (stream_id=13) last-modified: Tue, 12 Aug 2014 17:00:17 GMT
+[ 0.007] recv (stream_id=13) server: Cowboy
+[ 0.007] recv HEADERS frame <length=81, flags=0x04, stream_id=13>
+ ; END_HEADERS
+ (padlen=0)
+ ; First response header
+If you read this then the static file server works!
+[ 0.007] recv DATA frame <length=52, flags=0x01, stream_id=13>
+ ; END_STREAM
+[ 0.007] send GOAWAY frame <length=8, flags=0x00, stream_id=0>
+ (last_stream_id=0, error_code=NO_ERROR(0x00), opaque_data(0)=[])
+----
diff --git a/examples/static_world/priv/small.mp4 b/examples/file_server/priv/small.mp4
index 1fc4788..1fc4788 100644
--- a/examples/static_world/priv/small.mp4
+++ b/examples/file_server/priv/small.mp4
Binary files differ
diff --git a/examples/static_world/priv/small.ogv b/examples/file_server/priv/small.ogv
index 6409d6e..6409d6e 100644
--- a/examples/static_world/priv/small.ogv
+++ b/examples/file_server/priv/small.ogv
Binary files differ
diff --git a/examples/static_world/priv/test.txt b/examples/file_server/priv/test.txt
index 760cddb..760cddb 100644
--- a/examples/static_world/priv/test.txt
+++ b/examples/file_server/priv/test.txt
diff --git a/examples/static_world/priv/video.html b/examples/file_server/priv/video.html
index eca63ee..0830ae0 100644
--- a/examples/static_world/priv/video.html
+++ b/examples/file_server/priv/video.html
@@ -3,7 +3,7 @@
<body>
<h1>HTML5 Video Example</h1>
<video controls>
- <source src="small.ogv" type="video/ogg"/>
+ <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>
diff --git a/examples/file_server/relx.config b/examples/file_server/relx.config
new file mode 100644
index 0000000..9e365e9
--- /dev/null
+++ b/examples/file_server/relx.config
@@ -0,0 +1,2 @@
+{release, {file_server_example, "1"}, [file_server]}.
+{extended_start_script, true}.
diff --git a/examples/web_server/src/directory_handler.erl b/examples/file_server/src/directory_handler.erl
index 7af67ba..d16e014 100644
--- a/examples/web_server/src/directory_handler.erl
+++ b/examples/file_server/src/directory_handler.erl
@@ -27,12 +27,12 @@ resource_exists(Req, {ReqPath, FilePath}) ->
content_types_provided(Req, State) ->
{[
- {{<<"application">>, <<"json">>, []}, list_json},
- {{<<"text">>, <<"html">>, []}, list_html}
+ {{<<"text">>, <<"html">>, []}, list_html},
+ {{<<"application">>, <<"json">>, []}, list_json}
], Req, State}.
list_json(Req, {Path, Fs}) ->
- Files = [[ <<(list_to_binary(F))/binary>> || F <- Fs ]],
+ Files = [ <<(list_to_binary(F))/binary>> || F <- Fs ],
{jsx:encode(Files), Req, Path}.
list_html(Req, {Path, Fs}) ->
diff --git a/examples/web_server/src/directory_lister.erl b/examples/file_server/src/directory_lister.erl
index bc34073..2ca9d30 100644
--- a/examples/web_server/src/directory_lister.erl
+++ b/examples/file_server/src/directory_lister.erl
@@ -5,16 +5,14 @@
-export([execute/2]).
+execute(Req, Env=#{handler := cowboy_static}) ->
+ redirect_directory(Req, Env);
execute(Req, Env) ->
- case lists:keyfind(handler, 1, Env) of
- {handler, cowboy_static} -> redirect_directory(Req, Env);
- _H -> {ok, Req, Env}
- end.
+ {ok, Req, Env}.
-redirect_directory(Req, Env) ->
+redirect_directory(Req, Env=#{handler_opts := {_, _, _, Extra}}) ->
Path = cowboy_req:path_info(Req),
Path1 = << <<S/binary, $/>> || S <- Path >>,
- {handler_opts, {_, _, _, Extra}} = lists:keyfind(handler_opts, 1, Env),
{dir_handler, DirHandler} = lists:keyfind(dir_handler, 1, Extra),
FullPath = resource_path(Path1),
case valid_path(Path) and filelib:is_dir(FullPath) of
@@ -23,9 +21,7 @@ redirect_directory(Req, Env) ->
end.
handle_directory(Req, Env, Prefix, Path, DirHandler) ->
- Env1 = lists:keydelete(handler, 1,
- lists:keydelete(handler_opts, 1, Env)),
- {ok, Req, [{handler, DirHandler}, {handler_opts, {Prefix, Path}} | Env1]}.
+ {ok, Req, Env#{handler => DirHandler, handler_opts => {Prefix, Path}}}.
valid_path([]) -> true;
valid_path([<<"..">> | _T]) -> false;
@@ -33,4 +29,4 @@ valid_path([<<"/", _/binary>> | _T]) -> false;
valid_path([_H | Rest]) -> valid_path(Rest).
resource_path(Path) ->
- filename:join([code:priv_dir(web_server), Path]).
+ filename:join([code:priv_dir(file_server), Path]).
diff --git a/examples/web_server/src/web_server_app.erl b/examples/file_server/src/file_server_app.erl
index e32d947..49924ba 100644
--- a/examples/web_server/src/web_server_app.erl
+++ b/examples/file_server/src/file_server_app.erl
@@ -1,7 +1,7 @@
%% Feel free to use, reuse and abuse the code in this file.
%% @private
--module(web_server_app).
+-module(file_server_app).
-behaviour(application).
%% API.
@@ -13,17 +13,17 @@
start(_Type, _Args) ->
Dispatch = cowboy_router:compile([
{'_', [
- {"/[...]", cowboy_static, {priv_dir, web_server, "", [
+ {"/[...]", cowboy_static, {priv_dir, file_server, "", [
{mimetypes, cow_mimetypes, all},
{dir_handler, directory_handler}
]}}
]}
]),
- {ok, _} = cowboy:start_http(http, 100, [{port, 8080}], [
- {env, [{dispatch, Dispatch}]},
- {middlewares, [cowboy_router, directory_lister, cowboy_handler]}
- ]),
- web_server_sup:start_link().
+ {ok, _} = cowboy:start_clear(http, 100, [{port, 8080}], #{
+ env => #{dispatch => Dispatch},
+ middlewares => [cowboy_router, directory_lister, cowboy_handler]
+ }),
+ file_server_sup:start_link().
stop(_State) ->
ok.
diff --git a/examples/web_server/src/web_server_sup.erl b/examples/file_server/src/file_server_sup.erl
index 03f9f67..961eb5e 100644
--- a/examples/web_server/src/web_server_sup.erl
+++ b/examples/file_server/src/file_server_sup.erl
@@ -1,7 +1,7 @@
%% Feel free to use, reuse and abuse the code in this file.
%% @private
--module(web_server_sup).
+-module(file_server_sup).
-behaviour(supervisor).
%% API.
diff --git a/examples/static_world/Makefile b/examples/static_world/Makefile
deleted file mode 100644
index cbce76c..0000000
--- a/examples/static_world/Makefile
+++ /dev/null
@@ -1,8 +0,0 @@
-PROJECT = static_world
-PROJECT_DESCRIPTION = Cowboy static file handler example
-PROJECT_VERSION = 1
-
-DEPS = cowboy
-dep_cowboy_commit = master
-
-include ../../erlang.mk
diff --git a/examples/static_world/README.asciidoc b/examples/static_world/README.asciidoc
deleted file mode 100644
index 3f09900..0000000
--- a/examples/static_world/README.asciidoc
+++ /dev/null
@@ -1,30 +0,0 @@
-= Static file handler example
-
-To try this example, you need GNU `make` and `git` in your PATH.
-
-To build and run the example, use the following command:
-
-[source,bash]
-$ make run
-
-The example will serve all the files found in the 'priv/'
-directory. For example:
-
-* http://localhost:8080/test.txt[Plain text file]
-* http://localhost:8080/video.html[HTML5 video demo]
-
-== Example output
-
-[source,bash]
-----
-$ curl -i http://localhost:8080/test.txt
-HTTP/1.1 200 OK
-connection: keep-alive
-server: Cowboy
-date: Mon, 09 Sep 2013 13:49:50 GMT
-content-length: 52
-content-type: text/plain
-last-modified: Fri, 18 Jan 2013 16:33:31 GMT
-
-If you read this then the static file server works!
-----
diff --git a/examples/static_world/priv/index.html b/examples/static_world/priv/index.html
deleted file mode 100644
index 2ebe508..0000000
--- a/examples/static_world/priv/index.html
+++ /dev/null
@@ -1 +0,0 @@
-<h1>Howdy, Pardner</h1>
diff --git a/examples/static_world/relx.config b/examples/static_world/relx.config
deleted file mode 100644
index a480902..0000000
--- a/examples/static_world/relx.config
+++ /dev/null
@@ -1,2 +0,0 @@
-{release, {static_world_example, "1"}, [static_world]}.
-{extended_start_script, true}.
diff --git a/examples/static_world/src/static_world_app.erl b/examples/static_world/src/static_world_app.erl
deleted file mode 100644
index 4d194c7..0000000
--- a/examples/static_world/src/static_world_app.erl
+++ /dev/null
@@ -1,27 +0,0 @@
-%% Feel free to use, reuse and abuse the code in this file.
-
-%% @private
--module(static_world_app).
--behaviour(application).
-
-%% API.
--export([start/2]).
--export([stop/1]).
-
-%% API.
-
-start(_Type, _Args) ->
- Dispatch = cowboy_router:compile([
- {'_', [
- {"/", cowboy_static, {priv_file, static_world, "index.html"}},
- {"/[...]", cowboy_static, {priv_dir, static_world, "",
- [{mimetypes, cow_mimetypes, all}]}}
- ]}
- ]),
- {ok, _} = cowboy:start_http(http, 100, [{port, 8080}], [
- {env, [{dispatch, Dispatch}]}
- ]),
- static_world_sup:start_link().
-
-stop(_State) ->
- ok.
diff --git a/examples/static_world/src/static_world_sup.erl b/examples/static_world/src/static_world_sup.erl
deleted file mode 100644
index a57d07a..0000000
--- a/examples/static_world/src/static_world_sup.erl
+++ /dev/null
@@ -1,23 +0,0 @@
-%% Feel free to use, reuse and abuse the code in this file.
-
-%% @private
--module(static_world_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/web_server/Makefile b/examples/web_server/Makefile
deleted file mode 100644
index 34ec805..0000000
--- a/examples/web_server/Makefile
+++ /dev/null
@@ -1,8 +0,0 @@
-PROJECT = web_server
-PROJECT_DESCRIPTION = Cowboy static directory indexing example
-PROJECT_VERSION = 1
-
-DEPS = cowboy
-dep_cowboy_commit = master
-
-include ../../erlang.mk
diff --git a/examples/web_server/README.asciidoc b/examples/web_server/README.asciidoc
deleted file mode 100644
index 8f0eb4b..0000000
--- a/examples/web_server/README.asciidoc
+++ /dev/null
@@ -1,11 +0,0 @@
-= Directory indexing example
-
-To try this example, you need GNU `make` and `git` in your PATH.
-
-To build and run the example, use the following command:
-
-[source,bash]
-$ make run
-
-Then point your browser to http://localhost:8080
-to browse the contents of the `priv` directory.
diff --git a/examples/web_server/priv/small.mp4 b/examples/web_server/priv/small.mp4
deleted file mode 100644
index 1fc4788..0000000
--- a/examples/web_server/priv/small.mp4
+++ /dev/null
Binary files differ
diff --git a/examples/web_server/priv/small.ogv b/examples/web_server/priv/small.ogv
deleted file mode 100644
index 6409d6e..0000000
--- a/examples/web_server/priv/small.ogv
+++ /dev/null
Binary files differ
diff --git a/examples/web_server/priv/test.txt b/examples/web_server/priv/test.txt
deleted file mode 100644
index 760cddb..0000000
--- a/examples/web_server/priv/test.txt
+++ /dev/null
@@ -1 +0,0 @@
-If you read this then the static file server works!
diff --git a/examples/web_server/priv/video.html b/examples/web_server/priv/video.html
deleted file mode 100644
index eca63ee..0000000
--- a/examples/web_server/priv/video.html
+++ /dev/null
@@ -1,11 +0,0 @@
-<!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/web_server/relx.config b/examples/web_server/relx.config
deleted file mode 100644
index 305ab94..0000000
--- a/examples/web_server/relx.config
+++ /dev/null
@@ -1,2 +0,0 @@
-{release, {web_server_example, "1"}, [web_server]}.
-{extended_start_script, true}.