-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Adrián Quintás
committed
Mar 11, 2018
1 parent
2231c51
commit a2a2378
Showing
7 changed files
with
127 additions
and
88 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
defmodule MetadataPlugs.Info do | ||
@behaviour Plug | ||
import Plug.Conn | ||
|
||
@path "/info" | ||
|
||
@typedoc """ | ||
- `:env_vars` -- String list of environment variables names to get the value from (default: []). | ||
- `:path` -- (Optional) Info endpoint path (default: `/info`). | ||
""" | ||
|
||
@type opts :: [ | ||
path: String.t(), | ||
env_vars: list(String.t()) | ||
] | ||
|
||
@spec init(opts :: opts) :: map | ||
def init(opts) do | ||
default_opts = [ | ||
path: @path, | ||
env_vars: [] | ||
] | ||
|
||
Keyword.merge(default_opts, opts) | ||
end | ||
|
||
@doc """ | ||
Resolves a health request | ||
""" | ||
@spec call(Plug.Conn.t(), map) :: Plug.Conn.t() | ||
def call(conn, opts) do | ||
if conn.request_path == opts[:path] and conn.method == "GET" do | ||
send_info(conn, opts[:env_vars]) | ||
else | ||
conn | ||
end | ||
end | ||
|
||
@spec send_info(Plug.Conn.t(), list(String.t())) :: Plug.Conn.t() | ||
defp send_info(conn, env_vars) do | ||
conn | ||
|> put_resp_content_type("application/json") | ||
|> send_resp(200, info_response(env_vars)) | ||
end | ||
|
||
@spec info_response(list(String.t())) :: Map.t() | ||
defp info_response(env_vars) do | ||
env_vars | ||
|> Enum.reduce(%{}, fn env_var, acc -> | ||
env_var_value = System.get_env(env_var) || "" | ||
Map.put(acc, env_var, env_var_value) | ||
end) | ||
|> Poison.encode!() | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
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,37 @@ | ||
defmodule MetadataPlugs.InfoTest do | ||
use ExUnit.Case | ||
use Plug.Test | ||
|
||
doctest MetadataPlugs.Info | ||
|
||
@path "/info" | ||
@env_vars ["APP_VERSION", "TEST_VAR"] | ||
|
||
@opts MetadataPlugs.Info.init(info_path: @path, env_vars: @env_vars) | ||
|
||
test "Given a list of env variables when call the info endpoint then value of the variables is returned" do | ||
Enum.each(@env_vars, &System.put_env(&1, "#{&1}_value")) | ||
|
||
expected_env_vars_response = | ||
@env_vars | ||
|> Enum.reduce(%{}, fn env_var, acc -> Map.put(acc, env_var, "#{env_var}_value") end) | ||
|> Poison.encode!() | ||
|
||
conn = | ||
:get | ||
|> conn(@path) | ||
|> MetadataPlugs.Info.call(@opts) | ||
|
||
assert conn.state == :sent | ||
assert conn.status == 200 | ||
assert conn.resp_body == expected_env_vars_response | ||
end | ||
|
||
test "when the conn does not match with the plug then the conn is returned" do | ||
another_conn = conn(:get, "/whatever") | ||
|
||
conn = MetadataPlugs.Info.call(another_conn, @opts) | ||
|
||
assert(conn == another_conn) | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.