Skip to content

Commit

Permalink
Add c:Phoenix.Endpoint.server_info/1 (#5620)
Browse files Browse the repository at this point in the history
  • Loading branch information
mtrudel committed Nov 2, 2023
1 parent bd63cf3 commit 932d2e5
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 2 deletions.
17 changes: 17 additions & 0 deletions lib/phoenix/endpoint.ex
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,9 @@ defmodule Phoenix.Endpoint do
* for handling paths and URLs: `c:struct_url/0`, `c:url/0`, `c:path/1`,
`c:static_url/0`,`c:static_path/1`, and `c:static_integrity/1`
* for gethering runtime information about the address and port the
endpoint is running on: `c:server_info/1`
* for broadcasting to channels: `c:broadcast/3`, `c:broadcast!/3`,
`c:broadcast_from/4`, `c:broadcast_from!/4`, `c:local_broadcast/3`,
and `c:local_broadcast_from/4`
Expand Down Expand Up @@ -342,6 +345,15 @@ defmodule Phoenix.Endpoint do
"""
@callback host() :: String.t()

# Server information

@doc """
Returns the address and port that the server is running on
"""
@callback server_info(Plug.Conn.scheme()) ::
{:ok, {:inet.ip_address(), :inet.port_number()} | :inet.returned_non_ip_address()}
| {:error, term()}

# Channels

@doc """
Expand Down Expand Up @@ -605,6 +617,11 @@ defmodule Phoenix.Endpoint do
&Phoenix.Endpoint.Supervisor.static_lookup(&1, path)
)
end

@doc """
Returns the address and port that the server is running on
"""
def server_info(scheme), do: config(:adapter).server_info(__MODULE__, scheme)
end
end

Expand Down
14 changes: 13 additions & 1 deletion lib/phoenix/endpoint/cowboy2_adapter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ defmodule Phoenix.Endpoint.Cowboy2Adapter do
Application.ensure_all_started(:ssl)
end

ref = Module.concat(endpoint, scheme |> Atom.to_string() |> String.upcase())
ref = make_ref(endpoint, scheme)

plug =
if code_reloader? do
Expand Down Expand Up @@ -137,4 +137,16 @@ defmodule Phoenix.Endpoint.Cowboy2Adapter do
defp port_to_integer({:system, env_var}), do: port_to_integer(System.get_env(env_var))
defp port_to_integer(port) when is_binary(port), do: String.to_integer(port)
defp port_to_integer(port) when is_integer(port), do: port

def server_info(endpoint, scheme) do
make_ref(endpoint, scheme)
|> :ranch.get_addr()
|> then(&{:ok, &1})
rescue
e -> {:error, e.message}
end

defp make_ref(endpoint, scheme) do
Module.concat(endpoint, scheme |> Atom.to_string() |> String.upcase())
end
end
3 changes: 2 additions & 1 deletion lib/phoenix/endpoint/supervisor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ defmodule Phoenix.Endpoint.Supervisor do
defp server_children(mod, config, server?) do
cond do
server? ->
adapter = config[:adapter] || Phoenix.Endpoint.Cowboy2Adapter
adapter = config[:adapter]
adapter.child_specs(mod, config)

config[:http] || config[:https] ->
Expand Down Expand Up @@ -196,6 +196,7 @@ defmodule Phoenix.Endpoint.Supervisor do
render_errors: [view: render_errors(module), accepts: ~w(html), layout: false],

# Runtime config
adapter: Phoenix.Endpoint.Cowboy2Adapter,
cache_static_manifest: nil,
check_origin: true,
http: false,
Expand Down
16 changes: 16 additions & 0 deletions test/phoenix/endpoint/endpoint_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,22 @@ defmodule Phoenix.Endpoint.EndpointTest do
assert StaticEndpoint.static_path("/phoenix.png") =~ "/static/phoenix.png"
end

@tag :capture_log
test "can find the running address and port for an endpoint" do
Application.put_env(:phoenix, __MODULE__.AddressEndpoint,
http: [ip: {127, 0, 0, 1}, port: 0],
server: true
)

defmodule AddressEndpoint do
use Phoenix.Endpoint, otp_app: :phoenix
end

AddressEndpoint.start_link()
assert {:ok, {{127, 0, 0, 1}, port}} = AddressEndpoint.server_info(:http)
assert is_integer(port)
end

test "injects pubsub broadcast with configured server" do
Endpoint.subscribe("sometopic")
some = spawn fn -> :ok end
Expand Down

0 comments on commit 932d2e5

Please sign in to comment.