Kaynağa Gözat

Convert project to "modern" config management

theenglishway (time) 4 yıl önce
ebeveyn
işleme
8bb5cf6430
9 değiştirilmiş dosya ile 62 ekleme ve 64 silme
  1. 2 2
      config/config.exs
  2. 1 1
      config/dev.exs
  3. 23 0
      config/helper.ex
  4. 1 5
      config/prod.exs
  5. 0 25
      config/prod.secret.exs
  6. 0 28
      config/releases.exs
  7. 32 0
      config/runtime.exs
  8. 1 1
      config/test.exs
  9. 2 2
      mix.exs

+ 2 - 2
config/config.exs

@@ -5,7 +5,7 @@
 # is restricted to this project.
 
 # General application configuration
-use Mix.Config
+import Config
 
 # Configures the endpoint
 config :vaccins, VaccinsWeb.Endpoint,
@@ -25,4 +25,4 @@ config :phoenix, :json_library, Jason
 
 # Import environment specific config. This must remain at the bottom
 # of this file so it overrides the configuration defined above.
-import_config "#{Mix.env()}.exs"
+import_config "#{config_env()}.exs"

+ 1 - 1
config/dev.exs

@@ -1,4 +1,4 @@
-use Mix.Config
+import Config
 
 # For development, we disable any cache and enable
 # debugging and code reloading.

+ 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

+ 1 - 5
config/prod.exs

@@ -1,4 +1,4 @@
-use Mix.Config
+import Config
 
 # For production, don't forget to configure the url host
 # to something meaningful, Phoenix uses this information
@@ -47,7 +47,3 @@ config :logger, level: :info
 #       force_ssl: [hsts: true]
 #
 # Check `Plug.SSL` for all available options in `force_ssl`.
-
-# Finally import the config/prod.secret.exs which loads secrets
-# and configuration from environment variables.
-import_config "prod.secret.exs"

+ 0 - 25
config/prod.secret.exs

@@ -1,25 +0,0 @@
-# In this file, we load production configuration and secrets
-# from environment variables. You can also hardcode secrets,
-# although such is generally not recommended and you have to
-# remember to add this file to your .gitignore.
-use Mix.Config
-
-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
-    """
-
-config :vaccins, VaccinsWeb.Endpoint, secret_key_base: secret_key_base
-
-# ## Using releases (Elixir v1.9+)
-#
-# If you are doing OTP releases, you need to instruct Phoenix
-# to start each relevant endpoint:
-#
-#     config :vaccins, VaccinsWeb.Endpoint, server: true
-#
-# Then you can assemble a release by calling `mix release`.
-# See `mix help release` for more information.
-config :vaccins, VaccinsWeb.Endpoint, server: true

+ 0 - 28
config/releases.exs

@@ -1,28 +0,0 @@
-# In this file, we load production configuration and secrets
-# from environment variables. You can also hardcode secrets,
-# although such is generally not recommended and you have to
-# remember to add this file to your .gitignore.
-import Config
-
-host_ip =
-  System.get_env("HOST_IP") ||
-    raise "environment variable HOST_IP is missing."
-
-port =
-  System.get_env("PORT") ||
-    raise "environment variable PORT is missing."
-
-config :vaccins, VaccinsWeb.Endpoint,
-  url: [host: host_ip, port: port],
-  http: [
-    port: String.to_integer(port),
-    net: :inet,
-    transport_options: [socket_opts: [:inet6]]
-  ],
-  check_origin: [
-    "http://localhost:#{port}",
-    "http://192.168.0.13:#{port}",
-    "http://#{host_ip}:#{port}"
-  ]
-
-config :vaccins, VaccinsWeb.Endpoint, server: true

+ 32 - 0
config/runtime.exs

@@ -0,0 +1,32 @@
+import Config
+alias Config.Helper
+
+# config/runtime.exs is executed for all environments, including
+# during releases. It is executed after compilation and before the
+# system starts, so it typically used load production configuration
+# and secrets from environment variables or elsewhere. Do not define
+# 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
+  env =
+    Helper.load_all([
+      "SECRET_KEY_BASE",
+      "PORT",
+      "HOST_IP"
+    ])
+
+  config :vaccins, VaccinsWeb.Endpoint,
+    secret_key_base: env.secret_key_base,
+    url: [host: env.host_ip, port: env.port],
+    http: [
+      port: String.to_integer(env.port),
+      net: :inet,
+      transport_options: [socket_opts: [:inet6]]
+    ],
+    check_origin: [
+      "http://localhost:#{env.port}",
+      "http://192.168.0.13:#{env.port}",
+      "http://#{env.host_ip}:#{env.port}"
+    ],
+    server: true
+end

+ 1 - 1
config/test.exs

@@ -1,4 +1,4 @@
-use Mix.Config
+import Config
 
 # We don't run a server during test. If one is required,
 # you can enable the server option below.

+ 2 - 2
mix.exs

@@ -25,8 +25,8 @@ defmodule Vaccins.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: ["config", "lib", "test/support"]
+  defp elixirc_paths(_), do: ["config", "lib"]
 
   # Specifies your project dependencies.
   #