首页 技术 正文
技术 2022年11月14日
0 收藏 870 点赞 4,232 浏览 2838 个字

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/, stop/]).-define(C_ACCEPTORS,  ).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 = ,
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/]).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/]).
-export([handle/]).
-export([terminate/]).init(_Transport, Req, []) ->
{ok, Req, undefined}.handle(Req, State) ->
{CookieVal,_} = cowboy_req:cookie(<<"test_cookie">>, Req,<<"no cookie found">>),
{ok, Req2} = cowboy_req:reply(, [{<<"content-type">>, <<"text/html">>}],CookieVal, Req),
{ok, Req2, State}.terminate(_Reason, _Req, _State) ->
ok.

cookie_write_handler.erl

-module(cookie_write_handler).-export([init/]).
-export([handle/]).
-export([terminate/]).init(_Transport, Req, []) ->
{ok, Req, undefined}.handle(Req, State) ->
TestCookieVal = integer_to_list(random:uniform()),
Req2 = cowboy_req:set_resp_cookie(<<"test_cookie">>, TestCookieVal, [{path, <<"/">>}], Req),
{ok, Req3} = cowboy_req:reply(, [{<<"content-type">>, <<"text/html">>}],TestCookieVal, Req2),
{ok, Req3, State}.terminate(_Reason, _Req, _State) ->
ok.

session_read_handler.erl

-module(session_read_handler).-export([init/]).
-export([handle/]).
-export([terminate/]).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(, [{<<"content-type">>, <<"text/html">>}],SessionVal, Req2),
{ok, Req3, State}.terminate(_Reason, _Req, _State) ->
ok.

session_write_handler.erl

-module(session_write_handler).-export([init/]).
-export([handle/]).
-export([terminate/]).init(_Transport, Req, []) ->
{ok, Req, undefined}.handle(Req, State) ->
TestCookieVal = integer_to_list(random:uniform()),
{ok, Req2} = cowboy_session:set(<<"test_session">>, TestCookieVal, Req),
{ok, Req3} = cowboy_req:reply(, [{<<"content-type">>, <<"text/html">>}],TestCookieVal, Req2),
{ok, Req3, State}.terminate(_Reason, _Req, _State) ->
ok.

如果测试session存redis,下载cowboy_session_storage_redis放工程编译即可。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,496
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,909
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,743
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,496
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,134
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,298