Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#44] Introduce Primus/WS transfer configuration at run-time
- Loading branch information
Joseph Yiasemides
committed
May 28, 2018
1 parent
527bd15
commit 2d1938a
Showing
6 changed files
with
145 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"POAAgent":{ | ||
"transfers":[ | ||
{ | ||
"id":"node_integration", | ||
"address":"ws://localhost:3000/api", | ||
"identifier":"elixirNodeJSIntegration", | ||
"name":"Elixir-NodeJS-Integration", | ||
"secret":"Fr00b5", | ||
"contact":"a@b.c" | ||
} | ||
] | ||
} | ||
} |
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,50 @@ | ||
defmodule POAAgent.Configuration do | ||
@moduledoc false | ||
|
||
def transfers do | ||
elixir_config = Application.fetch_env!(:poa_agent, :transfers) | ||
case Application.fetch_env(:poa_agent, :transfer_config_overlay) do | ||
:error -> | ||
elixir_config | ||
{:ok, path} when is_binary(path) -> | ||
path | ||
|> File.read!() | ||
|> Jason.decode!() | ||
|> transfers(elixir_config) | ||
end | ||
end | ||
|
||
def transfers(_, []) do | ||
[] | ||
end | ||
|
||
def transfers(overlay, default) do | ||
overlay | ||
|> Kernel.get_in(["POAAgent", "transfers"]) | ||
|> Kernel.hd() | ||
|> merge_overlay_into_config(default) | ||
end | ||
|
||
defp merge_overlay_into_config(overlay, [{id, module_name, default}]) do | ||
keys = [ | ||
"address", | ||
"identifier", | ||
"name", | ||
"secret", | ||
"contact" | ||
] | ||
POAAgent.Plugins.Transfers.WebSocket.Primus = module_name | ||
^id = String.to_existing_atom(Map.fetch!(overlay, "id")) | ||
restricted = Map.take(overlay, keys) | ||
want = to_keyword(restricted) | ||
final = Keyword.merge(default, want) | ||
[{id, module_name, final}] | ||
end | ||
|
||
defp to_keyword(x) do | ||
f = fn {k, v} -> | ||
{String.to_existing_atom(k), v} | ||
end | ||
Enum.into(x, [], f) | ||
end | ||
end |
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,41 @@ | ||
defmodule POAAgent.ConfigurationTest do | ||
use ExUnit.Case | ||
|
||
test "transfer configuration overlay and default merge" do | ||
default = [ | ||
{:node_integration, POAAgent.Plugins.Transfers.WebSocket.Primus, [ | ||
address: "ws://localhost:3000/api", | ||
identifier: "elixirNodeJSIntegration", | ||
name: "Elixir-NodeJS-Integration", | ||
secret: "", | ||
contact: "mymail@mail.com" | ||
] | ||
} | ||
] | ||
overlay = %{"POAAgent" => | ||
%{"transfers" => | ||
[ | ||
%{"address" => "ws://localhost:3000/api", | ||
"contact" => "a@b.c", | ||
"id" => "node_integration", | ||
"identifier" => "elixirNodeJSIntegration", | ||
"name" => "Elixir-NodeJS-Integration", | ||
"secret" => "Fr00b5" | ||
} | ||
] | ||
} | ||
} | ||
|
||
assert POAAgent.Configuration.transfers(overlay, default) == [ | ||
{:node_integration, POAAgent.Plugins.Transfers.WebSocket.Primus, | ||
[address: "ws://localhost:3000/api", | ||
contact: "a@b.c", | ||
identifier: "elixirNodeJSIntegration", | ||
name: "Elixir-NodeJS-Integration", | ||
secret: "Fr00b5" | ||
] | ||
} | ||
] | ||
assert POAAgent.Configuration.transfers(overlay, []) == [] | ||
end | ||
end |