From 834056402ac4a94baa1ab622f127dfdd84826bc3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Hoguin?= Date: Mon, 9 Sep 2013 16:21:53 +0200 Subject: Convert the websocket example to a release --- examples/websocket/Makefile | 14 ++++ examples/websocket/README.md | 24 +++--- examples/websocket/priv/html_ws_client.html | 112 ---------------------------- examples/websocket/priv/index.html | 112 ++++++++++++++++++++++++++++ examples/websocket/rebar.config | 6 -- examples/websocket/relx.config | 2 + examples/websocket/src/toppage_handler.erl | 26 ------- examples/websocket/src/websocket.erl | 13 ---- examples/websocket/src/websocket_app.erl | 8 +- examples/websocket/start.sh | 4 - 10 files changed, 147 insertions(+), 174 deletions(-) create mode 100644 examples/websocket/Makefile delete mode 100644 examples/websocket/priv/html_ws_client.html create mode 100644 examples/websocket/priv/index.html delete mode 100644 examples/websocket/rebar.config create mode 100644 examples/websocket/relx.config delete mode 100644 examples/websocket/src/toppage_handler.erl delete mode 100644 examples/websocket/src/websocket.erl delete mode 100755 examples/websocket/start.sh (limited to 'examples/websocket') diff --git a/examples/websocket/Makefile b/examples/websocket/Makefile new file mode 100644 index 0000000..0c6826c --- /dev/null +++ b/examples/websocket/Makefile @@ -0,0 +1,14 @@ +PROJECT = websocket + +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/websocket/README.md b/examples/websocket/README.md index f3b4cbd..d4e3982 100644 --- a/examples/websocket/README.md +++ b/examples/websocket/README.md @@ -1,17 +1,19 @@ -Cowboy websocket -================ +Websocket 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: +``` bash +$ make ``` -./start.sh + +To start the release in the foreground: + +``` bash +$ ./_rel/bin/websocket_example console ``` -Then point your browser to the indicated URL to open a websocket client. -Not all browsers support websockets. It was tested with Chromium. +Then point your browser at [http://localhost:8080](http://localhost:8080). diff --git a/examples/websocket/priv/html_ws_client.html b/examples/websocket/priv/html_ws_client.html deleted file mode 100644 index 5bc7f15..0000000 --- a/examples/websocket/priv/html_ws_client.html +++ /dev/null @@ -1,112 +0,0 @@ - - - - Websocket client - - - - - - - - - - - diff --git a/examples/websocket/priv/index.html b/examples/websocket/priv/index.html new file mode 100644 index 0000000..5bc7f15 --- /dev/null +++ b/examples/websocket/priv/index.html @@ -0,0 +1,112 @@ + + + + Websocket client + + + + + + + + + + + diff --git a/examples/websocket/rebar.config b/examples/websocket/rebar.config deleted file mode 100644 index eb6f194..0000000 --- a/examples/websocket/rebar.config +++ /dev/null @@ -1,6 +0,0 @@ -{deps, [ - {cowboy, ".*", - {git, "git://github.com/extend/cowboy.git", "master"}}, - {mimetypes, ".*", - {git, "git://github.com/spawngrid/mimetypes.git", "master"}} -]}. diff --git a/examples/websocket/relx.config b/examples/websocket/relx.config new file mode 100644 index 0000000..227589b --- /dev/null +++ b/examples/websocket/relx.config @@ -0,0 +1,2 @@ +{release, {websocket_example, "1"}, [websocket]}. +{extended_start_script, true}. diff --git a/examples/websocket/src/toppage_handler.erl b/examples/websocket/src/toppage_handler.erl deleted file mode 100644 index b315a19..0000000 --- a/examples/websocket/src/toppage_handler.erl +++ /dev/null @@ -1,26 +0,0 @@ -%% Feel free to use, reuse and abuse the code in this file. - --module(toppage_handler). - --export([init/3]). --export([handle/2]). --export([terminate/3]). - -init(_Transport, Req, []) -> - {ok, Req, undefined}. - -handle(Req, State) -> - Html = get_html(), - {ok, Req2} = cowboy_req:reply(200, - [{<<"content-type">>, <<"text/html">>}], - Html, Req), - {ok, Req2, State}. - -terminate(_Reason, _Req, _State) -> - ok. - -get_html() -> - {ok, Cwd} = file:get_cwd(), - Filename =filename:join([Cwd, "priv", "html_ws_client.html"]), - {ok, Binary} = file:read_file(Filename), - Binary. diff --git a/examples/websocket/src/websocket.erl b/examples/websocket/src/websocket.erl deleted file mode 100644 index 23afe13..0000000 --- a/examples/websocket/src/websocket.erl +++ /dev/null @@ -1,13 +0,0 @@ -%% Feel free to use, reuse and abuse the code in this file. - --module(websocket). - -%% API. --export([start/0]). - -start() -> - ok = application:start(crypto), - ok = application:start(cowlib), - ok = application:start(ranch), - ok = application:start(cowboy), - ok = application:start(websocket). diff --git a/examples/websocket/src/websocket_app.erl b/examples/websocket/src/websocket_app.erl index 049c524..1b9a421 100644 --- a/examples/websocket/src/websocket_app.erl +++ b/examples/websocket/src/websocket_app.erl @@ -12,11 +12,15 @@ start(_Type, _Args) -> Dispatch = cowboy_router:compile([ {'_', [ - {"/", toppage_handler, []}, + {"/", cowboy_static, [ + {directory, {priv_dir, websocket, []}}, + {file, <<"index.html">>}, + {mimetypes, [{<<".html">>, [<<"text/html">>]}]} + ]}, {"/websocket", ws_handler, []}, {"/static/[...]", cowboy_static, [ {directory, {priv_dir, websocket, [<<"static">>]}}, - {mimetypes, {fun mimetypes:path_to_mimes/2, default}} + {mimetypes, [{<<".js">>, [<<"application/javascript">>]}]} ]} ]} ]), diff --git a/examples/websocket/start.sh b/examples/websocket/start.sh deleted file mode 100755 index d2ad16a..0000000 --- a/examples/websocket/start.sh +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/sh -erl -pa ebin deps/*/ebin -s websocket \ - -eval "io:format(\"Point your browser at http://localhost:8080/ to use a simple websocket client~n\")." - -- cgit v1.2.3