Improve ip server booting (#1173)

* Bind to the same ip as the main application

* Improve error message if port is already taken
This commit is contained in:
José Valim 2022-05-06 10:27:48 +02:00 committed by GitHub
parent 9b14d06123
commit 7ef0de2722
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -199,10 +199,38 @@ defmodule Livebook.Application do
port = Livebook.Config.iframe_port()
if server? do
# Start the iframe endpoint on a different port
[{Plug.Cowboy, scheme: :http, plug: LivebookWeb.IframeEndpoint, options: [port: port]}]
iframe_opts = [
scheme: :http,
plug: LivebookWeb.IframeEndpoint,
options: [port: port],
ip: Application.fetch_env!(:livebook, LivebookWeb.Endpoint)[:http][:ip]
]
spec = Plug.Cowboy.child_spec(iframe_opts)
spec = update_in(spec.start, &{__MODULE__, :start_iframe, [port, &1]})
[spec]
else
[]
end
end
@doc false
def start_iframe(port, {m, f, a}) do
require Logger
case apply(m, f, a) do
{:ok, pid} ->
{:ok, pid}
{:error, {:shutdown, {_, _, {:listen_error, _, :eaddrinuse}}}} = error ->
Logger.error(
"Failed to start Livebook iframe server because port #{port} is already in use"
)
error
{:error, _} = error ->
error
end
end
end