Browse Source

n2o_document to n4u_document

221V 3 years ago
parent
commit
5c3ca88fb5

+ 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, 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]},
+  {modules, [n2o, n4u_app, n4u_sup, n4u_async, n2o_xhr, n4u_cx, n2o_cowboy, n2o_multipart, n2o_static, n2o_stream, n4u_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, []}

+ 1 - 1
src/endpoints/cowboy/n2o_cowboy.erl

@@ -10,7 +10,7 @@
 
 init(_Transport, Req, _Opts) -> {ok, Req, #state{}}.
 terminate(_Reason, _Req, _State) -> ok.
-handle(Req, State) ->  {ok, NewReq} = n2o_document:run(Req), {ok, NewReq, State}.
+handle(Req, State) ->  {ok, NewReq} = n4u_document:run(Req), {ok, NewReq, State}.
 
 % Cowboy Bridge Abstraction
 

+ 0 - 34
src/endpoints/n2o_document.erl

@@ -1,34 +0,0 @@
--module(n2o_document).
--description('N4U Server Pages HTTP endpoint handler').
--include_lib("n4u/include/n4u.hrl").
--compile([export_all, nowarn_export_all]).
-
-transition(Actions) ->
-    receive
-        {'INIT',A} -> transition(A);
-        {'N2O',Pid} -> Pid ! {actions,Actions}
-    after wf:config(n2o,transition_timeout,30000) -> {timeout,transition}
-    end.
-
-run(Req) ->
-    wf:state(status,200),
-    Pid = spawn(fun() -> transition([]) end),
-    wf:script(["var transition = {pid: '", wf:pickle(Pid), "', ",
-                "port:'", wf:to_list(wf:config(n2o,websocket_port,wf:config(n2o,port,8000))),"'}"]),
-    Ctx = wf:init_context(Req),
-    Ctx1 = wf:fold(init,Ctx#cx.handlers,Ctx),
-    wf:actions(Ctx1#cx.actions),
-    wf:context(Ctx1),
-    Elements = try (Ctx1#cx.module):main() catch C:E -> wf:error_page(C,E) end,
-    Html = wf_render:render(Elements),
-    Actions = wf:actions(),
-    Pid ! {'INIT',Actions},
-    Ctx2 = wf:fold(finish,Ctx#cx.handlers,?CTX),
-    Req2 = wf:response(Html,set_cookies(wf:cookies(),Ctx2#cx.req)),
-    wf:info(?MODULE,"Cookies Req: ~p",[Req2]),
-    {ok, _ReqFinal} = wf:reply(wf:state(status), Req2).
-
-set_cookies([],Req)-> Req;
-set_cookies([{Name,Value,Path,TTL}|Cookies],Req)->
-    set_cookies(Cookies,wf:cookie_req(Name,Value,Path,TTL,Req)).
-

+ 54 - 0
src/endpoints/n4u_document.erl

@@ -0,0 +1,54 @@
+-module(n4u_document).
+-include_lib("n4u/include/n4u.hrl").
+
+-export([run/1]).
+
+
+% N4U Server Pages HTTP endpoint handler
+
+transition(Actions) ->
+  receive
+    {'INIT', A} -> transition(A);
+    {'N4U', Pid} -> Pid ! {actions, Actions}
+  after application:get_env(n4u, transition_timeout, 30000) ->
+    {timeout, transition}
+  end.
+
+
+run(Req) ->
+  erlang:put(status, 200),
+  Pid = erlang:spawn(
+    fun() ->
+      transition([])
+    end),
+  
+  Script = ["var transition = {pid: '", wf:pickle(Pid), "', ",
+    "port:'", nitro:to_list(application:get_env(n4u, websocket_port, application:get_env(n4u, port, 8000))), "'}"],
+  erlang:put(script, Script),
+  
+  Ctx = n4u_cx:init_context(Req),
+  Ctx1 = n4u_cx:fold(init, Ctx#cx.handlers, Ctx),
+  
+  erlang:put(actions, Ctx1#cx.actions),
+  erlang:put(context, Ctx1),
+  
+  Elements = try (Ctx1#cx.module):main()
+    catch E:R:Stk ->
+      wf:error(?MODULE, "run main:~n ~p~n ~p~n ~p~n", [E, R, Stk]),
+      wf:error_page(E, R, Stk)
+    end,
+  
+  Html = nitro:render(Elements),
+  Actions = erlang:get(actions),
+  Pid ! {'INIT', Actions},
+  Ctx2 = n4u_cx:fold(finish, Ctx#cx.handlers, erlang:get(context)),
+  Req2 = wf:response(Html, set_cookies(wf:cookies(), Ctx2#cx.req)),
+  wf:info(?MODULE, "Cookies Req: ~p~n", [Req2]),
+  %erlang:exit(Pid, {}),
+  wf:reply(erlang:get(status), Req2). % {ok, New_Req}
+
+
+set_cookies([], Req)-> Req;
+set_cookies([{Name, Value, Path, TTL}|Cookies], Req)->
+  set_cookies(Cookies, wf:cookie_req(Name, Value, Path, TTL, Req)).
+

+ 1 - 1
src/handlers/n2o_error.erl

@@ -10,7 +10,7 @@
 %
 % STACK:  index:body/0:18
 %         index:main/0:8
-%         n2o_document:run/1:15
+%         n4u_document:run/1:15
 
 stack(Error, Reason) ->
     Stacktrace = [case A of