mirror of
https://github.com/ninenines/cowboy.git
synced 2025-07-14 12:20:24 +00:00
Convert the GET echo example to a release
This commit is contained in:
parent
340f0a5112
commit
e2b11bbeed
7 changed files with 38 additions and 38 deletions
14
examples/echo_get/Makefile
Normal file
14
examples/echo_get/Makefile
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
PROJECT = echo_get
|
||||||
|
|
||||||
|
DEPS = cowboy
|
||||||
|
dep_cowboy = pkg://cowboy master
|
||||||
|
|
||||||
|
.PHONY: release clean-release
|
||||||
|
|
||||||
|
release: clean-release all
|
||||||
|
relx
|
||||||
|
|
||||||
|
clean-release:
|
||||||
|
rm -rf _rel
|
||||||
|
|
||||||
|
include ../../erlang.mk
|
|
@ -1,23 +1,28 @@
|
||||||
Cowboy GET Echo
|
GET parameter echo 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/get_echo_example console
|
||||||
```
|
```
|
||||||
|
|
||||||
Then point your browser to the indicated URL. You can change
|
Then point your browser at
|
||||||
the GET parameter to check that the handler is echoing properly.
|
[http://localhost:8080/?echo=hello](http://localhost:8080/?echo=hello).
|
||||||
|
You can replace the `echo` parameter with another to check
|
||||||
|
that the handler is echoing it back properly.
|
||||||
|
|
||||||
Example
|
Example output
|
||||||
-------
|
--------------
|
||||||
|
|
||||||
``` bash
|
``` bash
|
||||||
$ curl -i "http://localhost:8080/?echo=saymyname"
|
$ curl -i "http://localhost:8080/?echo=saymyname"
|
||||||
|
@ -26,7 +31,7 @@ connection: keep-alive
|
||||||
server: Cowboy
|
server: Cowboy
|
||||||
date: Fri, 28 Sep 2012 04:09:04 GMT
|
date: Fri, 28 Sep 2012 04:09:04 GMT
|
||||||
content-length: 9
|
content-length: 9
|
||||||
Content-Encoding: utf-8
|
content-type: text/plain; charset=utf-8
|
||||||
|
|
||||||
saymyname
|
saymyname
|
||||||
```
|
```
|
||||||
|
|
|
@ -1,4 +0,0 @@
|
||||||
{deps, [
|
|
||||||
{cowboy, ".*",
|
|
||||||
{git, "git://github.com/extend/cowboy.git", "master"}}
|
|
||||||
]}.
|
|
2
examples/echo_get/relx.config
Normal file
2
examples/echo_get/relx.config
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
{release, {echo_get_example, "1"}, [echo_get]}.
|
||||||
|
{extended_start_script, true}.
|
|
@ -1,15 +0,0 @@
|
||||||
%% Feel free to use, reuse and abuse the code in this file.
|
|
||||||
|
|
||||||
-module(echo_get).
|
|
||||||
|
|
||||||
%% 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(echo_get).
|
|
|
@ -19,8 +19,9 @@ handle(Req, State) ->
|
||||||
echo(<<"GET">>, undefined, Req) ->
|
echo(<<"GET">>, undefined, Req) ->
|
||||||
cowboy_req:reply(400, [], <<"Missing echo parameter.">>, Req);
|
cowboy_req:reply(400, [], <<"Missing echo parameter.">>, Req);
|
||||||
echo(<<"GET">>, Echo, Req) ->
|
echo(<<"GET">>, Echo, Req) ->
|
||||||
cowboy_req:reply(200,
|
cowboy_req:reply(200, [
|
||||||
[{<<"content-encoding">>, <<"utf-8">>}], Echo, Req);
|
{<<"content-type">>, <<"text/plain; charset=utf-8">>}
|
||||||
|
], Echo, Req);
|
||||||
echo(_, _, Req) ->
|
echo(_, _, Req) ->
|
||||||
%% Method not allowed.
|
%% Method not allowed.
|
||||||
cowboy_req:reply(405, Req).
|
cowboy_req:reply(405, Req).
|
||||||
|
|
|
@ -1,3 +0,0 @@
|
||||||
#!/bin/sh
|
|
||||||
erl -pa ebin deps/*/ebin -s echo_get \
|
|
||||||
-eval "io:format(\"Point your browser at http://localhost:8080/?echo=test~n\")."
|
|
Loading…
Add table
Add a link
Reference in a new issue