Загрузка конфигурации конечной точки из системной среды - PullRequest
0 голосов
/ 25 сентября 2018

У меня есть следующая инициализация конечной точки в lib/flashcards_web/endpoint.ex:

  @doc """
  Callback invoked for dynamically configuring the endpoint.

  It receives the endpoint configuration and checks if
  configuration should be loaded from the system environment.
  """
  def init(_key, config) do
    if config[:load_from_system_env] do
      port = System.get_env("PORT") || raise "expected the PORT environment variable to be set"                                                      
      jwt_token_ttl_minutes =
        "USER_SESSION_MINUTES"
        |> System.get_env
        |> String.to_integer
      || raise "expected the USER_SESSION_MINUTES environment variable to be set"                                                                    

      config =
        config
        |> Keyword.put(:http, [:inet6, port: port])
        |> Keyword.put(:jwt_token_ttl_minutes, jwt_token_ttl_minutes)

      {:ok, config}
    else
      {:ok, config}
    end
  end

и требуемая строка load_from_system_env: true в config/dev.exs:

# For development, we disable any cache and enable
# debugging and code reloading.
#
# The watchers configuration can be used to run external
# watchers to your application. For example, we use it
# with brunch.io to recompile .js and .css sources.
config :flashcards, FlashcardsWeb.Endpoint,
  http: [port: 4000],
  debug_errors: true,
  code_reloader: true,
  check_origin: false,
  watchers: [node: ["node_modules/brunch/bin/brunch", "watch", "--stdin",
                    cd: Path.expand("../assets", __DIR__)]],
  load_from_system_env: true

Однако при работе

PORT=4000 USER_SESSION_MINUTES=1 iex -S mix phx.server

Я получаю:

iex(1)> Application.get_env(:flashcards, FlashcardsWeb.Endpoint)[:jwt_token_ttl_minutes]
nil

Я что-то здесь упускаю?

1 Ответ

0 голосов
/ 26 сентября 2018

Нашли решение для доступа к динамической конфигурации конечной точки.

В документах упоминается, что функция config/2 автоматически генерируется в конечной точке .

Динамическая конечная точкаПоэтому к конфигурации можно получить доступ следующим образом:

iex(2)> FlashcardsWeb.Endpoint.config(:jwt_token_ttl_minutes)
1
...