Prod.exs & EndPoint.init() : Is port defined twice?

Hello,

After generating a new app (phx.new 1.3), you will find:
In prod.exs

config :phx, PhxWeb.Endpoint,
  load_from_system_env: true,
  url: [host: "example.com", port: 80]

In Phx.Endpoint

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"
    {:ok, Keyword.put(config, :http, [:inet6, port: port])}
  else
    {:ok, config}
  end
end

Is there a difference between the port defined by url: [host: "example.com", port: 80] and the port defined by Keyword.put(config, :http, [:inet6, port: port])? What does it use for?

Thanks,
G.

Is there a difference between the port defined by url: [host: “example.com”, port: 80] and the port defined by Keyword.put(config, :http, [:inet6, port: port])? What does it use for?

Yes, url: [host: "example.com", port: 80] is for url generation (for example, inside your templates) and Keyword.put(config, :http, [:inet6, port: port]) is the actual port cowboy will bind to.

2 Likes