diff options
-rw-r--r-- | examples/web_server/Makefile | 14 | ||||
-rw-r--r-- | examples/web_server/README.md | 33 | ||||
-rw-r--r-- | examples/web_server/rebar.config | 8 | ||||
-rw-r--r-- | examples/web_server/relx.config | 2 | ||||
-rw-r--r-- | examples/web_server/src/directory_lister.erl | 3 | ||||
-rw-r--r-- | examples/web_server/src/web_server.erl | 15 | ||||
-rw-r--r-- | examples/web_server/src/web_server_app.erl | 7 | ||||
-rwxr-xr-x | examples/web_server/start.sh | 3 |
8 files changed, 36 insertions, 49 deletions
diff --git a/examples/web_server/Makefile b/examples/web_server/Makefile new file mode 100644 index 0000000..57cd131 --- /dev/null +++ b/examples/web_server/Makefile @@ -0,0 +1,14 @@ +PROJECT = web_server + +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/web_server/README.md b/examples/web_server/README.md index c123c98..b81a955 100644 --- a/examples/web_server/README.md +++ b/examples/web_server/README.md @@ -1,27 +1,20 @@ -Cowboy Static File Handler with Index Support -============================================= +Static file handler 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: -``` -./start.sh +``` bash +$ make ``` -Cowboy will serve all the files you put in the priv/ directory. You can replace -the filename given in the example URL with the one of a file you added to this -directory to receive that file. A middleware has been added that will re-route -the request to a different handler if the requested path is a directory. +To start the release in the foreground: -Example -------- +``` bash +$ ./_rel/bin/web_server_example console +``` -Point your browser to http://localhost:8080 to see the contents of `priv/`. You -can click on a link to see that file. If HTML is not preferred, the contents of -a directory will be listed as a JSON array (e.g. with `curl -http://localhost:8080`). +Then point your browser at [http://localhost:8080](http://localhost:8080) +to browse the contents of the `priv` directory. diff --git a/examples/web_server/rebar.config b/examples/web_server/rebar.config deleted file mode 100644 index f9cbdef..0000000 --- a/examples/web_server/rebar.config +++ /dev/null @@ -1,8 +0,0 @@ -{deps, [ - {cowboy, ".*", - {git, "git://github.com/extend/cowboy.git", "master"}}, - {mimetypes, ".*", - {git, "git://github.com/spawngrid/mimetypes.git", "master"}}, - {jsx, ".*", - {git, "git://github.com/talentdeficit/jsx.git", "master"}} -]}. diff --git a/examples/web_server/relx.config b/examples/web_server/relx.config new file mode 100644 index 0000000..305ab94 --- /dev/null +++ b/examples/web_server/relx.config @@ -0,0 +1,2 @@ +{release, {web_server_example, "1"}, [web_server]}. +{extended_start_script, true}. diff --git a/examples/web_server/src/directory_lister.erl b/examples/web_server/src/directory_lister.erl index a59f957..aa36314 100644 --- a/examples/web_server/src/directory_lister.erl +++ b/examples/web_server/src/directory_lister.erl @@ -33,5 +33,4 @@ valid_path([<<"/", _/binary>> | _T]) -> false; valid_path([_H | Rest]) -> valid_path(Rest). resource_path(Path) -> - {ok, Cwd} = file:get_cwd(), - filename:join([Cwd, "priv", Path]). + filename:join([code:priv_dir(web_server), Path]). diff --git a/examples/web_server/src/web_server.erl b/examples/web_server/src/web_server.erl deleted file mode 100644 index ac3f1b1..0000000 --- a/examples/web_server/src/web_server.erl +++ /dev/null @@ -1,15 +0,0 @@ -%% Feel free to use, reuse and abuse the code in this file. - --module(web_server). - -%% 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(web_server). diff --git a/examples/web_server/src/web_server_app.erl b/examples/web_server/src/web_server_app.erl index 43dc078..988a8fb 100644 --- a/examples/web_server/src/web_server_app.erl +++ b/examples/web_server/src/web_server_app.erl @@ -16,7 +16,12 @@ start(_Type, _Args) -> {"/[...]", cowboy_static, [ {directory, {priv_dir, web_server, []}}, {dir_handler, directory_handler}, - {mimetypes, {fun mimetypes:path_to_mimes/2, default}} + {mimetypes, [ + {<<".html">>, [<<"text/html">>]}, + {<<".txt">>, [<<"text/plain">>]}, + {<<".mp4">>, [<<"video/mp4">>]}, + {<<".ogv">>, [<<"video/ogg">>]} + ]} ]} ]} ]), diff --git a/examples/web_server/start.sh b/examples/web_server/start.sh deleted file mode 100755 index ede9bf9..0000000 --- a/examples/web_server/start.sh +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/sh -erl -pa ebin deps/*/ebin -s web_server \ - -eval "io:format(\"Point your browser at http://localhost:8080/~n\")." |