Skip to content

Commit

Permalink
Integrate with new server_info callback on endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
LostKobrakai authored Dec 30, 2023
1 parent 5176b51 commit a1ed375
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions lib/desktop/endpoint.ex
Original file line number Diff line number Diff line change
@@ -1,14 +1,33 @@
defmodule Desktop.Endpoint do
@doc false
defmacro __using__(opts) do
scheme = Keyword.get(opts, :desktop_scheme, :http)

quote do
use Phoenix.Endpoint, unquote(opts)
defoverridable url: 0

def url do
url = super()
endpoint = Module.safe_concat(__MODULE__, HTTP)
String.replace(url, ":0", ":#{:ranch.get_port(endpoint)}")
scheme = unquote(scheme)

case Keyword.get(config(scheme), :port, 0) do
0 -> String.replace(url, ":0", ":#{get_dynamic_port(scheme)}")
port -> url
end
end

if Version.match?(:phoenix |> Application.spec(:vsn) |> List.to_string(), "~> 1.7.10") do
def get_dynamic_port(scheme) do
{:ok, {_ip, port}} = server_info(scheme)
port
end
else
# Supports only cowboy adapter for phoenix
def get_dynamic_port(scheme) do
ref = Module.safe_concat(__MODULE__, scheme |> Atom.to_string() |> String.upcase())
:ranch.get_port(ref)
end
end
end
end
Expand Down

0 comments on commit a1ed375

Please sign in to comment.