Skip to content

Commit

Permalink
[#49] Update to Cowboy 2
Browse files Browse the repository at this point in the history
  • Loading branch information
jfacorro committed Nov 29, 2017
1 parent 2ffd764 commit 206f837
Show file tree
Hide file tree
Showing 11 changed files with 86 additions and 98 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
sudo: false
language: erlang
otp_release:
- 19.2
- 18.3
- 20.1
- 19.3
before_install:
- ./ci before_install "${PWD:?}"/rebar3
install:
Expand Down
1 change: 0 additions & 1 deletion example/config/sys.config
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
{example,
[
{http_port, 8080},
{http_listener_count, 10}
]
},

Expand Down
22 changes: 8 additions & 14 deletions example/src/example.erl
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,16 @@ stop(_State) ->
-spec start_phase(atom(), application:start_type(), []) -> ok | {error, term()}.
start_phase(start_trails_http, _StartType, []) ->
{ok, Port} = application:get_env(example, http_port),
{ok, ListenerCount} = application:get_env(example, http_listener_count),
Trails = trails:trails([example_echo_handler,
example_description_handler,
cowboy_swagger_handler]),
trails:store(Trails),
Dispatch = trails:single_host_compile(Trails),
RanchOptions = [{port, Port}],
CowboyOptions =
[
{env,
[
{dispatch, Dispatch}
]},
{compress, true},
{timeout, 12000}
],
{ok, _} =
cowboy:start_http(example_http, ListenerCount, RanchOptions, CowboyOptions),
Dispatch = trails:single_host_compile(Trails),
RanchOptions = [{port, Port}],
CowboyOptions = #{ env => #{dispatch => Dispatch}
, compress => true
, timeout => 12000
},

{ok, _} = cowboy:start_clear(example_http, RanchOptions, CowboyOptions),
ok.
2 changes: 1 addition & 1 deletion rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
%% == Dependencies ==

{deps, [ {jsx, "2.8.1"}
, {trails, "0.2.1"}
, {trails, "2.0.0"}
]}.

%% == Dialyzer ==
Expand Down
16 changes: 8 additions & 8 deletions rebar.lock
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{"1.1.0",
[{<<"cowboy">>,{pkg,<<"cowboy">>,<<"1.0.4">>},1},
{<<"cowlib">>,{pkg,<<"cowlib">>,<<"1.0.2">>},2},
[{<<"cowboy">>,{pkg,<<"cowboy">>,<<"2.1.0">>},1},
{<<"cowlib">>,{pkg,<<"cowlib">>,<<"2.0.1">>},2},
{<<"jsx">>,{pkg,<<"jsx">>,<<"2.8.1">>},0},
{<<"ranch">>,{pkg,<<"ranch">>,<<"1.2.1">>},1},
{<<"trails">>,{pkg,<<"trails">>,<<"0.2.1">>},0}]}.
{<<"ranch">>,{pkg,<<"ranch">>,<<"1.4.0">>},1},
{<<"trails">>,{pkg,<<"trails">>,<<"2.0.0">>},0}]}.
[
{pkg_hash,[
{<<"cowboy">>, <<"A324A8DF9F2316C833A470D918AAF73AE894278B8AA6226CE7A9BF699388F878">>},
{<<"cowlib">>, <<"9D769A1D062C9C3AC753096F868CA121E2730B9A377DE23DEC0F7E08B1DF84EE">>},
{<<"cowboy">>, <<"69F9DB3B23C24AB6B3A169A6357130C16B39CDA1A1F8C582F818883EE552589D">>},
{<<"cowlib">>, <<"4DFFFB1DB296EAB9F2E8B95EE3017007F674BC920CE30AEB5A53BBDA82FC38C0">>},
{<<"jsx">>, <<"1453B4EB3615ACB3E2CD0A105D27E6761E2ED2E501AC0B390F5BBEC497669846">>},
{<<"ranch">>, <<"A6FB992C10F2187B46FFD17CE398DDF8A54F691B81768F9EF5F461EA7E28C762">>},
{<<"trails">>, <<"6769F7A2B777EC9CEAF07F22C23E5F3BE6A7F07E9A11B44725B102A832334D46">>}]}
{<<"ranch">>, <<"10272F95DA79340FA7E8774BA7930B901713D272905D0012B06CA6D994F8826B">>},
{<<"trails">>, <<"40125994045FBDB07909D0778E12004B3B7722E25AAE292EC83019630BA66E7E">>}]}
].
19 changes: 5 additions & 14 deletions src/cowboy_swagger_json_handler.erl
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,24 @@
-module(cowboy_swagger_json_handler).

%% Cowboy callbacks
-export([ init/3
, rest_init/2
-export([ init/2
, content_types_provided/2
]).

%% Handlers
-export([handle_get/2]).

-type state() :: #{}.
-type route_match() :: '_' | iodata().
-type options() :: #{server => ranch:ref(), host => route_match()}.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Cowboy Callbacks
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%% @hidden
-spec init({atom(), atom()}, cowboy_req:req(), options()) ->
{upgrade, protocol, cowboy_rest}.
init(_Transport, _Req, _Opts) ->
{upgrade, protocol, cowboy_rest}.

%% @hidden
-spec rest_init(cowboy_req:req(), options()) ->
{ok, cowboy_req:req(), options()}.
rest_init(Req, Opts) ->
{ok, Req, Opts}.
-spec init(cowboy_req:req(), state()) ->
{cowboy_rest, cowboy_req:req(), state()}.
init(Req, State) ->
{cowboy_rest, Req, State}.

%% @hidden
-spec content_types_provided(cowboy_req:req(), state()) ->
Expand Down
11 changes: 5 additions & 6 deletions src/cowboy_swagger_redirect_handler.erl
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
-module(cowboy_swagger_redirect_handler).

%% Cowboy callbacks
-export([init/3]).
-export([init/2]).

%% Handlers
-export([resource_exists/2, previously_existed/2, moved_permanently/2]).

-type state() :: #{}.
-type options() :: any().

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Cowboy Callbacks
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%% @hidden
-spec init({atom(), atom()}, cowboy_req:req(), options()) ->
{upgrade, protocol, cowboy_rest}.
init(_Transport, _Req, _Opts) ->
{upgrade, protocol, cowboy_rest}.
-spec init(cowboy_req:req(), state()) ->
{cowboy_rest, cowboy_req:req(), state()}.
init(Req, State) ->
{cowboy_rest, Req, State}.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Handlers
Expand Down
7 changes: 6 additions & 1 deletion test/cowboy_swagger_meta_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,15 @@
]
}]).

-export([init_per_suite/1]).
-export([ init_per_suite/1
, end_per_suite/1
]).

-type config() :: [{atom(), term()}].

-spec init_per_suite(config()) -> config().
init_per_suite(Config) ->
[ {application, cowboy_swagger} | Config].

-spec end_per_suite(config()) -> config().
end_per_suite(Config) -> Config.
36 changes: 19 additions & 17 deletions test/example.erl
Original file line number Diff line number Diff line change
Expand Up @@ -8,41 +8,43 @@

%% application
%% @doc Starts the application
-spec start() -> {ok, [atom()]}.
start() ->
application:ensure_all_started(example).

%% @doc Stops the application
-spec stop() -> ok.
stop() ->
application:stop(example).

%% behaviour
%% @private
-spec start(normal, [any()]) -> {ok, pid()} |
{ok, pid(), any()} |
{error, any()}.
start(_StartType, _StartArgs) ->
example_sup:start_link().

%% @private
-spec stop(_) -> ok.
stop(_State) ->
ok = cowboy:stop_listener(example_http).

-spec start_phase(atom(), application:start_type(), []) -> ok | {error, term()}.
start_phase(start_trails_http, _StartType, []) ->
{ok, Port} = application:get_env(example, http_port),
{ok, ListenerCount} = application:get_env(example, http_listener_count),
Trails = trails:trails([example_echo_handler,
example_description_handler,
cowboy_swagger_handler]),
Trails = trails:trails([ example_echo_handler
, example_description_handler
, cowboy_swagger_handler
]),
trails:store(Trails),
Dispatch = trails:single_host_compile(Trails),
RanchOptions = [{port, Port}],
CowboyOptions =
[
{env,
[
{dispatch, Dispatch}
]},
{compress, true},
{timeout, 12000}
],
{ok, _} =
cowboy:start_http(example_http, ListenerCount, RanchOptions, CowboyOptions),

Dispatch = trails:single_host_compile(Trails),
RanchOptions = [{port, Port}],
CowboyOptions = #{ env => #{dispatch => Dispatch}
, compress => true
, timeout => 12000
},

{ok, _} = cowboy:start_clear(example_http, RanchOptions, CowboyOptions),
ok.
24 changes: 15 additions & 9 deletions test/multiple_hosts_servers_example.erl
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,28 @@

%% application
%% @doc Starts the application
-spec start() -> {ok, [atom()]}.
start() ->
application:ensure_all_started(multiple_hosts_servers_example).

%% @doc Stops the application
-spec stop() -> ok.
stop() ->
application:stop(multiple_hosts_servers_example).

%% behaviour
%% @private
-spec start(normal, [any()]) -> {ok, pid()} |
{ok, pid(), any()} |
{error, any()}.
start(_StartType, _StartArgs) ->
_ = application:stop(lager),
ok = application:stop(sasl),
{ok, _} = application:ensure_all_started(sasl),
{ok, self()}.

%% @private
-spec stop(_) -> ok.
stop(_State) ->
ok = cowboy:stop_listener(multiple_hosts_servers_http).

Expand All @@ -34,8 +40,6 @@ start_phase(start_multiple_hosts_servers_example_http, _StartType, []) ->
application:get_env(multiple_hosts_servers_example, api1),
{ok, #{hosts := ['_'], port := Port2}} =
application:get_env(multiple_hosts_servers_example, api2),
{ok, ListenerCount} =
application:get_env(multiple_hosts_servers_example, http_listener_count),

Trails11 =
trails:trails(example_echo_handler) ++
Expand All @@ -47,20 +51,22 @@ start_phase(start_multiple_hosts_servers_example_http, _StartType, []) ->

trails:store(api1, Routes1),
Dispatch1 = trails:compile(Routes1),
{ok, _} = start_cowboy(api1, ListenerCount, Dispatch1, Port1),
{ok, _} = start_cowboy(api1, Dispatch1, Port1),

Trails21 =
trails:trails([host1_handler, example_echo_handler]) ++
cowboy_swagger_handler:trails(#{server => api2}),

trails:store(api2, Trails21),
Dispatch2 = trails:single_host_compile(Trails21),
{ok, _} = start_cowboy(api2, ListenerCount, Dispatch2, Port2),
{ok, _} = start_cowboy(api2, Dispatch2, Port2),
ok.

%% @private
start_cowboy(Server, ListenerCount, Dispatch, Port) ->
RanchOptions = [{port, Port}],
CowboyOptions =
[{env, [{dispatch, Dispatch}]}, {compress, true}, {timeout, 12000}],
cowboy:start_http(Server, ListenerCount, RanchOptions, CowboyOptions).
start_cowboy(Server, Dispatch, Port) ->
RanchOptions = [{port, Port}],
CowboyOptions = #{ env => #{dispatch => Dispatch}
, compress => true
, timeout => 12000
},
cowboy:start_clear(Server, RanchOptions, CowboyOptions).
42 changes: 17 additions & 25 deletions test/test.config
Original file line number Diff line number Diff line change
@@ -1,27 +1,19 @@
[
{example,
[
{http_port, 8080},
{http_listener_count, 10}
]
},
[ { example
, [{http_port, 8080}]
}

{multiple_hosts_servers_example,
[
{http_listener_count, 10},
{api1, #{hosts => ["localhost", "127.0.0.1"], port => 8383}},
{api2, #{hosts => ['_'], port => 8282}}
]
},

{cowboy_swagger,
[
{global_spec,
#{swagger => "2.0",
info => #{title => "Example API"},
basePath => ""
}
}
]
}
, { multiple_hosts_servers_example
, [ {api1, #{hosts => ["localhost", "127.0.0.1"], port => 8383}}
, {api2, #{hosts => ['_'], port => 8282}}
]
}
, { cowboy_swagger
, [ { global_spec
, #{ swagger => "2.0"
, info => #{title => "Example API"}
, basePath => ""
}
}
]
}
].

0 comments on commit 206f837

Please sign in to comment.