livebook/lib/livebook_app.ex

58 lines
1.2 KiB
Elixir
Raw Normal View History

2022-01-18 00:34:38 +08:00
if Mix.target() == :app do
defmodule LivebookApp do
use GenServer
def start_link(arg) do
2023-01-17 04:09:47 +08:00
GenServer.start_link(__MODULE__, arg, name: __MODULE__)
end
@impl true
def init(_) do
{:ok, pid} = ElixirKit.start()
ref = Process.monitor(pid)
ElixirKit.publish("url", LivebookWeb.Endpoint.access_url())
{:ok, %{ref: ref}}
end
@impl true
2023-01-17 04:09:47 +08:00
def handle_info({:event, "open", url}, state) do
open(url)
{:noreply, state}
end
@impl true
def handle_info({:DOWN, ref, :process, _, :shutdown}, state) when ref == state.ref do
Livebook.Config.shutdown()
{:noreply, state}
end
2023-01-17 04:09:47 +08:00
defp open("") do
open(LivebookWeb.Endpoint.access_url())
end
defp open("file://" <> path) do
path
|> Livebook.Utils.notebook_open_url()
2023-01-17 04:09:47 +08:00
|> open()
end
2023-01-17 04:09:47 +08:00
defp open("livebook://" <> rest) do
"https://#{rest}"
|> Livebook.Utils.notebook_import_url()
2023-01-17 04:09:47 +08:00
|> open()
end
defp open("/settings") do
%{LivebookWeb.Endpoint.access_struct_url() | path: "/settings"}
|> to_string()
|> open()
end
2023-01-17 04:09:47 +08:00
defp open(url) do
Livebook.Utils.browser_open(url)
end
end
2022-01-18 00:34:38 +08:00
end