aboutsummaryrefslogtreecommitdiffstats
path: root/examples/cookie/src/toppage_handler.erl
blob: 837f83bfeae3884afa86ad8515b0fe655f2178b5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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/3]).

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),
	#{client := ClientCookie, server := ServerCookie}
		= cowboy_req:match_cookies(Req2, [client, server]),
	{ok, Body} = toppage_dtl:render([
		{client, ClientCookie},
		{server, ServerCookie}
	]),
	Req3 = cowboy_req:reply(200,
		[{<<"content-type">>, <<"text/html">>}],
		Body, Req2),
	{ok, Req3, State}.

terminate(_Reason, _Req, _State) ->
	ok.