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

  • 相关阅读:
    设计模式详解(图码)
    设计模式详解(图)
    Zookeeper学习
    取消单元格的点击事件
    ios 中生成随机数
    IOS 时间和时间戳之间转化
    偏好存空判断
    限制textfield的文字长度
    tabBar的图标不被系统渲染
    (转)IOS http请求的get 和 post的请求的区别
  • 原文地址:https://www.cnblogs.com/ziyouchutuwenwu/p/4277992.html
Copyright © 2020-2023  润新知