session插件需要下载https://github.com/chvanikoff/cowboy_session
如果session需要分布式存储,可以参考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放工程编译即可。