Use IP address as hostname when configured (#1181)

* Use IP address as hostname when configured

* Use localhost for access URL hostname when applicable

* Keep 0.0.0.0 as is for hostname
This commit is contained in:
Jonatan Kłosko 2022-05-13 15:22:41 +02:00 committed by GitHub
parent 49492771f3
commit 36e48f861e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 2 deletions

View file

@ -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

View file

@ -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

View file

@ -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