aboutsummaryrefslogtreecommitdiffstats
path: root/examples/cookie
diff options
context:
space:
mode:
authorLoïc Hoguin <[email protected]>2012-12-20 14:40:22 +0100
committerLoïc Hoguin <[email protected]>2012-12-20 14:40:22 +0100
commita07d063fd8fc8a1cfadcb35cc89e190695773fe8 (patch)
treeb1f9c4540b8ecdc43538945f6371177b48d3b1b5 /examples/cookie
parent659ca05c546ed8b537fe0d36cf5520ad0bc62a0f (diff)
downloadcowboy-a07d063fd8fc8a1cfadcb35cc89e190695773fe8.tar.gz
cowboy-a07d063fd8fc8a1cfadcb35cc89e190695773fe8.tar.bz2
cowboy-a07d063fd8fc8a1cfadcb35cc89e190695773fe8.zip
Add cookie example
Diffstat (limited to 'examples/cookie')
-rw-r--r--examples/cookie/README.md18
-rw-r--r--examples/cookie/rebar.config6
-rw-r--r--examples/cookie/src/cookie.app.src15
-rw-r--r--examples/cookie/src/cookie.erl14
-rw-r--r--examples/cookie/src/cookie_app.erl25
-rw-r--r--examples/cookie/src/cookie_sup.erl23
-rw-r--r--examples/cookie/src/toppage_handler.erl29
-rwxr-xr-xexamples/cookie/start.sh3
-rw-r--r--examples/cookie/templates/toppage.dtl24
9 files changed, 157 insertions, 0 deletions
diff --git a/examples/cookie/README.md b/examples/cookie/README.md
new file mode 100644
index 0000000..cb6465f
--- /dev/null
+++ b/examples/cookie/README.md
@@ -0,0 +1,18 @@
+Cowboy Cookie
+=============
+
+To compile this example you need rebar in your PATH.
+
+Type the following command:
+```
+$ rebar get-deps compile
+```
+
+You can then start the Erlang node with the following command:
+```
+./start.sh
+```
+
+Then point your browser to the indicated URL. This example allows
+you to use any path you want to try to show that cookies are defined
+site-wide. Try it in your browser!
diff --git a/examples/cookie/rebar.config b/examples/cookie/rebar.config
new file mode 100644
index 0000000..d1fecef
--- /dev/null
+++ b/examples/cookie/rebar.config
@@ -0,0 +1,6 @@
+{deps, [
+ {cowboy, ".*",
+ {git, "git://github.com/extend/cowboy.git", "master"}},
+ {erlydtl, ".*",
+ {git, "https://github.com/evanmiller/erlydtl.git", "master"}}
+]}.
diff --git a/examples/cookie/src/cookie.app.src b/examples/cookie/src/cookie.app.src
new file mode 100644
index 0000000..1e9cd3d
--- /dev/null
+++ b/examples/cookie/src/cookie.app.src
@@ -0,0 +1,15 @@
+%% Feel free to use, reuse and abuse the code in this file.
+
+{application, cookie, [
+ {description, "Cowboy Cookie example."},
+ {vsn, "1"},
+ {modules, []},
+ {registered, []},
+ {applications, [
+ kernel,
+ stdlib,
+ cowboy
+ ]},
+ {mod, {cookie_app, []}},
+ {env, []}
+]}.
diff --git a/examples/cookie/src/cookie.erl b/examples/cookie/src/cookie.erl
new file mode 100644
index 0000000..455fcb0
--- /dev/null
+++ b/examples/cookie/src/cookie.erl
@@ -0,0 +1,14 @@
+%% Feel free to use, reuse and abuse the code in this file.
+
+-module(cookie).
+
+%% API.
+-export([start/0]).
+
+%% API.
+
+start() ->
+ ok = application:start(crypto),
+ ok = application:start(ranch),
+ ok = application:start(cowboy),
+ ok = application:start(cookie).
diff --git a/examples/cookie/src/cookie_app.erl b/examples/cookie/src/cookie_app.erl
new file mode 100644
index 0000000..195d6b6
--- /dev/null
+++ b/examples/cookie/src/cookie_app.erl
@@ -0,0 +1,25 @@
+%% Feel free to use, reuse and abuse the code in this file.
+
+%% @private
+-module(cookie_app).
+-behaviour(application).
+
+%% API.
+-export([start/2]).
+-export([stop/1]).
+
+%% API.
+
+start(_Type, _Args) ->
+ Dispatch = [
+ {'_', [
+ {'_', toppage_handler, []}
+ ]}
+ ],
+ {ok, _} = cowboy:start_http(http, 100, [{port, 8080}], [
+ {dispatch, Dispatch}
+ ]),
+ cookie_sup:start_link().
+
+stop(_State) ->
+ ok.
diff --git a/examples/cookie/src/cookie_sup.erl b/examples/cookie/src/cookie_sup.erl
new file mode 100644
index 0000000..5ba4e53
--- /dev/null
+++ b/examples/cookie/src/cookie_sup.erl
@@ -0,0 +1,23 @@
+%% Feel free to use, reuse and abuse the code in this file.
+
+%% @private
+-module(cookie_sup).
+-behaviour(supervisor).
+
+%% API.
+-export([start_link/0]).
+
+%% supervisor.
+-export([init/1]).
+
+%% API.
+
+-spec start_link() -> {ok, pid()}.
+start_link() ->
+ supervisor:start_link({local, ?MODULE}, ?MODULE, []).
+
+%% supervisor.
+
+init([]) ->
+ Procs = [],
+ {ok, {{one_for_one, 10, 10}, Procs}}.
diff --git a/examples/cookie/src/toppage_handler.erl b/examples/cookie/src/toppage_handler.erl
new file mode 100644
index 0000000..783cda6
--- /dev/null
+++ b/examples/cookie/src/toppage_handler.erl
@@ -0,0 +1,29 @@
+%% Feel free to use, reuse and abuse the code in this file.
+
+%% @doc Cookie handler.
+-module(toppage_handler).
+
+-export([init/3]).
+-export([handle/2]).
+-export([terminate/2]).
+
+init(_Transport, Req, []) ->
+ {ok, Req, undefined}.
+
+handle(Req, State) ->
+ NewValue = integer_to_list(random:uniform(1000000)),
+ Req2 = cowboy_req:set_resp_cookie(
+ <<"server">>, NewValue, [{path, <<"/">>}], Req),
+ {ClientCookie, Req3} = cowboy_req:cookie(<<"client">>, Req2),
+ {ServerCookie, Req4} = cowboy_req:cookie(<<"server">>, Req3),
+ {ok, Body} = toppage_dtl:render([
+ {client, ClientCookie},
+ {server, ServerCookie}
+ ]),
+ {ok, Req5} = cowboy_req:reply(200,
+ [{<<"content-type">>, <<"text/html">>}],
+ Body, Req4),
+ {ok, Req5, State}.
+
+terminate(_Req, _State) ->
+ ok.
diff --git a/examples/cookie/start.sh b/examples/cookie/start.sh
new file mode 100755
index 0000000..7b1d728
--- /dev/null
+++ b/examples/cookie/start.sh
@@ -0,0 +1,3 @@
+#!/bin/sh
+erl -pa ebin deps/*/ebin -s cookie \
+ -eval "io:format(\"Point your browser at http://localhost:8080~n\")."
diff --git a/examples/cookie/templates/toppage.dtl b/examples/cookie/templates/toppage.dtl
new file mode 100644
index 0000000..e6faa2b
--- /dev/null
+++ b/examples/cookie/templates/toppage.dtl
@@ -0,0 +1,24 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+ <meta charset="utf-8">
+ <title>Cowboy Cookie Example</title>
+</head>
+
+<body>
+ <h1>Cowboy Cookie Example</h1>
+ <p>Refresh the page to see the next cookie.</p>
+
+ <h2>Cookie Set Server-Side</h2>
+ <p>{{ server }}</p>
+
+ <h2>Cookie Set Client-Side</h2>
+ <p>{{ client }}</p>
+
+ <script type="text/javascript">
+// <![CDATA[
+document.cookie="client=test=";
+// ]]>
+ </script>
+</body>
+</html>