From 630a8055412bb9622ca202ffb7008d418df78d5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Hoguin?= Date: Sun, 8 Sep 2013 19:08:37 +0200 Subject: Convert the REST pastebin example to a release --- examples/rest_pastebin/Makefile | 14 ++++++ examples/rest_pastebin/README.md | 63 +++++++++++++++----------- examples/rest_pastebin/rebar.config | 4 -- examples/rest_pastebin/relx.config | 2 + examples/rest_pastebin/src/rest_pastebin.erl | 15 ------ examples/rest_pastebin/src/toppage_handler.erl | 7 ++- examples/rest_pastebin/start.sh | 6 --- 7 files changed, 55 insertions(+), 56 deletions(-) create mode 100644 examples/rest_pastebin/Makefile delete mode 100644 examples/rest_pastebin/rebar.config create mode 100644 examples/rest_pastebin/relx.config delete mode 100644 examples/rest_pastebin/src/rest_pastebin.erl delete mode 100755 examples/rest_pastebin/start.sh diff --git a/examples/rest_pastebin/Makefile b/examples/rest_pastebin/Makefile new file mode 100644 index 0000000..ba6b378 --- /dev/null +++ b/examples/rest_pastebin/Makefile @@ -0,0 +1,14 @@ +PROJECT = rest_pastebin + +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/rest_pastebin/README.md b/examples/rest_pastebin/README.md index 0e9d9b5..525cf39 100644 --- a/examples/rest_pastebin/README.md +++ b/examples/rest_pastebin/README.md @@ -1,29 +1,35 @@ -Cowboy Rest Hello World -======================= +REST pastebin 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/rest_pastebin_example console ``` -Then run any given command or point your browser to the indicated URL. +Then point your browser at [http://localhost:8080](http://localhost:8080). -Examples --------- +Usage +----- -To upload something to the paste application, you can use curl like: -``` - | curl -i --data-urlencode paste@- localhost:8080 -``` -or to upload my_file: +To upload something to the paste application, you can use `curl`: + +``` bash +$ | curl -i --data-urlencode paste@- localhost:8080 ``` + +Or, to upload the file `my_file`: + +``` bash curl -i --data-urlencode paste@my_file localhost:8080 ``` @@ -33,20 +39,23 @@ the form. Code that has been pasted can be highlighted with ?lang= option if you have [highlight](http://www.andre-simon.de/doku/highlight/en/highlight.html) -installed (although pygments or any other should work just fine). For example: -``` +installed (although `pygments` or any other should work just fine). + +This will show the contents of the HTML file: + +``` bash curl -i --data-urlencode paste@priv/index.html localhost:8080 curl ``` -Will show the text of the html file. If your terminal supports color -sequences and highlight is installed: -``` +If your terminal supports color sequences and `highlight` is installed, +the following command will show the same contents but with HTML syntax +highlighting. + +``` bash curl ?lang=html ``` -Will show a syntax highlighted version of the source file. If you open the -same URL in your web browser and your web browser tells cowboy that it prefers -html files, you will see the file highlighted with html/css markup. Firefox is -known to work. - +If you open the same URL in your web browser and your web browser tells +Cowboy that it prefers HTML files, you will see the file highlighted +with special HTML markup and CSS. Firefox is known to work. diff --git a/examples/rest_pastebin/rebar.config b/examples/rest_pastebin/rebar.config deleted file mode 100644 index 6ad3062..0000000 --- a/examples/rest_pastebin/rebar.config +++ /dev/null @@ -1,4 +0,0 @@ -{deps, [ - {cowboy, ".*", - {git, "git://github.com/extend/cowboy.git", "master"}} -]}. diff --git a/examples/rest_pastebin/relx.config b/examples/rest_pastebin/relx.config new file mode 100644 index 0000000..6b8a67c --- /dev/null +++ b/examples/rest_pastebin/relx.config @@ -0,0 +1,2 @@ +{release, {rest_pastebin_example, "1"}, [rest_pastebin]}. +{extended_start_script, true}. diff --git a/examples/rest_pastebin/src/rest_pastebin.erl b/examples/rest_pastebin/src/rest_pastebin.erl deleted file mode 100644 index e5bd1b5..0000000 --- a/examples/rest_pastebin/src/rest_pastebin.erl +++ /dev/null @@ -1,15 +0,0 @@ -%% Feel free to use, reuse and abuse the code in this file. - --module(rest_pastebin). - -%% 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(rest_pastebin). diff --git a/examples/rest_pastebin/src/toppage_handler.erl b/examples/rest_pastebin/src/toppage_handler.erl index 94f08aa..be2b803 100644 --- a/examples/rest_pastebin/src/toppage_handler.erl +++ b/examples/rest_pastebin/src/toppage_handler.erl @@ -3,14 +3,14 @@ %% @doc Pastebin handler. -module(toppage_handler). -%% REST Callbacks +%% Standard callbacks. -export([init/3]). -export([allowed_methods/2]). -export([content_types_provided/2]). -export([content_types_accepted/2]). -export([resource_exists/2]). -%% Callback Callbacks +%% Custom callbacks. -export([create_paste/2]). -export([paste_html/2]). -export([paste_text/2]). @@ -75,8 +75,7 @@ read_file(Name) -> Binary. full_path(Name) -> - {ok, Cwd} = file:get_cwd(), - filename:join([Cwd, "priv", Name]). + filename:join([code:priv_dir(rest_pastebin), Name]). file_exists(Name) -> case file:read_file_info(full_path(Name)) of diff --git a/examples/rest_pastebin/start.sh b/examples/rest_pastebin/start.sh deleted file mode 100755 index 20e9375..0000000 --- a/examples/rest_pastebin/start.sh +++ /dev/null @@ -1,6 +0,0 @@ -#!/bin/sh -erl -pa ebin deps/*/ebin -s rest_pastebin \ - -eval "io:format(\"Upload: echo foo | curl -i --data-urlencode paste@- localhost:8080~n\")." \ - -eval "io:format(\"Get: curl ~n\")." \ - -eval "io:format(\"Get with highlighting: curl ?lang=~n\")." \ - -eval "io:format(\"To get html, point your browser to http://localhost:8080~n\")." -- cgit v1.2.3