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




                                                           
                  
 
                  


                                                                
                                                         
                                                                   



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

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

-export([init/2]).

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