Browse Source

Convert the web_server example to a release

Temporary mimetypes list here too.
Loïc Hoguin 11 years ago
parent
commit
6a90d00cee

+ 14 - 0
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

+ 13 - 20
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.

+ 0 - 8
examples/web_server/rebar.config

@@ -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"}}
-]}.

+ 2 - 0
examples/web_server/relx.config

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

+ 1 - 2
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]).

+ 0 - 15
examples/web_server/src/web_server.erl

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

+ 6 - 1
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">>]}
+				]}
 			]}
 		]}
 	]),

+ 0 - 3
examples/web_server/start.sh

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