Skip to content

Commit

Permalink
WIP OS Env Var to choose cowboy version
Browse files Browse the repository at this point in the history
- default is cowboy 2.x
- to use 1.x $COWBOY_VERSION needs to be set to a value
  starting with "1" (same method as Elixir Plug)
- no runtime detection of available cowboy version
  • Loading branch information
gomoripeti committed Nov 16, 2017
1 parent 2d68421 commit aa6f487
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 19 deletions.
22 changes: 14 additions & 8 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,30 @@ branches:
only:
- master
- release_2.0
otp_release:
- 20.0
- 19.3
- 18.3
- 17.5
- R16B03-1
- cowboy
matrix:
include:
- otp_release: 20.0
- otp_release: 19.3
- otp_release: 18.3
env: COWBOY_VERSION=1.1.2
- otp_release: 17.5
env: COWBOY_VERSION=1.1.2
- otp_release: R16B03-1
env: COWBOY_VERSION=1.1.2
cache:
directories:
- $HOME/.cache/rebar3
- priv/node_modules
install:
- ./rebar3 deps
- DEBUG=1 ./rebar3 deps
- nvm install 6.2 && nvm use 6.2
- make bootstrap_front_end
script:
- make test
- make test_front_end
- if [ $TRAVIS_OTP_RELEASE = "20.0" ]; then make doc dialyzer; fi
- if [ $TRAVIS_OTP_RELEASE = "17.5" ]; then make dialyzer; fi # run dialyzer with cowboy 1.x too
after_success:
- ./rebar3 cover
- if [ $TRAVIS_OTP_RELEASE = "18.3" ]; then ./rebar3 coveralls send; fi
- if [ $TRAVIS_OTP_RELEASE = "20.0" ]; then ./rebar3 coveralls send; fi
2 changes: 1 addition & 1 deletion apps/xprof_gui/rebar.config
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{deps,
[{cowboy, "1.1.2"},
[{cowboy, "~> 2.0.0"},
{lager, "3.2.4"},
{jsone, "1.3.1"}
]}.
22 changes: 22 additions & 0 deletions apps/xprof_gui/rebar.config.script
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
case os:getenv("COWBOY_VERSION") of
"1" ++ _ ->
ErlOpts = proplists:get_value(erl_opts, CONFIG, []),
NewErlOpts = {erl_opts, [{d, 'COWBOY_VERSION_1'}|ErlOpts]},
Config2 = lists:keystore(erl_opts, 1, CONFIG, NewErlOpts),

Deps = proplists:get_value(deps, CONFIG, []),
NewDeps = {deps, lists:keystore(cowboy, 1, Deps, {cowboy, "1.1.2"})},
lists:keystore(deps, 1, Config2, NewDeps);
_ ->
CONFIG
end.










9 changes: 7 additions & 2 deletions apps/xprof_gui/src/xprof_gui_app.erl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
-define(APP, xprof_gui).
-define(DEF_WEB_IF_PORT, 7890).
-define(LISTENER, xprof_http_listener).
-ifdef(COWBOY_VERSION_1).
-define(HANDLER_MOD, xprof_gui_cowboy1_handler).
-else.
-define(HANDLER_MOD, xprof_gui_cowboy2_handler).
-endif.

%% Application callbacks

Expand All @@ -30,8 +35,8 @@ stop(_State) ->

start_cowboy() ->
Port = application:get_env(?APP, port, ?DEF_WEB_IF_PORT),
Dispatch = cowboy_dispatch(xprof_gui_cowboy1_handler),
xprof_gui_cowboy1_handler:start_listener(?LISTENER, Port, Dispatch).
Dispatch = cowboy_dispatch(?HANDLER_MOD),
?HANDLER_MOD:start_listener(?LISTENER, Port, Dispatch).

cowboy_dispatch(Mod) ->
Routes =
Expand Down
4 changes: 4 additions & 0 deletions apps/xprof_gui/src/xprof_gui_cowboy1_handler.erl
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
%%% @doc Cowboy 1.x compatible HTTP handler
-module(xprof_gui_cowboy1_handler).

-ifdef(COWBOY_VERSION_1).

-behavior(cowboy_http_handler).

%% xprof_gui_app callback
Expand Down Expand Up @@ -46,3 +48,5 @@ handle(Req0, State) ->

terminate(_Reason, _Req, _State) ->
ok.

-endif.
42 changes: 42 additions & 0 deletions apps/xprof_gui/src/xprof_gui_cowboy2_handler.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
%%% @doc Cowboy 2.x compatible HTTP handler
-module(xprof_gui_cowboy2_handler).

-ifndef(COWBOY_VERSION_1).

-behavior(cowboy_handler).

%% xprof_gui_app callback
-export([start_listener/3]).

%% Cowboy 2.x callback
-export([init/2]).

-define(HDR_JSON, #{<<"content-type">> => <<"application/json">>}).

%% In case an XHR receives no content with no content-type Firefox will emit
%% the following error: "XML Parsing Error: no root element found..."
%% As a workaround always return a content-type of octet-stream with
%% 204 No Content responses
-define(HDR_NO_CONTENT, #{<<"content-type">> => <<"application/octet-stream">>}).

%% xprof_gui_app callback

start_listener(Name, Port, Dispatch) ->
cowboy:start_clear(Name, [{port, Port}],
#{env => #{dispatch => Dispatch}}).

%% Cowboy 2.x callback

init(Req0, State) ->
What = cowboy_req:binding(what, Req0),
Params = cowboy_req:parse_qs(Req0),
Req =
case xprof_gui_rest:handle_req(What, Params) of
{StatusCode, Json} when is_integer(StatusCode), is_binary(Json) ->
cowboy_req:reply(StatusCode, ?HDR_JSON, Json, Req0);
StatusCode when is_integer(StatusCode) ->
cowboy_req:reply(StatusCode, ?HDR_NO_CONTENT, Req0)
end,
{ok, Req, State}.

-endif.
4 changes: 2 additions & 2 deletions rebar.config.script
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
case os:getenv("TRAVIS") of
"true" ->
JobId = os:getenv("TRAVIS_JOB_ID"),
JobId = os:getenv("TRAVIS_JOB_ID"),
lists:keystore(coveralls_service_job_id, 1, CONFIG, {coveralls_service_job_id, JobId});

_ ->
CONFIG
end.
end.
12 changes: 6 additions & 6 deletions rebar.lock
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
{"1.1.0",
[{<<"cowboy">>,{pkg,<<"cowboy">>,<<"1.1.2">>},0},
{<<"cowlib">>,{pkg,<<"cowlib">>,<<"1.0.2">>},1},
[{<<"cowboy">>,{pkg,<<"cowboy">>,<<"2.0.0">>},0},
{<<"cowlib">>,{pkg,<<"cowlib">>,<<"2.0.1">>},1},
{<<"customized_hdr_histogram">>,
{pkg,<<"customized_hdr_histogram">>,<<"0.3.2">>},
0},
{<<"goldrush">>,{pkg,<<"goldrush">>,<<"0.1.9">>},1},
{<<"jsone">>,{pkg,<<"jsone">>,<<"1.3.1">>},0},
{<<"lager">>,{pkg,<<"lager">>,<<"3.2.4">>},0},
{<<"ranch">>,{pkg,<<"ranch">>,<<"1.3.2">>},1}]}.
{<<"ranch">>,{pkg,<<"ranch">>,<<"1.4.0">>},1}]}.
[
{pkg_hash,[
{<<"cowboy">>, <<"61AC29EA970389A88ECA5A65601460162D370A70018AFE6F949A29DCA91F3BB0">>},
{<<"cowlib">>, <<"9D769A1D062C9C3AC753096F868CA121E2730B9A377DE23DEC0F7E08B1DF84EE">>},
{<<"cowboy">>, <<"A3B680BCC1156C6FBCB398CC56ADC35177037012D7DC28D8F7E7926D6C243561">>},
{<<"cowlib">>, <<"4DFFFB1DB296EAB9F2E8B95EE3017007F674BC920CE30AEB5A53BBDA82FC38C0">>},
{<<"customized_hdr_histogram">>, <<"14DDAE316FB694455FCC20CBD1AD5F057E421132E562AFB93BD81EDA44BD937D">>},
{<<"goldrush">>, <<"F06E5D5F1277DA5C413E84D5A2924174182FB108DABB39D5EC548B27424CD106">>},
{<<"jsone">>, <<"133BE761810EC0E94E05D4156944FFC35D02D6A88FA8FDFC2AF29BA53EADB377">>},
{<<"lager">>, <<"A6DEB74DAE7927F46BD13255268308EF03EB206EC784A94EAF7C1C0F3B811615">>},
{<<"ranch">>, <<"E4965A144DC9FBE70E5C077C65E73C57165416A901BD02EA899CFD95AA890986">>}]}
{<<"ranch">>, <<"10272F95DA79340FA7E8774BA7930B901713D272905D0012B06CA6D994F8826B">>}]}
].

0 comments on commit aa6f487

Please sign in to comment.