cowboy的cookie和session的例子

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放工程编译即可。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值