mirror of
https://github.com/ninenines/cowboy.git
synced 2025-07-15 04:30:25 +00:00
Add cookie example
This commit is contained in:
parent
659ca05c54
commit
a07d063fd8
10 changed files with 159 additions and 0 deletions
15
examples/cookie/src/cookie.app.src
Normal file
15
examples/cookie/src/cookie.app.src
Normal file
|
@ -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, []}
|
||||
]}.
|
14
examples/cookie/src/cookie.erl
Normal file
14
examples/cookie/src/cookie.erl
Normal file
|
@ -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).
|
25
examples/cookie/src/cookie_app.erl
Normal file
25
examples/cookie/src/cookie_app.erl
Normal file
|
@ -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.
|
23
examples/cookie/src/cookie_sup.erl
Normal file
23
examples/cookie/src/cookie_sup.erl
Normal file
|
@ -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}}.
|
29
examples/cookie/src/toppage_handler.erl
Normal file
29
examples/cookie/src/toppage_handler.erl
Normal file
|
@ -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.
|
Loading…
Add table
Add a link
Reference in a new issue