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