this repo has no description

Create helper module for reading system configuration

This provides us with simple approach to reading refresh duration in
human-readable way.

hauleth.dev 3929634f 3b882f27

verified
Changed files
+92 -5
config
lib
test
+5
config/runtime.exs
···
import Config
+
alias EslHn.ConfigReader, as: CR
+
# ## Using releases
#
# If you use `mix release`, you need to explicitly enable the server
···
if System.get_env("PHX_SERVER") do
config :esl_hn, EslHnWeb.Endpoint, server: true
end
+
+
config :esl_hn,
+
refresh: CR.duration("ESL_HN_REFRESH", {:minutes, 5})
if config_env() == :prod do
secret_key_base =
+36
lib/esl_hn/config_reader.ex
···
+
defmodule EslHn.ConfigReader do
+
@moduledoc """
+
Configuration helpers for reading different data from system environment
+
+
TODO: Extract that as a library as it is yet another project where I need
+
something like that
+
"""
+
+
def duration(name, default) do
+
case System.fetch_env(name) do
+
:error -> default
+
{:ok, ""} -> default
+
{:ok, value} -> parse_duration(value)
+
end
+
|> normalise_duration()
+
end
+
+
defp parse_duration(value) do
+
case Integer.parse(value) do
+
{value, ""} -> value
+
{value, "s"} -> {:seconds, value}
+
{value, "m"} -> {:minutes, value}
+
{value, "h"} -> {:hours, value}
+
end
+
end
+
+
defp normalise_duration(milli) when is_integer(milli), do: milli
+
+
defp normalise_duration({:seconds, s}) when is_integer(s),
+
do: :timer.seconds(s)
+
+
defp normalise_duration({:minutes, m}) when is_integer(m),
+
do: :timer.minutes(m)
+
+
defp normalise_duration({:hours, h}) when is_integer(h), do: :timer.hours(h)
+
end
+1 -2
mix.exs
···
{:credo, ">= 0.0.0", only: [:dev]},
# Testing
-
{:test_server, "~> 0.1.21", only: [:test]},
-
{:repatch, "~> 1.6", only: [:test]}
+
{:test_server, "~> 0.1.21", only: [:test]}
]
end
-2
mix.lock
···
"credo": {:hex, :credo, "1.7.12", "9e3c20463de4b5f3f23721527fcaf16722ec815e70ff6c60b86412c695d426c1", [:mix], [{:bunt, "~> 0.2.1 or ~> 1.0", [hex: :bunt, repo: "hexpm", optional: false]}, {:file_system, "~> 0.2 or ~> 1.0", [hex: :file_system, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "8493d45c656c5427d9c729235b99d498bd133421f3e0a683e5c1b561471291e5"},
"decimal": {:hex, :decimal, "2.3.0", "3ad6255aa77b4a3c4f818171b12d237500e63525c2fd056699967a3e7ea20f62", [:mix], [], "hexpm", "a4d66355cb29cb47c3cf30e71329e58361cfcb37c34235ef3bf1d7bf3773aeac"},
"ecto": {:hex, :ecto, "3.13.2", "7d0c0863f3fc8d71d17fc3ad3b9424beae13f02712ad84191a826c7169484f01", [:mix], [{:decimal, "~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "669d9291370513ff56e7b7e7081b7af3283d02e046cf3d403053c557894a0b3e"},
-
"ex2ms": {:hex, :ex2ms, "1.7.0", "45b9f523d0b777667ded60070d82d871a37e294f0b6c5b8eca86771f00f82ee1", [:mix], [], "hexpm", "2589eee51f81f1b1caa6d08c990b1ad409215fe6f64c73f73c67d36ed10be827"},
"file_system": {:hex, :file_system, "1.1.0", "08d232062284546c6c34426997dd7ef6ec9f8bbd090eb91780283c9016840e8f", [:mix], [], "hexpm", "bfcf81244f416871f2a2e15c1b515287faa5db9c6bcf290222206d120b3d43f6"},
"finch": {:hex, :finch, "0.20.0", "5330aefb6b010f424dcbbc4615d914e9e3deae40095e73ab0c1bb0968933cadf", [:mix], [{:mime, "~> 1.0 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:mint, "~> 1.6.2 or ~> 1.7", [hex: :mint, repo: "hexpm", optional: false]}, {:nimble_options, "~> 0.4 or ~> 1.0", [hex: :nimble_options, repo: "hexpm", optional: false]}, {:nimble_pool, "~> 1.1", [hex: :nimble_pool, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "2658131a74d051aabfcba936093c903b8e89da9a1b63e430bee62045fa9b2ee2"},
"hpax": {:hex, :hpax, "1.0.3", "ed67ef51ad4df91e75cc6a1494f851850c0bd98ebc0be6e81b026e765ee535aa", [:mix], [], "hexpm", "8eab6e1cfa8d5918c2ce4ba43588e894af35dbd8e91e6e55c817bca5847df34a"},
···
"phoenix_template": {:hex, :phoenix_template, "1.0.4", "e2092c132f3b5e5b2d49c96695342eb36d0ed514c5b252a77048d5969330d639", [:mix], [{:phoenix_html, "~> 2.14.2 or ~> 3.0 or ~> 4.0", [hex: :phoenix_html, repo: "hexpm", optional: true]}], "hexpm", "2c0c81f0e5c6753faf5cca2f229c9709919aba34fab866d3bc05060c9c444206"},
"plug": {:hex, :plug, "1.18.1", "5067f26f7745b7e31bc3368bc1a2b818b9779faa959b49c934c17730efc911cf", [:mix], [{:mime, "~> 1.0 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:plug_crypto, "~> 1.1.1 or ~> 1.2 or ~> 2.0", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4.3 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "57a57db70df2b422b564437d2d33cf8d33cd16339c1edb190cd11b1a3a546cc2"},
"plug_crypto": {:hex, :plug_crypto, "2.1.1", "19bda8184399cb24afa10be734f84a16ea0a2bc65054e23a62bb10f06bc89491", [:mix], [], "hexpm", "6470bce6ffe41c8bd497612ffde1a7e4af67f36a15eea5f921af71cf3e11247c"},
-
"repatch": {:hex, :repatch, "1.6.0", "9d5d2f97b66179dc596e9e8984d46c1de34d4d1e6b9af0b5c188e68e8f270445", [:mix], [{:ex2ms, "~> 1.7.0", [hex: :ex2ms, repo: "hexpm", optional: false]}], "hexpm", "6e89e75c5b120b52fa01892bf603e43f1213f5fd6604e60b7ba4efbf3c48d3b4"},
"req": {:hex, :req, "0.5.15", "662020efb6ea60b9f0e0fac9be88cd7558b53fe51155a2d9899de594f9906ba9", [:mix], [{:brotli, "~> 0.3.1", [hex: :brotli, repo: "hexpm", optional: true]}, {:ezstd, "~> 1.0", [hex: :ezstd, repo: "hexpm", optional: true]}, {:finch, "~> 0.17", [hex: :finch, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}, {:mime, "~> 2.0.6 or ~> 2.1", [hex: :mime, repo: "hexpm", optional: false]}, {:nimble_csv, "~> 1.0", [hex: :nimble_csv, repo: "hexpm", optional: true]}, {:plug, "~> 1.0", [hex: :plug, repo: "hexpm", optional: true]}], "hexpm", "a6513a35fad65467893ced9785457e91693352c70b58bbc045b47e5eb2ef0c53"},
"telemetry": {:hex, :telemetry, "1.3.0", "fedebbae410d715cf8e7062c96a1ef32ec22e764197f70cda73d82778d61e7a2", [:rebar3], [], "hexpm", "7015fc8919dbe63764f4b4b87a95b7c0996bd539e0d499be6ec9d7f3875b79e6"},
"telemetry_metrics": {:hex, :telemetry_metrics, "1.1.0", "5bd5f3b5637e0abea0426b947e3ce5dd304f8b3bc6617039e2b5a008adc02f8f", [:mix], [{:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "e7b79e8ddfde70adb6db8a6623d1778ec66401f366e9a8f5dd0955c56bc8ce67"},
+49
test/esl_hn/config_reader_test.exs
···
+
defmodule EslHn.ConfigReaderTest do
+
# Theoretically as we use system environment this shouldn't be async-safe,
+
# but due to fact that all these functions use unique environment variable
+
# name, and that name has little chance to being used I thing we can live with
+
# that and use that code in given way.
+
use ExUnit.Case, async: true
+
+
@subject EslHn.ConfigReader
+
+
doctest @subject
+
+
setup ctx do
+
env = to_string(ctx.test)
+
+
on_exit(fn ->
+
System.delete_env(env)
+
end)
+
+
{:ok, env: env}
+
end
+
+
describe "duration/2" do
+
test "if environment variable is not set, then return default", ctx do
+
assert 2137 == @subject.duration(ctx.env, 2137)
+
end
+
+
test "if environment variable is empty, then default is returned", ctx do
+
System.put_env(ctx.env, "")
+
+
assert 2137 == @subject.duration(ctx.env, 2137)
+
end
+
+
test "if environment variable contains integer it will be parsed", ctx do
+
System.put_env(ctx.env, "420")
+
+
assert 420 == @subject.duration(ctx.env, 2137)
+
end
+
+
for {suffix, name} <- [s: :seconds, m: :minutes, h: :hours] do
+
test "if environment variable contains integer with `#{suffix}` suffix it will be treated as #{name}",
+
ctx do
+
System.put_env(ctx.env, unquote("420#{suffix}"))
+
+
assert apply(:timer, unquote(name), [420]) ==
+
@subject.duration(ctx.env, 2137)
+
end
+
end
+
end
+
end
+1 -1
test/esl_hn/hn_test.exs
···
defmodule EslHn.HnTest do
-
use ExUnit.Case, async: false
+
use ExUnit.Case, async: true
alias Plug.Conn