Parcourir la source

update erlang and cowboy (#3973)

18年梦醒 il y a 7 ans
Parent
commit
2aab1240e3

+ 2 - 2
frameworks/Erlang/cowboy/README.md

@@ -16,8 +16,8 @@ Uses the db abstraction class from Kohana
 ## Infrastructure Software Versions
 The tests were run with:
 
-* [Cowboy 0.8.3](https://github.com/extend/cowboy)
-* [Erlang R16B](http://www.erlang.org/)
+* [Cowboy 2.4.0](https://github.com/ninenines/cowboy)
+* [Erlang 20.3.8.4](http://www.erlang.org/)
 * [MySQL 5.5.29](https://dev.mysql.com/)
 
 ## Test URLs

+ 1 - 1
frameworks/Erlang/cowboy/cowboy.dockerfile

@@ -1,4 +1,4 @@
-FROM erlang:18.3.4.8
+FROM erlang:20.3.8.4
 
 ADD ./ /cowboy
 WORKDIR /cowboy

+ 2 - 2
frameworks/Erlang/cowboy/rebar.config

@@ -1,7 +1,7 @@
 {deps,
  [
-  {jiffy, ".*", {git, "https://github.com/davisp/jiffy.git", {tag, "0.15.0"}}},
+  {jiffy, ".*", {git, "https://github.com/davisp/jiffy.git", {tag, "0.15.2"}}},
   {mimetypes, ".*", {git, "http://github.com/spawngrid/mimetypes.git", {branch, master}}},
   {emysql, ".*", {git, "https://github.com/deadtrickster/Emysql.git", "52b802098322aad372198b9f5fa9ae9a4c758ad1"}},
-  {cowboy, ".*", {git, "https://github.com/extend/cowboy.git", {tag, "0.8.3"}}}
+  {cowboy, ".*", {git, "https://github.com/ninenines/cowboy.git", {tag, "2.4.0"}}}
  ]}.

+ 13 - 21
frameworks/Erlang/cowboy/src/db_handler.erl

@@ -3,27 +3,19 @@
 %% @doc Hello world handler.
 -module(db_handler).
 
--export([init/3]).
--export([handle/2]).
--export([terminate/3]).
+-export([init/2]).
 
-init(_Transport, Req, []) ->
-	{ok, Req, undefined}.
 
-handle(Req, State) ->
-        random:seed(erlang:now()),
-        {JSON, Req2} = case cowboy_req:qs_val(<<"queries">>, Req) of
-		{undefined, Req1} ->
-			{result_packet, _, _, [[ID, Rand]], _} = emysql:execute(test_pool, db_stmt, [random:uniform(10000)]),
-			{[{[{<<"id">>, ID}, {<<"randomNumber">>, Rand}]}], Req1};
-		{N, Req1} ->
-			I = list_to_integer(binary_to_list(N)),
-			Res = [ {[{<<"id">>, ID}, {<<"randomNumber">>, Rand}]} || 
-			        {result_packet, _, _, [[ID, Rand]], _} <- [emysql:execute(test_pool, db_stmt, [random:uniform(10000)]) || _ <- lists:seq(1, I) ]],
-			{Res, Req1}
-		end,
-	{ok, Req3} = cowboy_req:reply(200, [{<<"Content-Type">>, <<"application/json">>}], jiffy:encode(lists:nth(1,JSON)), Req2),
+init(Req, State) ->
+    QsVals = cowboy_req:parse_qs(Req),
+    JSON = case lists:keyfind(<<"queries">>, 1, QsVals) of
+               false ->
+                   {result_packet, _, _, [[ID, Rand]], _} = emysql:execute(test_pool, db_stmt, [rand:uniform(10000)]),
+                   [{[{<<"id">>, ID}, {<<"randomNumber">>, Rand}]}];
+               {_, N} ->
+                   I = binary_to_integer(N),
+                   [ {[{<<"id">>, ID}, {<<"randomNumber">>, Rand}]} ||
+                       {result_packet, _, _, [[ID, Rand]], _} <- [emysql:execute(test_pool, db_stmt, [rand:uniform(10000)]) || _ <- lists:seq(1, I) ]]
+           end,
+    Req3 = cowboy_req:reply(200, #{<<"Content-Type">> => <<"application/json">>}, jiffy:encode(lists:nth(1,JSON)), Req),
 	{ok, Req3, State}.
-
-terminate(_Reason, _Req, _State) ->
-	ok.

+ 2 - 1
frameworks/Erlang/cowboy/src/hello_world.erl

@@ -8,7 +8,8 @@
 %% API.
 
 start() ->
-	ok = application:start(crypto),
+        ssl:start(),	
 	ok = application:start(ranch),
+	ok = application:start(cowlib),
 	ok = application:start(cowboy),
 	ok = application:start(hello_world).

+ 4 - 5
frameworks/Erlang/cowboy/src/hello_world_app.erl

@@ -10,8 +10,8 @@
 
 %% API.
 
-%% NOTE: 
-%%   If size of db testpool is too big (e.g: 5000), 
+%% NOTE:
+%%   If size of db testpool is too big (e.g: 5000),
 %%   it will fail travis ci test. So I shrink this to 256.
 %%   [email protected]
 
@@ -31,9 +31,8 @@ start(_Type, _Args) ->
       {"/query", query_handler, []}
 		]}
 	]),
-	{ok, _} = cowboy:start_http(http, 256, [{port, 8080}], [
-		{env, [{dispatch, Dispatch}]}
-	]),
+	{ok, _} = cowboy:start_clear(http, [{port, 8080}], #{env => #{dispatch => Dispatch}}
+	),
 	hello_world_sup:start_link().
 
 stop(_State) ->

+ 3 - 11
frameworks/Erlang/cowboy/src/json_handler.erl

@@ -3,16 +3,8 @@
 %% @doc Hello world handler.
 -module(json_handler).
 
--export([init/3]).
--export([handle/2]).
--export([terminate/3]).
+-export([init/2]).
 
-init(_Transport, Req, []) ->
-	{ok, Req, undefined}.
-
-handle(Req, State) ->
-	{ok, Req2} = cowboy_req:reply(200, [{<<"Content-Type">>, <<"application/json">>}], jiffy:encode({[{<<"message">>, <<"Hello, World!">>}]}), Req),
+init(Req, State) ->
+	Req2 = cowboy_req:reply(200, #{<<"Content-Type">> => <<"application/json">>}, jiffy:encode({[{<<"message">>, <<"Hello, World!">>}]}), Req),
 	{ok, Req2, State}.
-
-terminate(_Reason, _Req, _State) ->
-	ok.

+ 3 - 11
frameworks/Erlang/cowboy/src/plaintext_handler.erl

@@ -3,16 +3,8 @@
 %% @doc Hello world handler.
 -module(plaintext_handler).
 
--export([init/3]).
--export([handle/2]).
--export([terminate/3]).
+-export([init/2]).
 
-init(_Transport, Req, []) ->
-  {ok, Req, undefined}.
-
-handle(Req, State) ->
-  {ok, Req2} = cowboy_req:reply(200, [{<<"Content-Type">>, <<"text/plain">>}], <<"Hello, World!">>, Req),
+init(Req, State) ->
+  Req2 = cowboy_req:reply(200, #{<<"Content-Type">> => <<"text/plain">>}, <<"Hello, World!">>, Req),
   {ok, Req2, State}.
-
-terminate(_Reason, _Req, _State) ->
-  ok.

+ 11 - 14
frameworks/Erlang/cowboy/src/query_handler.erl

@@ -3,16 +3,16 @@
 %% @doc Hello world handler.
 -module(query_handler).
 
--export([init/3]).
--export([handle/2]).
--export([terminate/3]).
+-export([init/2]).
 
-init(_Transport, Req, []) ->
-  {ok, Req, undefined}.
-
-handle(Req, State) ->
-        random:seed(erlang:now()),
-    {N, Req1} = cowboy_req:qs_val(<<"queries">>, Req, <<"1">>),
+init(Req, State) ->
+    QsVals = cowboy_req:parse_qs(Req),
+    N = case lists:keyfind(<<"queries">>, 1, QsVals) of
+            false ->
+                <<"1">>;
+            {_, BinaryN} ->
+                BinaryN
+        end,
 
       I = try binary_to_integer(N) of
             X when X > 500 -> 500;
@@ -21,10 +21,7 @@ handle(Req, State) ->
           catch error:badarg -> 1 end,
 
       JSON = [ {[{<<"id">>, ID}, {<<"randomNumber">>, Rand}]} ||
-              {result_packet, _, _, [[ID, Rand]], _} <- [emysql:execute(test_pool, db_stmt, [random:uniform(10000)]) || _ <- lists:seq(1, I) ]],
+              {result_packet, _, _, [[ID, Rand]], _} <- [emysql:execute(test_pool, db_stmt, [rand:uniform(10000)]) || _ <- lists:seq(1, I) ]],
 
-  {ok, Req2} = cowboy_req:reply(200, [{<<"Content-Type">>, <<"application/json">>}], jiffy:encode(JSON), Req1),
+    Req2 = cowboy_req:reply(200, #{<<"Content-Type">> => <<"application/json">>}, jiffy:encode(JSON), Req),
   {ok, Req2, State}.
-
-terminate(_Reason, _Req, _State) ->
-  ok.