cowboyのcookieとsessionの例
10165 ワード
セッションプラグインはhttps://github.com/chvanikoff/cowboy_sessionをダウンロードする必要があります
セッションが分散ストレージを必要とする場合は、https://github.com/spiegela/cowboy_session_storage_redisを参照してください.彼はredisを使用しています.上のcowboy_に基づいています.sessionが行った拡張は,三者が考慮したストレージがあれば,分散session処理を完全に自分で実現できる.
プロジェクトの作成
testCowboy_app.erl
route_helper.erl
cookie_read_handler.erl
cookie_write_handler.erl
session_read_handler.erl
session_write_handler.erl
session存redisをテストしたらcowboyをダウンロードします.session_storage_redis放工事はコンパイルすればいい.
セッションが分散ストレージを必要とする場合は、https://github.com/spiegela/cowboy_session_storage_redisを参照してください.彼はredisを使用しています.上のcowboy_に基づいています.sessionが行った拡張は,三者が考慮したストレージがあれば,分散session処理を完全に自分で実現できる.
プロジェクトの作成
rebar-creator create-app testCowboy
testCowboy_app.erl
-module(testCowboy_app).
-behaviour(application).
-export([start/2, stop/1]).
-define(C_ACCEPTORS, 100).
start(_StartType, _StartArgs) ->
application:start(crypto),
application:start(cowlib),
application:start(ranch),
application:start(cowboy),
application:start(gproc),
application:start(uuid),
application:start(cowboy_session),
%% redis session,
cowboy_session_config:update_storage(cowboy_session_storage_redis),
Routes = route_helper:get_routes(),
Dispatch = cowboy_router:compile(Routes),
Port = 8080,
TransOpts = [{port, Port}],
ProtoOpts = [{env, [{dispatch, Dispatch}]}],
cowboy:start_http(http, ?C_ACCEPTORS, TransOpts, ProtoOpts).
stop(_State) ->
ok.
route_helper.erl
-module(route_helper).
-export([get_routes/0]).
get_routes() ->
[
{'_', [
{"/cookie_read", cookie_read_handler, []},
{"/cookie_write", cookie_write_handler, []},
{"/session_read", session_read_handler, []},
{"/session_write", session_write_handler, []},
]}
].
cookie_read_handler.erl
-module(cookie_read_handler).
-export([init/3]).
-export([handle/2]).
-export([terminate/3]).
init(_Transport, Req, []) ->
{ok, Req, undefined}.
handle(Req, State) ->
{CookieVal,_} = cowboy_req:cookie(<<"test_cookie">>, Req,<<"no cookie found">>),
{ok, Req2} = cowboy_req:reply(200, [{<<"content-type">>, <<"text/html">>}],CookieVal, Req),
{ok, Req2, State}.
terminate(_Reason, _Req, _State) ->
ok.
cookie_write_handler.erl
-module(cookie_write_handler).
-export([init/3]).
-export([handle/2]).
-export([terminate/3]).
init(_Transport, Req, []) ->
{ok, Req, undefined}.
handle(Req, State) ->
TestCookieVal = integer_to_list(random:uniform(1000000)),
Req2 = cowboy_req:set_resp_cookie(<<"test_cookie">>, TestCookieVal, [{path, <<"/">>}], Req),
{ok, Req3} = cowboy_req:reply(200, [{<<"content-type">>, <<"text/html">>}],TestCookieVal, Req2),
{ok, Req3, State}.
terminate(_Reason, _Req, _State) ->
ok.
session_read_handler.erl
-module(session_read_handler).
-export([init/3]).
-export([handle/2]).
-export([terminate/3]).
init(_Transport, Req, []) ->
{ok, Req, undefined}.
handle(Req, State) ->
{SessionVal, Req2} = cowboy_session:get(<<"test_session">>, <<"no session found">>,Req),
{ok, Req3} = cowboy_req:reply(200, [{<<"content-type">>, <<"text/html">>}],SessionVal, Req2),
{ok, Req3, State}.
terminate(_Reason, _Req, _State) ->
ok.
session_write_handler.erl
-module(session_write_handler).
-export([init/3]).
-export([handle/2]).
-export([terminate/3]).
init(_Transport, Req, []) ->
{ok, Req, undefined}.
handle(Req, State) ->
TestCookieVal = integer_to_list(random:uniform(1000000)),
{ok, Req2} = cowboy_session:set(<<"test_session">>, TestCookieVal, Req),
{ok, Req3} = cowboy_req:reply(200, [{<<"content-type">>, <<"text/html">>}],TestCookieVal, Req2),
{ok, Req3, State}.
terminate(_Reason, _Req, _State) ->
ok.
session存redisをテストしたらcowboyをダウンロードします.session_storage_redis放工事はコンパイルすればいい.