-module(sample).
-behaviour(supervisor).
-behaviour(application).
-export([init/1, start/2, stop/1, main/1]).
start(_,_) -> supervisor:start_link({local,review},review,[]).
stop(_) -> ok.
init([]) -> case cowboy:start_http(http,3,port(),env()) of
{ok, _} -> ok;
{error,_} -> halt(abort,[]) end, sup().
sup() -> { ok, { { one_for_one, 5, 100 }, [] } }.
port() -> [ { port, application:get_env(n2o,port,8000) } ].
env() -> [ { env, [ { dispatch, points() } ] } ].
static() -> { dir, "apps/sample/priv/static", mime() }.
n2o() -> { dir, "deps/n2o/priv", mime() }.
mime() -> [ { mimetypes, cow_mimetypes, all } ].
points() -> cowboy_router:compile([{'_', [
{ "/static/[...]", n2o_static, static()},
{ "/n2o/[...]", n2o_static, n2o()},
{ "/ws/[...]", n2o_stream, []},
{ '_', n2o_cowboy, []} ]}]).
pickle(Data) ->
Message = term_to_binary({Data,now()}),
Padding = size(Message) rem 16,
Bits = (16-Padding)*8, Key = secret(), IV = crypto:rand_bytes(16),
Cipher = crypto:block_encrypt(aes_cbc128,Key,IV,<<Message/binary,0:Bits>>),
Signature = crypto:hmac(sha256,Key,<<Cipher/binary,IV/binary>>),
base64:encode(<<IV/binary,Signature/binary,Cipher/binary>>).
secret() -> wf:config(n2o,secret,<<"ThisIsClassified">>).
depickle(PickledData) ->
try Key = secret(),
Decoded = base64:decode(wf:to_binary(PickledData)),
<<IV:16/binary,Signature:32/binary,Cipher/binary>> = Decoded,
Signature = crypto:hmac(sha256,Key,<<Cipher/binary,IV/binary>>),
{Data,_Time} = binary_to_term(crypto:block_decrypt(aes_cbc128,Key,IV,Cipher),[safe]),
Data
catch E:R -> wf:info(?MODULE,"Depicke Error: ~p",[{E,R}]), undefined end.
$ wrk -c 50 -r 50 -t 4 -k http://localhost:8001/
Making 50 requests to http://localhost:8001/
4 threads and 50 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 162.69ms 483.80ms 1.69s 89.58%
Req/Sec 0.00 0.00 0.00 100.00%
48 requests, 48 sock connections in 1.75s, 47.53KB read
Requests/sec: 27.42
Transfer/sec: 27.15KB
-rest_record(user).
hunmap([],O,_,_) -> O;
hunmap([{BK,V}|T],O,Keys,0) -> O;
hunmap([{BK,V}|T],O,Keys,L) ->
K = wf:to_atom(BK),
hunmap(T, setelement(
wf_utils:indexof(K,Keys),O,wf:to_list(V)), Keys--[K],L-1).
-define(unmap(Record), unmap(P,R) ->
hunmap(P,R,record_info(fields, Record),size(R)-1)).
-define(map(Record), map(O) ->
Y = [ try N=lists:nth(1,B), if is_number(N) ->
wf:to_binary(B); true -> B end catch _:_ -> B end
|| B <- tl(tuple_to_list(O)) ],
lists:zip(record_info(fields, Record), Y)).
event(chat) -> wf:send(chat, wf:q(message)),
body() ->
wf:async(fun() -> loop() end,chat),
[ #panel{id=history}, #textbox{id=message}, #button{postback=chat}].
loop() ->
receive
Message ->
wf:insert_bottom(history, #span{text=Message}),
wf:flush()
end,
loop().