aboutsummaryrefslogtreecommitdiffstats
path: root/examples/cookie/src/toppage_handler.erl
diff options
context:
space:
mode:
Diffstat (limited to 'examples/cookie/src/toppage_handler.erl')
-rw-r--r--examples/cookie/src/toppage_handler.erl29
1 files changed, 29 insertions, 0 deletions
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.