Browse Source

n2o_cx to n4u_cx

221V 3 years ago
parent
commit
35d6a69d62
5 changed files with 82 additions and 55 deletions
  1. 1 1
      ebin/n4u.app
  2. 2 2
      src/handlers/n4u_async.erl
  3. 0 39
      src/n2o_cx.erl
  4. 66 0
      src/n4u_cx.erl
  5. 13 13
      src/wf.erl

+ 1 - 1
ebin/n4u.app

@@ -2,7 +2,7 @@
   {description, "N4U WebSocket Application Server"},
   {vsn, "4.4.20"},
   {applications, [kernel, stdlib, asn1, public_key, ssl, crypto, ranch, cowboy, fs, active, sh, gproc, nitro]},
-  {modules, [n2o, n4u_app, n4u_sup, n4u_async, n2o_xhr, n2o_cx, n2o_cowboy, n2o_multipart, n2o_static, n2o_stream, n2o_document, n2o_proto, n2o_relay, n2o_error, n2o_io, n2o_log, n2o_mq, n2o_pickle, n2o_query, n2o_secret, n2o_session, n2o_syn, n4u_client, n2o_file, n2o_heart, n2o_http, n2o_nitrogen, n2o_text, wf, wf_convert, wf_utils]},
+  {modules, [n2o, n4u_app, n4u_sup, n4u_async, n2o_xhr, n4u_cx, n2o_cowboy, n2o_multipart, n2o_static, n2o_stream, n2o_document, n2o_proto, n2o_relay, n2o_error, n2o_io, n2o_log, n2o_mq, n2o_pickle, n2o_query, n2o_secret, n2o_session, n2o_syn, n4u_client, n2o_file, n2o_heart, n2o_http, n2o_nitrogen, n2o_text, wf, wf_convert, wf_utils]},
   {registered, [n4u_sup]},
   {mod, {n4u_app, []}},
   {env, []}

+ 2 - 2
src/handlers/n4u_async.erl

@@ -90,8 +90,8 @@ flush(Pool) ->
 
 init_context(undefined) -> [];
 init_context(Req) ->
