代码之家  ›  专栏  ›  技术社区  ›  category

从系统环境加载端点配置

  •  0
  • category  · 技术社区  · 6 年前

    我在中进行了以下端点初始化 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 回复  |  直到 6 年前
        1
  •  1
  •   category    6 年前

    找到访问动态终结点配置的解决方案。

    医生说 config/2 function is automatically generated at the endpoint .

    因此,可以按如下方式访问动态端点配置:

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