0
Fork 0
mirror of https://github.com/ninenines/cowboy.git synced 2025-07-14 20:30:23 +00:00
cowboy/examples/rest_pastebin
Loïc Hoguin 0dc063ab7d Improve handler interface and documentation
This change simplifies a little more the sub protocols mechanism.
Aliases have been removed. The renaming of loop handlers as long
polling handlers has been reverted.

Plain HTTP handlers now simply do their work in the init/2
callback. There is no specific code for them.

Loop handlers now follow the same return value as Websocket,
they use ok to continue and shutdown to stop.

Terminate reasons for all handler types have been documented.
The terminate callback is now appropriately called in all cases
(or should be).

Behaviors for all handler types have been moved in the module
that implement them. This means that cowboy_handler replaces
the cowboy_http_handler behavior, and similarly cowboy_loop
replaces cowboy_loop_handler, cowboy_websocket replaces
cowboy_websocket_handler. Finally cowboy_rest now has the
start of a behavior in it and will have the full list of
optional callbacks defined once Erlang 18.0 gets released.

The guide has been reorganized and should be easier to follow.
2014-09-30 20:12:13 +03:00
..
priv Add a more involved REST example 2013-02-07 11:00:26 -06:00
src Improve handler interface and documentation 2014-09-30 20:12:13 +03:00
Makefile Update to erlang.mk 1.0.0 2014-08-01 14:26:51 +02:00
README.md Update erlang.mk and update paths to start the release 2014-06-30 10:14:05 +02:00
relx.config Convert the REST pastebin example to a release 2013-09-08 19:08:37 +02:00

REST pastebin example

To try this example, you need GNU make and git in your PATH.

To build the example, run the following command:

$ make

To start the release in the foreground:

$ ./_rel/rest_pastebin_example/bin/rest_pastebin_example console

Then point your browser at http://localhost:8080.

Usage

To upload something to the paste application, you can use curl:

$ <command> | curl -i --data-urlencode paste@- localhost:8080

Or, to upload the file my_file:

curl -i --data-urlencode paste@my_file localhost:8080

The URL of your data will be in the location header. Alternately, you can visit http://localhost:8080 with your favorite web browser and submit your paste via the form.

Code that has been pasted can be highlighted with ?lang= option if you have highlight installed (although pygments or any other should work just fine).

This will show the contents of the HTML file:

curl -i --data-urlencode paste@priv/index.html localhost:8080
curl <url from location header>

If your terminal supports color sequences and highlight is installed, the following command will show the same contents but with HTML syntax highlighting.

curl <url from location header>?lang=html

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.