-  Ctx = n2o_cx:init_context(Req),
-  NewCtx = n2o_cx:fold(init, Ctx#cx.handlers, Ctx),
+  Ctx = n4u_cx:init_context(Req),
+  NewCtx = n4u_cx:fold(init, Ctx#cx.handlers, Ctx),
   erlang:put(actions, NewCtx#cx.actions),
   erlang:put(context, NewCtx),
   NewCtx.

+ 0 - 39
src/n2o_cx.erl

@@ -1,39 +0,0 @@
--module(n2o_cx).
--description('N4U Process Context').
--include_lib("n4u/include/n4u.hrl").
--compile([export_all, nowarn_export_all]).
-
-actions() -> get(actions).
-actions(Ac) -> put(actions,Ac).
-context() -> get(context).
-context(Cx) -> put(context,Cx).
-context(Cx,Proto) -> lists:keyfind(Proto,1,Cx#cx.state).
-context(Cx,Proto,UserCx) -> 
-   NewCx = Cx#cx{state=wf:setkey(Proto,1,Cx#cx.state,{Proto,UserCx})},
-   wf:context(NewCx),
-   NewCx.
-clear_actions() -> put(actions,[]).
-add_action(Action) ->
-    Actions = case get(actions) of undefined -> []; E -> E end,
-    put(actions,Actions++[Action]).
-script() -> get(script).
-script(Script) -> put(script,Script).
-cookies() -> C = get(cookies), case is_list(C) of true -> C; _ -> [] end.
-add_cookie(Name,Value,Path,TTL) ->
-    C = cookies(),
-    Cookies = case lists:keyfind(Name,1,C) of
-        {Name,_,_,_} -> lists:keyreplace(Name,1,C,{Name,Value,Path,TTL});
-        false -> [{Name,Value,Path,TTL}|C] end,
-    put(cookies,Cookies).
-
-fold(Fun,Handlers,Ctx) ->
-    lists:foldl(fun({_,Module},Ctx1) ->
-        {ok,_,NewCtx} = Module:Fun([],Ctx1),
-        NewCtx end,Ctx,Handlers).
-
-init_context(Req) ->
-    #cx{
-        actions=[], module=index, path=[], req=Req, params=[],
-        handlers= [ {'query', wf:config(n2o,'query', n2o_query)},
-                    {session, wf:config(n2o,session, n2o_session)},
-                    {route,   wf:config(n2o,route,   n2o_dynroute)} ]}.

+ 66 - 0
src/n4u_cx.erl

@@ -0,0 +1,66 @@
+-module(n4u_cx).
+-include_lib("n4u/include/n4u.hrl").
+
+-export([context/2, context/3, add_action/1, cookies/0, add_cookie/4,
+  fold/3, init_context/1 ]).
+
+% N4U Process Context
+
+context(Cx, Proto) -> lists:keyfind(Proto, 1, Cx#cx.state).
+
+
+context(Cx, Proto, UserCx) ->
+  NewCx = Cx#cx{state=setkey(Proto, 1, Cx#cx.state, {Proto, UserCx})},
+  erlang:put(context, NewCx),
+  NewCx.
+
+setkey(Name, Pos, List, New) ->
+  case lists:keyfind(Name, Pos, List) of
+    false -> [New|List];
+    _Element -> lists:keyreplace(Name, Pos, List, New)
+  end.
+
+
+add_action(Action) ->
+  Actions = case erlang:get(actions) of
+    undefined -> [];
+    E -> E
+  end,
+  erlang:put(actions, Actions ++ [Action]).
+
+%clear_actions() -> erlang:put(actions, []).
+
+
+cookies() ->
+  C = erlang:get(cookies),
+  case erlang:is_list(C) of
+    true -> C;
+    _ -> []
+  end.
+
+
+add_cookie(Name, Value, Path, TTL) ->
+  C = cookies(),
+  Cookies = case lists:keyfind(Name, 1, C) of
+    {Name, _, _, _} ->
+      lists:keyreplace(Name, 1, C, {Name, Value, Path, TTL});
+    false ->
+      [{Name, Value, Path, TTL}|C]
+  end,
+  erlang:put(cookies, Cookies).
+
+
+fold(Fun, Handlers, Ctx) ->
+  lists:foldl(
+    fun({_, Module}, Ctx1) ->
+      {ok, _, NewCtx} = Module:Fun([], Ctx1),
+      NewCtx
+    end, Ctx, Handlers).
+
+
+init_context(Req) ->
+  #cx{actions=[], module=index, path=[], req=Req, params=[],
+      handlers= [ {'query', application:get_env(n4u, 'query', n2o_query)},
+                  {session, application:get_env(n4u, session, n2o_session)},
+                  {route,   application:get_env(n4u, route,   n2o_dynroute)} ]}.
+

+ 13 - 13
src/wf.erl

@@ -150,10 +150,10 @@ lang() -> ?CTX#cx.lang.
 
 % Cookies
 
-cookies() -> n2o_cx:cookies().
+cookies() -> n4u_cx:cookies().
 cookie(Name) -> lists:keyfind(Name,1,cookies()).
 cookie(Name,Value) -> cookie(Name,Value,"/", 24 * 60 * 60).
-cookie(Name,Value,Path,TTL) -> n2o_cx:add_cookie(Name,Value,Path,TTL).
+cookie(Name,Value,Path,TTL) -> n4u_cx:add_cookie(Name,Value,Path,TTL).
 
 % Bridge Information
 
@@ -251,17 +251,17 @@ temp_id() -> "auto" ++ integer_to_list(unique_integer() rem 1000000).
 append(List, Key, Value) -> case Value of undefined -> List; _A -> [{Key, Value}|List] end.
 render(X) -> wf_render:render(X).
 
-init_context(R)-> n2o_cx:init_context(R).
-actions()      -> n2o_cx:actions().
-actions(Ac)    -> n2o_cx:actions(Ac).
-script()       -> n2o_cx:script().
-script(Script) -> n2o_cx:script(Script).
-context()      -> n2o_cx:context().
-context(Cx)    -> n2o_cx:context(Cx).
-context(Cx,Proto)        -> n2o_cx:context(Cx,Proto).
-context(Cx,Proto,UserCx) -> n2o_cx:context(Cx,Proto,UserCx).
-add_action(Action)       -> n2o_cx:add_action(Action).
-fold(Fun,Handlers,Ctx) -> n2o_cx:fold(Fun,Handlers,Ctx).
+init_context(R)-> n4u_cx:init_context(R).
+actions()      -> get(actions).
+actions(Ac)    -> put(actions, Ac).
+script()       -> get(script).
+script(Script) -> put(script, Script).
+context()      -> get(context).
+context(Cx)    -> put(context, Cx).
+context(Cx,Proto)        -> n4u_cx:context(Cx, Proto).
+context(Cx,Proto,UserCx) -> n4u_cx:context(Cx, Proto, UserCx).
+add_action(Action)       -> n4u_cx:add_action(Action).
+fold(Fun,Handlers,Ctx) -> n4u_cx:fold(Fun,Handlers,Ctx).
 
 config_multiple(Keys) -> [config(Key, "") || Key <- Keys].
 config(Key) -> config(n4u, Key, "").