diff options
author | Loïc Hoguin <[email protected]> | 2013-09-07 16:18:51 +0200 |
---|---|---|
committer | Loïc Hoguin <[email protected]> | 2013-09-07 16:18:51 +0200 |
commit | 51ce122858c4dc09f4440269a412a91afb19ebe7 (patch) | |
tree | e68208030764cd08c78573d3871936001538d29c /examples | |
parent | 335676876b3f23cdc19450bd5eea68105e8fe142 (diff) | |
download | cowboy-51ce122858c4dc09f4440269a412a91afb19ebe7.tar.gz cowboy-51ce122858c4dc09f4440269a412a91afb19ebe7.tar.bz2 cowboy-51ce122858c4dc09f4440269a412a91afb19ebe7.zip |
Convert the basic auth example to a release
Diffstat (limited to 'examples')
-rw-r--r-- | examples/basic_auth/README.md | 35 | ||||
-rw-r--r-- | examples/basic_auth/rebar.config | 4 | ||||
-rw-r--r-- | examples/basic_auth/src/basic_auth.erl | 15 | ||||
-rw-r--r-- | examples/basic_auth/src/toppage_handler.erl | 17 | ||||
-rwxr-xr-x | examples/basic_auth/start.sh | 4 |
5 files changed, 27 insertions, 48 deletions
diff --git a/examples/basic_auth/README.md b/examples/basic_auth/README.md index 38ae9a2..eea6fd9 100644 --- a/examples/basic_auth/README.md +++ b/examples/basic_auth/README.md @@ -1,24 +1,28 @@ -Cowboy Basic Authorization Rest Hello World -=========================================== +Basic authorization example using REST +====================================== -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/hello_world_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 --------- +Example output +-------------- + +Request with no authentication: -### Get 401 ``` bash $ curl -i http://localhost:8080 HTTP/1.1 401 Unauthorized @@ -26,10 +30,11 @@ connection: keep-alive server: Cowboy date: Sun, 20 Jan 2013 14:10:27 GMT content-length: 0 -www-authenticate: Restricted +www-authenticate: Basic realm="cowboy" ``` -### Get 200 +Request with authentication: + ``` bash $ curl -i -u "Alladin:open sesame" http://localhost:8080 HTTP/1.1 200 OK diff --git a/examples/basic_auth/rebar.config b/examples/basic_auth/rebar.config deleted file mode 100644 index 6ad3062..0000000 --- a/examples/basic_auth/rebar.config +++ /dev/null @@ -1,4 +0,0 @@ -{deps, [ - {cowboy, ".*", - {git, "git://github.com/extend/cowboy.git", "master"}} -]}. diff --git a/examples/basic_auth/src/basic_auth.erl b/examples/basic_auth/src/basic_auth.erl deleted file mode 100644 index d54c677..0000000 --- a/examples/basic_auth/src/basic_auth.erl +++ /dev/null @@ -1,15 +0,0 @@ -%% Feel free to use, reuse and abuse the code in this file. - --module(basic_auth). - -%% 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(basic_auth). diff --git a/examples/basic_auth/src/toppage_handler.erl b/examples/basic_auth/src/toppage_handler.erl index dc044a5..2914064 100644 --- a/examples/basic_auth/src/toppage_handler.erl +++ b/examples/basic_auth/src/toppage_handler.erl @@ -1,32 +1,29 @@ %% Feel free to use, reuse and abuse the code in this file. -%% @doc Basic authorization Hello world handler. +%% @doc Handler with basic HTTP authorization. -module(toppage_handler). -export([init/3]). -export([content_types_provided/2]). -export([is_authorized/2]). --export([hello_to_text/2]). +-export([to_text/2]). init(_Transport, _Req, []) -> {upgrade, protocol, cowboy_rest}. - -is_authorized(Req, S) -> +is_authorized(Req, State) -> {ok, Auth, Req1} = cowboy_req:parse_header(<<"authorization">>, Req), case Auth of {<<"basic">>, {User = <<"Alladin">>, <<"open sesame">>}} -> {true, Req1, User}; _ -> - {{false, <<"Basic realm=\"cowboy\"">>}, Req1, S} + {{false, <<"Basic realm=\"cowboy\"">>}, Req1, State} end. content_types_provided(Req, State) -> {[ - {<<"text/plain">>, hello_to_text} + {<<"text/plain">>, to_text} ], Req, State}. - -hello_to_text(Req, User) -> - {<< <<"Hello, ">>/binary, User/binary, <<"!\n">>/binary >>, Req, User}. - +to_text(Req, User) -> + {<< "Hello, ", User/binary, "!\n" >>, Req, User}. diff --git a/examples/basic_auth/start.sh b/examples/basic_auth/start.sh deleted file mode 100755 index 9e8a30b..0000000 --- a/examples/basic_auth/start.sh +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/sh -erl -pa ebin deps/*/ebin -s basic_auth \ - -eval "io:format(\"Get 401: curl -i http://localhost:8080~n\")." \ - -eval "io:format(\"Get 200: curl -i -u \\\"Alladin:open sesame\\\" http://localhost:8080~n\")." |