Browse Source

deps -- erl24, cowboy2

221V 1 year ago
parent
commit
9252cb2421

+ 22 - 7
apps/n2o_sample/src/hm.erl

@@ -22,14 +22,29 @@
 
 %% get params
 
-
 get_language_from_header(Req) ->
-  {Headers,_} = wf:headers(Req),
-  Accept_language = [ V || {K,V} <- Headers, K =:= <<"accept-language">> ], %% todo header to lowercase
+  #{headers := Headers} = Req,
+  F1 = fun(K, V, Acc) ->
+    Is_Same = <<"accept-language">> =:= string:lowercase(K),
+    case Is_Same of
+      true ->
+        Is_A = <<"*">> =:= binary:part(V, {0, 1}),
+        Is_E = <<>> =:= V,
+        case {Is_A, Is_E} of
+          {false, false} -> V;
+          _ -> []
+        end;
+      _ -> Acc
+    end
+  end,
+  Accept_language = maps:fold(F1, [], Headers),
   case Accept_language of
     [] -> en;
-    [H|_] ->
-      <<Lang:2/binary,_/binary>> = H,
+    Val ->
+      <<Lang0:2/binary,_/binary>> = Val, %% Accept-Language: <<"uk,en;q=0.5">> %% <<"fr-CH, fr;q=0.9, en;q=0.8, de;q=0.7, *;q=0.5">>
+      Lang = try erlang:binary_to_existing_atom(Lang0, latin1)
+      catch _:_ -> en
+      end,
       case lists:member(Lang, [en, uk]) of %% todo mv to config langs list and default lang
         false -> en;
         true -> Lang
@@ -38,7 +53,7 @@ get_language_from_header(Req) ->
 
 
 get_language_from_cookies(Name, Req) ->
-  case wf:cookie_req(Name,Req) of
+  case wf:cookie_req(Name, Req) of
     undefined -> false;
     Lang ->
       case lists:member(Lang, [en, uk]) of
@@ -58,7 +73,7 @@ get_language(Name, Req) ->
 
 
 get_tr_mod() -> %% modules with i18n for config langs list -- en -> tr_en, uk -> tr_uk etc
-  Lang = hm:get_language(<<"lang">>, ?REQ),
+  Lang = get_language(<<"lang">>, ?REQ),
   case Lang of
     uk -> tr_uk;
     %en -> tr_en

+ 2 - 2
apps/n2o_sample/src/index.erl

@@ -15,7 +15,7 @@
 main() ->
   case wf:user() of
     undefined ->
-      wf:redirect("login.html"),
+      wf:redirect({http, "login.html"}),
       [];
     _ ->
       #dtl{file = "index", app = n2o_sample, bindings = [
