Преглед изворни кода

Add a config helper module

theenglishway (time) пре 4 година
родитељ
комит
2f31d47141
3 измењених фајлова са 37 додато и 27 уклоњено
  1. 23 0
      config/helper.ex
  2. 12 25
      config/runtime.exs
  3. 2 2
      mix.exs

+ 23 - 0
config/helper.ex

@@ -0,0 +1,23 @@
+defmodule Config.Helper do
+  @doc """
+  Get a map of environment variables.
+
+  `env_list` contains a list of environment variables to load, either as a single
+  binary (e.g. "PORT") or a tuple of a binary and an informative message to
+  display if the variable is missing (e.g. {"PORT", "It should be around 4000"}).
+
+  The map key is the environment variable converted to lower-case
+  """
+  def load_all(env_list) when is_list(env_list) do
+    env_list
+    |> Map.new(fn
+      {env, msg} -> {env |> to_env_dict_key, load_from_env(env, msg)}
+      env when is_binary(env) -> {env |> to_env_dict_key, load_from_env(env)}
+    end)
+  end
+
+  def load_from_env(env, msg \\ "") when is_binary(env) and is_binary(msg),
+    do: System.get_env(env) || raise("environment variable #{env} is missing. \n#{msg}")
+
+  defp to_env_dict_key(env) when is_binary(env), do: env |> String.downcase() |> String.to_atom()
+end

+ 12 - 25
config/runtime.exs

@@ -1,4 +1,5 @@
 import Config
+alias Config.Helper
 
 # config/runtime.exs is executed for all environments, including
 # during releases. It is executed after compilation and before the
@@ -7,43 +8,29 @@ import Config
 # any compile-time configuration in here, as it won't be applied.
 # The block below contains prod specific runtime configuration.
 if config_env() == :prod do
-  secret_key_base =
-    System.get_env("SECRET_KEY_BASE") ||
-      raise """
-      environment variable SECRET_KEY_BASE is missing.
-      You can generate one by calling: mix phx.gen.secret
-      """
-
-  database_url =
-    System.get_env("DATABASE_URL") ||
-      raise """
-      environment variable DATABASE_URL is missing.
-      For example: ecto://USER:PASS@HOST/DATABASE
-      """
-
-  port =
-    System.get_env("PORT") ||
-      raise "environment variable PORT is missing."
-
-  host =
-    System.get_env("HOST") ||
-      raise "environment variable HOST is missing."
+  env =
+    Helper.load_all([
+      {"SECRET_KEY_BASE", "You can generate one by calling: mix phx.gen.secret"},
+      {"DATABASE_URL", "For example: ecto://USER:PASS@HOST/DATABASE"},
+      "PORT",
+      "HOST"
+    ])
 
   config :toy, ToyWeb.Endpoint,
-    url: [host: host, port: port],
+    url: [host: env.host, port: env.port],
     http: [
       # Enable IPv6 and bind on all interfaces.
       # Set it to  {0, 0, 0, 0, 0, 0, 0, 1} for local network only access.
       # See the documentation on https://hexdocs.pm/plug_cowboy/Plug.Cowboy.html
       # for details about using IPv6 vs IPv4 and loopback vs public addresses.
       ip: {0, 0, 0, 0, 0, 0, 0, 0},
-      port: port
+      port: env.port
     ],
-    secret_key_base: secret_key_base
+    secret_key_base: env.secret_key_base
 
   config :toy, Toy.Repo,
     # ssl: true,
-    url: database_url,
+    url: env.database_url,
     pool_size: String.to_integer(System.get_env("POOL_SIZE") || "10")
 
   # ## Using releases

+ 2 - 2
mix.exs

@@ -25,8 +25,8 @@ defmodule Toy.MixProject do
   end
 
   # Specifies which paths to compile per environment.
-  defp elixirc_paths(:test), do: ["lib", "test/support"]
-  defp elixirc_paths(_), do: ["lib"]
+  defp elixirc_paths(:test), do: ["lib", "config", "test/support"]
+  defp elixirc_paths(_), do: ["lib", "config"]
 
   # Specifies your project dependencies.
   #