From f4e836a5da3c0018c3a35741e2c75c9270fddf4a Mon Sep 17 00:00:00 2001 From: Jacques Lorentz <34100034+jacqueslorentz@users.noreply.github.com> Date: Tue, 29 Nov 2022 23:34:38 +0100 Subject: [PATCH] Base url path configuration variable (#1549) --- README.md | 3 +++ assets/js/app.js | 2 +- assets/js/hooks/js_view/channel.js | 2 +- assets/webpack.config.js | 2 +- config/config.exs | 2 +- lib/livebook.ex | 4 ++++ lib/livebook/config.ex | 17 +++++++++++++++++ lib/livebook_web/live/hub/new_live.ex | 2 +- lib/livebook_web/live/layout_helpers.ex | 2 +- lib/livebook_web/live/learn_helpers.ex | 2 +- lib/livebook_web/live/learn_live.ex | 4 ++-- lib/livebook_web/live/session_live.ex | 2 +- lib/livebook_web/plugs/auth_plug.ex | 4 +++- lib/livebook_web/templates/auth/index.html.eex | 4 ++-- lib/livebook_web/templates/error/404.html.eex | 7 ++++--- lib/livebook_web/templates/error/500.html.eex | 7 ++++--- .../templates/layout/root.html.heex | 13 ++++--------- 17 files changed, 51 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index 14e53c7e9..a068ff7be 100644 --- a/README.md +++ b/README.md @@ -190,6 +190,9 @@ The following environment variables configure Livebook: * LIVEBOOK_IP - sets the ip address to start the web application on. Must be a valid IPv4 or IPv6 address. + * LIVEBOOK_BASE_URL_PATH - sets the base url path the web application is served on. + Useful when deploying behind a reverse proxy. + * LIVEBOOK_PASSWORD - sets a password that must be used to access Livebook. Must be at least 12 characters. Defaults to token authentication. diff --git a/assets/js/app.js b/assets/js/app.js index 33f3edf48..6972704e9 100644 --- a/assets/js/app.js +++ b/assets/js/app.js @@ -21,7 +21,7 @@ const csrfToken = document .querySelector("meta[name='csrf-token']") .getAttribute("content"); -const liveSocket = new LiveSocket("/live", Socket, { +const liveSocket = new LiveSocket(window.LIVEBOOK_BASE_URL_PATH + "/live", Socket, { params: (liveViewName) => { return { _csrf_token: csrfToken, diff --git a/assets/js/hooks/js_view/channel.js b/assets/js/hooks/js_view/channel.js index d3b8ddd10..bd47d4d1a 100644 --- a/assets/js/hooks/js_view/channel.js +++ b/assets/js/hooks/js_view/channel.js @@ -4,7 +4,7 @@ const csrfToken = document .querySelector("meta[name='csrf-token']") .getAttribute("content"); -const socket = new Socket("/socket", { params: { _csrf_token: csrfToken } }); +const socket = new Socket(window.LIVEBOOK_BASE_URL_PATH + "/socket", { params: { _csrf_token: csrfToken } }); let channel = null; diff --git a/assets/webpack.config.js b/assets/webpack.config.js index 6edbe3c40..0c3916c3e 100644 --- a/assets/webpack.config.js +++ b/assets/webpack.config.js @@ -21,7 +21,7 @@ module.exports = (env, options) => { __dirname, devMode ? "../tmp/static_dev/js" : "../static/js" ), - publicPath: "/js/", + publicPath: 'auto', }, devtool: devMode ? "eval-cheap-module-source-map" : undefined, module: { diff --git a/config/config.exs b/config/config.exs index d5ae60743..236dd8f12 100644 --- a/config/config.exs +++ b/config/config.exs @@ -2,7 +2,7 @@ import Config # Configures the endpoint config :livebook, LivebookWeb.Endpoint, - url: [host: "localhost"], + url: [host: "localhost", path: "/"], pubsub_server: Livebook.PubSub, live_view: [signing_salt: "livebook"] diff --git a/lib/livebook.ex b/lib/livebook.ex index e2ada4000..2cfb02c8d 100644 --- a/lib/livebook.ex +++ b/lib/livebook.ex @@ -99,6 +99,10 @@ defmodule Livebook do config :livebook, LivebookWeb.Endpoint, http: [ip: ip], url: [host: host] end + if base_url_path = Livebook.Config.base_url_path!("LIVEBOOK_BASE_URL_PATH") do + config :livebook, LivebookWeb.Endpoint, url: [path: base_url_path] + end + cond do password = Livebook.Config.password!("LIVEBOOK_PASSWORD") -> config :livebook, authentication_mode: :password, password: password diff --git a/lib/livebook/config.ex b/lib/livebook/config.ex index 1e4f1edc2..81588b776 100644 --- a/lib/livebook/config.ex +++ b/lib/livebook/config.ex @@ -87,6 +87,14 @@ defmodule Livebook.Config do Application.get_env(:livebook, LivebookWeb.Endpoint)[:http][:port] end + @doc """ + Returns the base url path for the Livebook endpoint. + """ + @spec base_url_path() :: String.t() + def base_url_path() do + Application.get_env(:livebook, LivebookWeb.Endpoint)[:url][:path] + end + @doc """ Returns the configured port for the iframe endpoint. """ @@ -228,6 +236,15 @@ defmodule Livebook.Config do end end + @doc """ + Parses and validates the base url path from env. + """ + def base_url_path!(env) do + if base_url_path = System.get_env(env) do + String.trim_trailing(base_url_path, "/") + end + end + @doc """ Parses and validates the ip from env. """ diff --git a/lib/livebook_web/live/hub/new_live.ex b/lib/livebook_web/live/hub/new_live.ex index 329801b21..4453a2a02 100644 --- a/lib/livebook_web/live/hub/new_live.ex +++ b/lib/livebook_web/live/hub/new_live.ex @@ -49,7 +49,7 @@ defmodule LivebookWeb.Hub.NewLive do <.card_item id="enterprise" selected={@selected_type} title="Livebook Enterprise"> <:logo> Livebook Enterprise logo diff --git a/lib/livebook_web/live/layout_helpers.ex b/lib/livebook_web/live/layout_helpers.ex index d6c7d40f6..feab54aeb 100644 --- a/lib/livebook_web/live/layout_helpers.ex +++ b/lib/livebook_web/live/layout_helpers.ex @@ -82,7 +82,7 @@ defmodule LivebookWeb.LayoutHelpers do
<%= live_redirect to: Routes.home_path(@socket, :page), class: "flex items-center border-l-4 border-gray-900 group" do %> - logo livebook + logo livebook diff --git a/lib/livebook_web/live/learn_helpers.ex b/lib/livebook_web/live/learn_helpers.ex index a7b1f2b28..2214e9be7 100644 --- a/lib/livebook_web/live/learn_helpers.ex +++ b/lib/livebook_web/live/learn_helpers.ex @@ -12,7 +12,7 @@ defmodule LivebookWeb.LearnHelpers do class: "flex flex-col border-2 border-gray-100 hover:border-gray-200 rounded-2xl" do %>
{"#{@notebook_info.title} diff --git a/lib/livebook_web/live/learn_live.ex b/lib/livebook_web/live/learn_live.ex index 27c038dcb..a602fdfc3 100644 --- a/lib/livebook_web/live/learn_live.ex +++ b/lib/livebook_web/live/learn_live.ex @@ -41,7 +41,7 @@ defmodule LivebookWeb.LearnLive do id="welcome-to-livebook" class="p-8 bg-gray-900 rounded-2xl flex flex-col sm:flex-row space-y-8 sm:space-y-0 space-x-0 sm:space-x-8 items-center" > - livebook + livebook

<%= @lead_notebook_info.title %> @@ -76,7 +76,7 @@ defmodule LivebookWeb.LearnLive do ~H"""
- +
<%= length(@group_info.notebook_infos) %> notebooks diff --git a/lib/livebook_web/live/session_live.ex b/lib/livebook_web/live/session_live.ex index 4aa2a8fe8..5b9e2c4e7 100644 --- a/lib/livebook_web/live/session_live.ex +++ b/lib/livebook_web/live/session_live.ex @@ -109,7 +109,7 @@ defmodule LivebookWeb.SessionLive do > <%= live_redirect to: Routes.home_path(@socket, :page), aria_label: "go to homepage" do %> - + <% end %> diff --git a/lib/livebook_web/plugs/auth_plug.ex b/lib/livebook_web/plugs/auth_plug.ex index 0a0c1ff66..92e116018 100644 --- a/lib/livebook_web/plugs/auth_plug.ex +++ b/lib/livebook_web/plugs/auth_plug.ex @@ -6,6 +6,8 @@ defmodule LivebookWeb.AuthPlug do import Plug.Conn import Phoenix.Controller + alias LivebookWeb.Router.Helpers, as: Routes + @impl true def init(opts), do: opts @@ -76,7 +78,7 @@ defmodule LivebookWeb.AuthPlug do %{method: "GET"} -> put_session(conn, :redirect_to, current_path(conn)) conn -> conn end) - |> redirect(to: "/authenticate") + |> redirect(to: Routes.path(conn, "/authenticate")) |> halt() end diff --git a/lib/livebook_web/templates/auth/index.html.eex b/lib/livebook_web/templates/auth/index.html.eex index cdcfc8c52..33f695ff8 100644 --- a/lib/livebook_web/templates/auth/index.html.eex +++ b/lib/livebook_web/templates/auth/index.html.eex @@ -1,7 +1,7 @@
- - livebook + "> + " height="128" width="128" alt="livebook" />
Authentication required diff --git a/lib/livebook_web/templates/error/404.html.eex b/lib/livebook_web/templates/error/404.html.eex index bbba60a28..eb9e45354 100644 --- a/lib/livebook_web/templates/error/404.html.eex +++ b/lib/livebook_web/templates/error/404.html.eex @@ -4,15 +4,16 @@ - + " /> + " /> <%= @status %> - Livebook "/>
- - livebook + "> + " height="128" width="128" alt="livebook" />
No Numbats here! diff --git a/lib/livebook_web/templates/error/500.html.eex b/lib/livebook_web/templates/error/500.html.eex index e7d69c5c3..09077c915 100644 --- a/lib/livebook_web/templates/error/500.html.eex +++ b/lib/livebook_web/templates/error/500.html.eex @@ -4,15 +4,16 @@ - + " /> + " /> <%= @status %> - Livebook "/>
- - livebook + "> + " height="128" width="128" alt="livebook" />
Something went wrong. diff --git a/lib/livebook_web/templates/layout/root.html.heex b/lib/livebook_web/templates/layout/root.html.heex index d6429de4a..1552f932f 100644 --- a/lib/livebook_web/templates/layout/root.html.heex +++ b/lib/livebook_web/templates/layout/root.html.heex @@ -5,17 +5,12 @@ - - + + <%= live_title_tag(assigns[:page_title] || "Livebook") %> - + + <%= @inner_content %>