mirror of
https://github.com/livebook-dev/livebook.git
synced 2024-09-20 10:05:57 +08:00
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:
parent
49492771f3
commit
36e48f861e
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue