-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #39 from inaka/harenson.34.allow-more-than-one-ser…
…ver-running-on-the-same-node [Fix #34] Allow more than one server running on the same node
- Loading branch information
Showing
8 changed files
with
246 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
-module(host1_handler). | ||
|
||
-include_lib("mixer/include/mixer.hrl"). | ||
-mixin([ | ||
{example_default, | ||
[ | ||
init/3, | ||
rest_init/2, | ||
content_types_accepted/2, | ||
content_types_provided/2, | ||
resource_exists/2 | ||
]} | ||
]). | ||
|
||
-export([ allowed_methods/2 | ||
, handle_get/2]). | ||
|
||
%trails | ||
-behaviour(trails_handler). | ||
-export([trails/0]). | ||
|
||
trails() -> | ||
Metadata = | ||
#{get => | ||
#{tags => ["whoami"], | ||
description => "Get hostname", | ||
produces => ["text/plain"] | ||
} | ||
}, | ||
[trails:trail( | ||
"/whoami", | ||
host1_handler, | ||
#{}, | ||
Metadata)]. | ||
|
||
%% cowboy | ||
allowed_methods(Req, State) -> | ||
{[<<"GET">>], Req, State}. | ||
|
||
%% internal | ||
handle_get(Req, State) -> | ||
{Host, Req1} = cowboy_req:host(Req), | ||
Body = <<"I am ", Host/binary>>, | ||
{Body, Req1, State}. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{application, multiple_hosts_servers_example, | ||
[ | ||
{description, "Cowboy Swagger Complex Example."}, | ||
{vsn, "0.1"}, | ||
{applications, | ||
[ kernel | ||
, stdlib | ||
, sasl | ||
|
||
, cowboy | ||
, trails | ||
, cowboy_swagger | ||
]}, | ||
{modules, []}, | ||
{mod, {multiple_hosts_servers_example, []}}, | ||
{start_phases, [{start_multiple_hosts_servers_example_http, []}]} | ||
] | ||
}. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
-module(multiple_hosts_servers_example). | ||
|
||
-export([start/0]). | ||
-export([start/2]). | ||
-export([stop/0]). | ||
-export([stop/1]). | ||
-export([start_phase/3]). | ||
|
||
%% application | ||
%% @doc Starts the application | ||
start() -> | ||
application:ensure_all_started(multiple_hosts_servers_example). | ||
|
||
%% @doc Stops the application | ||
stop() -> | ||
application:stop(multiple_hosts_servers_example). | ||
|
||
%% behaviour | ||
%% @private | ||
start(_StartType, _StartArgs) -> | ||
_ = application:stop(lager), | ||
ok = application:stop(sasl), | ||
{ok, _} = application:ensure_all_started(sasl), | ||
{ok, self()}. | ||
|
||
%% @private | ||
stop(_State) -> | ||
ok = cowboy:stop_listener(multiple_hosts_servers_http). | ||
|
||
-spec start_phase(atom(), application:start_type(), []) -> ok | {error, term()}. | ||
start_phase(start_multiple_hosts_servers_example_http, _StartType, []) -> | ||
%% Host1 | ||
{ok, #{hosts := [HostMatch11, HostMatch12], port := Port1}} = | ||
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) ++ | ||
cowboy_swagger_handler:trails(#{server => api1, host => HostMatch11}), | ||
Trails12 = | ||
trails:trails(host1_handler) ++ | ||
cowboy_swagger_handler:trails(#{server => api1, host => HostMatch12}), | ||
Routes1 = [{HostMatch11, Trails11}, {HostMatch12, Trails12}], | ||
|
||
trails:store(api1, Routes1), | ||
Dispatch1 = trails:compile(Routes1), | ||
{ok, _} = start_cowboy(api1, ListenerCount, 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. | ||
|
||
%% @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). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters