diff --git a/lib/livebook.ex b/lib/livebook.ex index 4ec084171..bd4fbba3b 100644 --- a/lib/livebook.ex +++ b/lib/livebook.ex @@ -95,7 +95,8 @@ defmodule Livebook do end if ip = Livebook.Config.ip!("LIVEBOOK_IP") do - config :livebook, LivebookWeb.Endpoint, http: [ip: ip] + host = Livebook.Utils.ip_to_host(ip) + config :livebook, LivebookWeb.Endpoint, http: [ip: ip], url: [host: host] end cond do diff --git a/lib/livebook/utils.ex b/lib/livebook/utils.ex index 85d3ebe02..aff099ada 100644 --- a/lib/livebook/utils.ex +++ b/lib/livebook/utils.ex @@ -556,4 +556,27 @@ defmodule Livebook.Utils do _ -> default end end + + @doc """ + Converts the given IP address into a valid hostname. + + ## Examples + + iex> Livebook.Utils.ip_to_host({192, 168, 0, 1}) + "192.168.0.1" + + iex> Livebook.Utils.ip_to_host({127, 0, 0, 1}) + "localhost" + + iex> Livebook.Utils.ip_to_host({0, 0, 0, 0}) + "0.0.0.0" + """ + @spec ip_to_host(:inet.ip_address()) :: String.t() + def ip_to_host(ip) + + def ip_to_host({127, 0, 0, 1}), do: "localhost" + + def ip_to_host(ip) do + ip |> :inet.ntoa() |> List.to_string() + end end diff --git a/lib/livebook_cli/server.ex b/lib/livebook_cli/server.ex index 7617f3a23..e725a9916 100644 --- a/lib/livebook_cli/server.ex +++ b/lib/livebook_cli/server.ex @@ -241,7 +241,11 @@ defmodule LivebookCLI.Server do defp opts_to_config([{:ip, ip} | opts], config) do ip = Livebook.Config.ip!("--ip", ip) - opts_to_config(opts, [{:livebook, LivebookWeb.Endpoint, http: [ip: ip]} | config]) + host = Livebook.Utils.ip_to_host(ip) + + opts_to_config(opts, [ + {:livebook, LivebookWeb.Endpoint, http: [ip: ip], url: [host: host]} | config + ]) end defp opts_to_config([{:home, home} | opts], config) do