0
Fork 0
mirror of https://github.com/ninenines/cowboy.git synced 2025-07-14 12:20:24 +00:00

Convert the REST pastebin example to a release

This commit is contained in:
Loïc Hoguin 2013-09-08 19:08:37 +02:00
parent dacaf5d293
commit 630a805541
7 changed files with 55 additions and 56 deletions

View file

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

View file

@ -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: To build the example, run the following command:
```
$ rebar get-deps compile ``` bash
$ make
``` ```
You can then start the Erlang node with the following command: To start the release in the foreground:
```
./start.sh ``` 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: To upload something to the paste application, you can use `curl`:
```
<command> | curl -i --data-urlencode paste@- localhost:8080 ``` bash
``` $ <command> | curl -i --data-urlencode paste@- localhost:8080
or to upload my_file:
``` ```
Or, to upload the file `my_file`:
``` bash
curl -i --data-urlencode paste@my_file localhost:8080 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=<language> option if Code that has been pasted can be highlighted with ?lang=<language> option if
you have [highlight](http://www.andre-simon.de/doku/highlight/en/highlight.html) 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 -i --data-urlencode paste@priv/index.html localhost:8080
curl <url from location header> curl <url from location header>
``` ```
Will show the text of the html file. If your terminal supports color If your terminal supports color sequences and `highlight` is installed,
sequences and highlight is installed: the following command will show the same contents but with HTML syntax
``` highlighting.
``` bash
curl <url from location header>?lang=html curl <url from location header>?lang=html
``` ```
Will show a syntax highlighted version of the source file. If you open the If you open the same URL in your web browser and your web browser tells
same URL in your web browser and your web browser tells cowboy that it prefers Cowboy that it prefers HTML files, you will see the file highlighted
html files, you will see the file highlighted with html/css markup. Firefox is with special HTML markup and CSS. Firefox is known to work.
known to work.

View file

@ -1,4 +0,0 @@
{deps, [
{cowboy, ".*",
{git, "git://github.com/extend/cowboy.git", "master"}}
]}.

View file

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

View file

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

View file

@ -3,14 +3,14 @@
%% @doc Pastebin handler. %% @doc Pastebin handler.
-module(toppage_handler). -module(toppage_handler).
%% REST Callbacks %% Standard callbacks.
-export([init/3]). -export([init/3]).
-export([allowed_methods/2]). -export([allowed_methods/2]).
-export([content_types_provided/2]). -export([content_types_provided/2]).
-export([content_types_accepted/2]). -export([content_types_accepted/2]).
-export([resource_exists/2]). -export([resource_exists/2]).
%% Callback Callbacks %% Custom callbacks.
-export([create_paste/2]). -export([create_paste/2]).
-export([paste_html/2]). -export([paste_html/2]).
-export([paste_text/2]). -export([paste_text/2]).
@ -75,8 +75,7 @@ read_file(Name) ->
Binary. Binary.
full_path(Name) -> full_path(Name) ->
{ok, Cwd} = file:get_cwd(), filename:join([code:priv_dir(rest_pastebin), Name]).
filename:join([Cwd, "priv", Name]).
file_exists(Name) -> file_exists(Name) ->
case file:read_file_info(full_path(Name)) of case file:read_file_info(full_path(Name)) of

View file

@ -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 <value of the location header>~n\")." \
-eval "io:format(\"Get with highlighting: curl <location>?lang=<language>~n\")." \
-eval "io:format(\"To get html, point your browser to http://localhost:8080~n\")."