@@ -38,7 +38,7 @@ body() ->
   [ #span{id=upload}, #button{id=send, body= <<"Chat">>, postback=chat, source=[message] } ].
 
 event(init) ->
-  io:format("~p~n", [wf:config(n4u, appurl, undefined)]),
+  %%io:format("~p~n", [wf:config(n4u, appurl, undefined)]),
   
   Room = code(),
   wf:update(upload, #upload{id=upload}),

+ 40 - 4
apps/n2o_sample/src/n2o_sample_app.erl

@@ -5,15 +5,51 @@
 -export([
   start/2,
   stop/1
-%  main/1
 ]).
 
+-export([
+  routes/0,
+  update_routes/0
+]).
+
+
+-define(MIME, [{mimetypes, cow_mimetypes, all}]).
+
+update_routes() ->
+  Dispatch = routes(),
+  cowboy:set_env(http, dispatch, Dispatch).
+
+
+%-define(STATIC,     { dir, "apps/n2o_sample/priv", ?MIME }).
+-define(STATIC1,    { dir, "apps/n2o_sample/priv/static", ?MIME }).
+-define(STATIC2,    { dir, "apps/n2o_sample/priv/js", ?MIME }).
+-define(STATIC3,    { dir, "apps/n2o_sample/priv/css", ?MIME }).
+%-define(STATIC_N4U, { dir, "deps/n4u/priv",           ?MIME }).
+
+routes() ->
+  cowboy_router:compile([{'_', [
+    {"/static/[...]",          n2o_static,   ?STATIC1}, %% static without nginx by cowboy  % todo : clean - mv to stc folder
+    {"/js/[...]",              n2o_static,   ?STATIC2}, %% static without nginx by cowboy
+    {"/css/[...]",             n2o_static,   ?STATIC3}, %% static without nginx by cowboy
+    {"/img/[...]",             n2o_static,   ?STATIC1}, %% static without nginx by cowboy
+    %{"/static/[...]",          n2o_static,   ?STATIC}, %% static without nginx by cowboy  % todo : clean - mv to stc folder
+    %{"/js/[...]",              n2o_static,   ?STATIC}, %% static without nginx by cowboy
+    %{"/css/[...]",             n2o_static,   ?STATIC}, %% static without nginx by cowboy
+    %{"/img/[...]",             n2o_static,   ?STATIC}, %% static without nginx by cowboy
+%    {"/n4u/[...]",             n2o_static, ?STATIC_N4U},%% static without nginx by cowboy
+    {"/multipart/[...]",       n2o_multipart, []},
+    {"/rest/:resource",        rest_cowboy,   []},
+    {"/rest/:resource/:id",    rest_cowboy,   []},
+    {"/ws/[...]",              n2o_stream,    []},
+    {'_',                      n2o_cowboy,    []}
+  ]}]).
+
 
-%main(A)    -> mad:main(A).
 %start()    -> start(normal,[]).
 start(_,_) ->
-  %%[application:start(A) || A <- [asn1,kernel,stdlib,fs,ranch,compiler,syntax_tools,mnesia,crypto,inets,kvs,cowlib,public_key,ssl,cowboy,n4u,n4u_sample,active,avz,epgsql,pooler,epgsql_pool,erlydtl,gproc,jsone,mad,nitro,oauth,rest,sh] ],
-  [application:start(A) || A <- [asn1,kernel,stdlib,fs,ranch,compiler,syntax_tools,mnesia,crypto,inets,kvs,cowlib,public_key,ssl,cowboy,n4u,active,avz,epgsql,pooler,epgsql_pool,erlydtl,gproc,jsone,mad,nitro,oauth,rest,sh] ],
+  %%[application:start(A) || A <- [asn1,kernel,stdlib,fs,ranch,compiler,syntax_tools,mnesia,crypto,inets,kvs,public_key,ssl,cowboy,n4u,n4u_sample,active,avz,epgsql,pooler,epgsql_pool,erlydtl,gproc,jsone,mad,nitro,oauth,rest,sh] ],
+  %[application:start(A) || A <- [asn1,kernel,stdlib,fs,compiler,syntax_tools,mnesia,crypto,inets,kvs,public_key,ssl,ranch,cowboy,n4u,active,avz,epgsql,pooler,epgsql_pool,erlydtl,gproc,jsone,mad,nitro,oauth,rest,sh] ],% todo fix rest on erl24
+  [application:start(A) || A <- [asn1,kernel,stdlib,fs,compiler,syntax_tools,mnesia,crypto,inets,kvs,public_key,ssl,ranch,cowboy,n4u,active,avz,epgsql,pooler,epgsql_pool,erlydtl,gproc,jsone,mad,nitro,oauth,sh] ],
   
   {ok, _} = application:ensure_all_started(ranch),
   {ok, _} = application:ensure_all_started(cowboy),

+ 10 - 28
apps/n2o_sample/src/n2o_sample_sup.erl

@@ -2,7 +2,7 @@
 
 -behaviour(supervisor).
 
--include_lib("kvs/include/user.hrl").
+%%-include_lib("kvs/include/user.hrl").
 
 
 -export([
@@ -15,14 +15,14 @@ start_link() ->
   supervisor:start_link({local, ?MODULE}, ?MODULE,[]).
 
 
--define(USERS, [ #user{id = "maxim",  email = "maxim@synrc.com"},
-                 #user{id = "doxtop", email = "doxtop@synrc.com"},
-                 #user{id = "roman",  email = "roman@github.com"} ]).
+%-define(USERS, [ #user{id = "maxim",  email = "maxim@synrc.com"},
+%                 #user{id = "doxtop", email = "doxtop@synrc.com"},
+%                 #user{id = "roman",  email = "roman@github.com"} ]).
 
 
 init([]) ->
-  users:init(),
-  users:populate(?USERS),
+%  users:init(),            %% todo check debug rest on erl24
+%  users:populate(?USERS),
   kvs:join(),
   
   Params = #{host => application:get_env(n4u, pgs_host, "localhost"), %% gets settings from sys.config
@@ -40,26 +40,8 @@ init([]) ->
       err
   end,
   
-  {ok, {{one_for_one, 10, 10}, [spec()]}}.
-
-
-spec()   -> ranch:child_spec(http, 100, ranch_tcp, port(), cowboy_protocol, env()).
-env()    -> [ { env, [ { dispatch, points() } ] } ].
-static() ->   { dir, "apps/n2o_sample/priv/static", mime() }.
-static2() ->  { dir, "apps/n2o_sample/priv/js", mime() }.
-%n4u()    ->   { dir, "deps/n4u/priv",           mime() }.
-mime()   -> [ { mimetypes, cow_mimetypes, all   } ].
-port()   -> [ { port, wf:config(n4u, port, 8000)  } ].
-points() -> cowboy_router:compile([{'_', [
-    {"/static/[...]",       n2o_static,    static()}, %% static without nginx by cowboy  % todo : clean - mv to stc folder
-    {"/js/[...]",           n2o_static,   static2()}, %% static without nginx by cowboy
-    {"/css/[...]",          n2o_static,    static()}, %% static without nginx by cowboy
-    {"/img/[...]",          n2o_static,    static()}, %% static without nginx by cowboy
-%    {"/n4u/[...]",          n2o_static,    n4u()},    %% static without nginx by cowboy
-    {"/multipart/[...]",    n2o_multipart, []},
-    {"/rest/:resource",     rest_cowboy,   []},
-    {"/rest/:resource/:id", rest_cowboy,   []},
-    {"/ws/[...]",           n2o_stream,    []},
-    {'_',                   n2o_cowboy,    []}
-  ]}]).
+  Dispatch = n2o_sample_app:routes(),
+  cowboy:start_clear(http, [{port, application:get_env(n4u, port, 8000)}], #{env => #{dispatch => Dispatch}}),
+  
+  {ok, {{one_for_one, 10, 10}, []}}.
 

+ 13 - 13
apps/n2o_sample/src/users.erl

@@ -1,20 +1,20 @@
 -module(users).
 
--behaviour(rest).
--compile({parse_transform, rest}).
+%%-behaviour(rest).
+%%-compile({parse_transform, rest}). %% todo fix rest on erl24
 
--include_lib("kvs/include/user.hrl").
+%-include_lib("kvs/include/user.hrl").
 
--export([init/0, populate/1, exists/1, get/0, get/1, post/1, delete/1]).
+%-export([init/0, populate/1, exists/1, get/0, get/1, post/1, delete/1]).
 
--rest_record(user).
+%-rest_record(user).
 
-init()               -> ets:new(users, [public, named_table, {keypos, #user.id}]).
-populate(Users)      -> ets:insert(users, Users).
-exists(Id)           -> ets:member(users, wf:to_list(Id)).
-get()                -> ets:tab2list(users).
-get(Id)              -> #user{id=Id}.
-delete(Id)           -> ets:delete(users, wf:to_list(Id)).
-post(#user{} = User) -> ets:insert(users, User), true;
-post(Data)           -> post(from_json(Data, #user{})), true.
+%init()               -> ets:new(users, [public, named_table, {keypos, #user.id}]).
+%populate(Users)      -> ets:insert(users, Users).
+%exists(Id)           -> ets:member(users, wf:to_list(Id)).
+%get()                -> ets:tab2list(users).
+%get(Id)              -> #user{id=Id}.
+%delete(Id)           -> ets:delete(users, wf:to_list(Id)).
+%post(#user{} = User) -> ets:insert(users, User), true;
+%post(Data)           -> post(from_json(Data, #user{})), true.
 

+ 1 - 0
otp.mk

@@ -27,6 +27,7 @@ $(RUN_DIR) $(LOG_DIR):
 #console: applist
 console:
 	ERL_LIBS="$(ERL_LIBS)" erl +pc unicode $(ERL_ARGS) -eval 'application:ensure_all_started(n4u_sample)'
+#	ERL_LIBS="$(ERL_LIBS)" erl +pc unicode $(ERL_ARGS) -eval 'ok'
 #	ERL_LIBS="$(ERL_LIBS)" erl +pc unicode $(ERL_ARGS) -eval 'application:ensure_all_started($(PROJECT)),n4u_sample:start([],[]),application:start($(PROJECT))'
 #	ERL_LIBS="$(ERL_LIBS)" erl +pc unicode $(ERL_ARGS) -eval 'n4u_sample:start([],[])'
 #	ERL_LIBS="$(ERL_LIBS)" erl +pc unicode $(ERL_ARGS) -eval 'application:start($(PROJECT))'

+ 20 - 16
rebar.config

@@ -6,37 +6,41 @@
     %{syn,     ".*", {git, "https://git.4dev.win/n4u/syn.git",          {tag, "1.1.0"}}},
     
     {jsone,   ".*", {git, "https://git.4dev.win/n4u/jsone.git",        {tag, "1.4.7"} }},
-    {erlydtl, ".*", {git, "https://git.4dev.win/n4u/erlydtl.git",      {tag, "0.13.0"}}},
+    {erlydtl, ".*", {git, "https://git.4dev.win/n4u/erlydtl.git",      {tag, "erl24"}}},
     
-    {nitro,   ".*", {git, "https://git.4dev.win/n4u/nitro.git",        {tag, "6.6.4-erl23"}  }},
-    %{nitro,   ".*", {git, "https://git.4dev.win/n4u/nitro.git",        {branch, "n4u"}  }},
+    {nitro,   ".*", {git, "https://git.4dev.win/n4u/nitro.git",        {tag, "6.6.4-erl24"}  }},
     
     {fs,      ".*", {git, "https://git.4dev.win/n4u/fs.git",           {tag, "6.12"}  }},
-    {active,  ".*", {git, "https://git.4dev.win/n4u/active.git",       {tag, "6.2-erl23"}  }},  %% todo fix hot reload with active and mad
+    {active,  ".*", {git, "https://git.4dev.win/n4u/active.git",       {tag, "6.2-erl24"}  }},  %% todo fix hot reload with active and mad
     
-    {sh,      ".*", {git, "https://git.4dev.win/n4u/sh.git",           {tag, "4.1.1-erl23"}  }},
+    {sh,      ".*", {git, "https://git.4dev.win/n4u/sh.git",           {tag, "4.1.1-erl24"}  }},
     
     {epgsql,  ".*", {git, "https://git.4dev.win/n4u/epgsql.git",       {tag, "4.6.0"} }},
-    {pooler,  ".*", {git, "https://git.4dev.win/n4u/pooler.git",       {tag, "erl23"} }},
-    {herd,    ".*", {git, "https://git.4dev.win/n4u/herd.git",         {tag, "erl23"} }},
-    {epgsql_pool, ".*", {git, "https://git.4dev.win/n4u/epgsql_pool.git", {tag, "erl23"} }},
-    
-    {cowlib,  ".*", {git, "https://git.4dev.win/n4u/cowlib.git",       {branch, "1.0.2-erl23"} }},
+    {pooler,  ".*", {git, "https://git.4dev.win/n4u/pooler.git",       {tag, "erl24"} }},
+    {herd,    ".*", {git, "https://git.4dev.win/n4u/herd.git",         {tag, "erl24"} }},
+    {epgsql_pool, ".*", {git, "https://git.4dev.win/n4u/epgsql_pool.git", {tag, "erl24"} }},
+    
+    %%{cowlib,  ".*", {git, "https://git.4dev.win/n4u/cowlib.git",       {branch, "1.0.2-erl23"} }},
+    %%{ranch,   ".*", {git, "https://git.4dev.win/n4u/ranch.git",        {tag, "1.8.0"} }},
+    %%{cowboy,  ".*", {git, "https://git.4dev.win/n4u/cowboy.git",       {tag, "1.1.2-erl23"} }},
+    {cowlib,  ".*", {git, "https://git.4dev.win/n4u/cowlib.git",       {branch, "2.11.0"} }},
     {ranch,   ".*", {git, "https://git.4dev.win/n4u/ranch.git",        {tag, "1.8.0"} }},
-    {cowboy,  ".*", {git, "https://git.4dev.win/n4u/cowboy.git",       {tag, "1.1.2-erl23"} }},
+    %{cowboy,  ".*", {git, "https://git.4dev.win/n4u/cowboy.git",       {tag, "2.9.0"} }},
+    {cowboy,  ".*", {git, "https://git.4dev.win/n4u/cowboy.git",       {branch, "2.9.0-b"} }},
     
     %{n4u,     ".*", {git, "https://git.4dev.win/n4u/n4u.git",        {branch, "master"}  }},
-    {n4u,     ".*", {git, "https://git.4dev.win/n4u/n4u.git",          {tag, "4.4.2-erl23"}  }},
+    {n4u,     ".*", {git, "https://git.4dev.win/n4u/n4u.git",          {tag, "4.5-erl24"}  }},
     
-    {rest,    ".*", {git, "https://git.4dev.win/n4u/rest.git",         {tag, "2.9-erl23"}  }},
+    %{rest,    ".*", {git, "https://git.4dev.win/n4u/rest.git",         {tag, "2.9-erl23"}  }}, %% cowboy1 %% todo check debug
+    %{rest,    ".*", {git, "https://git.4dev.win/n4u/rest.git",         {tag, "5.10-erl24"}  }}, %% cowboy2 %% todo check debug
     
     %{mad,     ".*", {git, "https://git.4dev.win/n4u/mad.git",          {branch, "n4u"}  }},
-    {mad,     ".*", {git, "https://git.4dev.win/n4u/mad.git",          {tag, "2.10.2-erl23"}  }},
+    {mad,     ".*", {git, "https://git.4dev.win/n4u/mad.git",          {tag, "2.10.2-erl24"}  }},
     
-    {kvs,     ".*", {git, "https://git.4dev.win/n4u/kvs.git",          {tag, "3.9.2-erl23"}}},
+    {kvs,     ".*", {git, "https://git.4dev.win/n4u/kvs.git",          {tag, "3.9.2-erl24"}}},
     
     {oauth,   ".*", {git, "https://git.4dev.win/n4u/erlang-oauth.git", {tag, "v2.0.0"}  }},
-    {avz,     ".*", {git, "https://git.4dev.win/n4u/avz.git",          {tag, "4.4.1-erl23"}  }}
+    {avz,     ".*", {git, "https://git.4dev.win/n4u/avz.git",          {tag, "4.4.1-erl24"}  }}
 ]}.
 {fetch_speed,fast_master}.
 {verbose,1}.