Created
February 7, 2021 05:42
-
-
Save rkulkarni/948696a0b5c0920170e073c12919cfd9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%------------------------------------------------------------------------------ | |
% | |
% itx session handler | |
% | |
% to make application stateless, this session handler stores | |
% session data in a cookie. best to keep session state as small | |
% as possible | |
%------------------------------------------------------------------------------ | |
%------------------------------------------------------------------------------ | |
% main | |
%------------------------------------------------------------------------------ | |
-module (itx_session_handler). | |
-include_lib("nitrogen_core/include/wf.hrl"). | |
-behaviour (session_handler). | |
-export ([ | |
init/2, | |
finish/2, | |
get_value/4, | |
set_value/4, | |
clear_all/2, | |
session_id/2 | |
]). | |
-record (state, { | |
unique, | |
kvstore=[] | |
}). | |
%------------------------------------------------------------------------------ | |
% init, finish | |
%------------------------------------------------------------------------------ | |
% | |
% init | |
% | |
init(_Config, _State) -> | |
Cookie = wf:cookie(get_cookie_name()), | |
State = case wf:depickle(Cookie) of | |
undefined -> | |
new_state(); | |
Other=#state{} -> | |
Other; | |
_ -> | |
new_state() | |
end, | |
{ok, State}. | |
% | |
% finish | |
% | |
finish(_Config, State) -> | |
Timeout = wf:config_default(session_timeout, 20), | |
Opts = [ | |
{path, "/"}, | |
{minutes_to_live, Timeout}, | |
{http_only, true} | |
], | |
ok = wf:cookie(get_cookie_name(), wf:pickle(State), Opts), | |
{ok, []}. | |
%------------------------------------------------------------------------------ | |
% get, set, clear ... | |
%------------------------------------------------------------------------------ | |
% | |
% get value | |
% | |
get_value(Key, DefaultValue, _Config, State) -> | |
Value = case lists:keyfind(Key, 1, State#state.kvstore) of | |
false -> | |
DefaultValue; | |
{Key, ValueFromStore} -> | |
ValueFromStore | |
end, | |
{ok, Value, State}. | |
% | |
% set value | |
% | |
set_value(Key, Value, _Config, State) -> | |
% | |
% old value | |
% | |
KVStore = case State#state.kvstore of | |
undefined -> | |
[]; | |
KVStore0 -> | |
KVStore0 | |
end, | |
OldValue = case lists:keyfind(Key, 1, KVStore) of | |
false -> | |
undefined; | |
{Key, ValueFromStore} -> | |
ValueFromStore | |
end, | |
% | |
% new kv store | |
% | |
KvStoreNew = lists:keystore(Key, 1, KVStore, {Key, Value}), | |
% | |
% return | |
% | |
{ok, OldValue, State#state {kvstore=KvStoreNew}}. | |
% | |
% clear all | |
% | |
clear_all(_Config, State) -> | |
{ok, State#state {kvstore=[]}}. | |
% | |
% session id | |
% | |
session_id(_Config, State) -> | |
{ok, SessionId} = wf:hex_encode (State#state.unique), | |
{ok, SessionId, State}. | |
%------------------------------------------------------------------------------ | |
% misc | |
%------------------------------------------------------------------------------ | |
% | |
% get cookie name | |
% | |
get_cookie_name() -> | |
wf:config_default(cookie_name, "newcookie"). | |
% | |
% new state | |
% | |
new_state() -> | |
Unique = erlang:md5(term_to_binary({os:timestamp(), erlang:make_ref()})), | |
#state {unique=Unique}. | |
%------------------------------------------------------------------------------ | |
% end | |
%------------------------------------------------------------------------------ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment