aboutsummaryrefslogblamecommitdiffstats
path: root/examples/cookie/src/toppage_handler.erl
blob: 38952ef5d0ab47509799661838e83d9370b020eb (plain) (tree)
1
2
3
4
5
6
7
8
9




                                                           
                  
 
                   
                                                          
                                                                 
                                          
                                                         
                                                                                           



                                         
                                      
                                                     
                       
                        
%% Feel free to use, reuse and abuse the code in this file.

%% @doc Cookie handler.
-module(toppage_handler).

-export([init/2]).

init(Req0, Opts) ->
	NewValue = integer_to_list(rand:uniform(1000000)),
	Req1 = cowboy_req:set_resp_cookie(<<"server">>, NewValue,
		Req0, #{path => <<"/">>}),
	#{client := ClientCookie, server := ServerCookie}
		= cowboy_req:match_cookies([{client, [], <<>>}, {server, [], <<>>}], Req1),
	{ok, Body} = toppage_dtl:render([
		{client, ClientCookie},
		{server, ServerCookie}
	]),
	Req = cowboy_req:reply(200, #{
		<<"content-type">> => <<"text/html">>
	}, Body, Req1),
	{ok, Req, Opts}.