Skip to content

Commit

Permalink
allow public routes to be served from a different base url path
Browse files Browse the repository at this point in the history
  • Loading branch information
elepedus committed Jul 12, 2024
1 parent b7ca4d6 commit 4f7a881
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 6 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,10 @@ The following environment variables can be used to configure Livebook on boot:
* `LIVEBOOK_BASE_URL_PATH` - sets the base url path the web application is
served on. Useful when deploying behind a reverse proxy.

* `LIVEBOOK_PUBLIC_BASE_URL_PATH_OVERRIDE` - overrides the base url path for the public
asset routes only. Useful to create exceptions when deploying behind a reverse proxy
that requires authentication.

* `LIVEBOOK_CACERTFILE` - path to a local file containing CA certificates.
Those certificates are used during for server authentication when Livebook
accesses files from external sources.
Expand Down
2 changes: 1 addition & 1 deletion assets/js/hooks/js_view.js
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ const JSView = {
* once and the response is cached.
*/
function cachedPublicEndpointCheck() {
const healthUrl = window.LIVEBOOK_BASE_URL_PATH + "/public/health";
const healthUrl = window.LIVEBOOK_PUBLIC_BASE_URL_PATH + "/public/health";

cachedPublicEndpointCheck.promise =
cachedPublicEndpointCheck.promise ||
Expand Down
5 changes: 5 additions & 0 deletions lib/livebook.ex
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,11 @@ defmodule Livebook do
config :livebook, LivebookWeb.Endpoint, url: [path: base_url_path]
end

if public_base_url_path_override =
Livebook.Config.public_base_url_path_override!("LIVEBOOK_PUBLIC_BASE_URL_PATH_OVERRIDE") do
config :livebook, :public_base_url_path_override, public_base_url_path_override
end

if password = Livebook.Config.password!("LIVEBOOK_PASSWORD") do
config :livebook, :authentication, {:password, password}
else
Expand Down
20 changes: 20 additions & 0 deletions lib/livebook/config.ex
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,17 @@ defmodule Livebook.Config do
String.trim_trailing(path, "/")
end

@doc """
Returns the base url path for Livebook public endpoints (health & assets)
"""
@spec public_base_url_path() :: String.t()
def public_base_url_path() do
case Application.get_env(:livebook, :public_base_url_path_override) do
nil -> base_url_path()
path -> String.trim_trailing(path, "/")
end
end

@doc """
Returns the configured port for the iframe endpoint.
"""
Expand Down Expand Up @@ -518,6 +529,15 @@ defmodule Livebook.Config do
end
end

@doc """
Parses and validates the public base url path override from env
"""
def public_base_url_path_override!(env) do
if override = System.get_env(env) do
String.trim_trailing(override, "/")
end
end

@doc """
Parses and validates the ip from env.
"""
Expand Down
1 change: 1 addition & 0 deletions lib/livebook_web/components/layouts/root.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<link phx-track-static rel="stylesheet" href={~p"/assets/app.css"} />
<script>
window.LIVEBOOK_BASE_URL_PATH = "<%= Livebook.Config.base_url_path() %>";
window.LIVEBOOK_PUBLIC_BASE_URL_PATH = "<%= Livebook.Config.public_base_url_path() %>"
</script>
<LivebookWeb.LayoutComponents.dev_script />
<%!-- This prevents the script to be loaded twice in Chrome --%>
Expand Down
9 changes: 8 additions & 1 deletion lib/livebook_web/controllers/session_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,14 @@ defmodule LivebookWeb.SessionController do
conn
|> cache_permanently()
|> put_status(:moved_permanently)
|> redirect(to: ~p"/public/sessions/node/#{node_id}/assets/#{hash}/#{file_parts}")
|> redirect(
to:
~p"/public/sessions/node/#{node_id}/assets/#{hash}/#{file_parts}"
|> String.replace(
Livebook.Config.base_url_path(),
Livebook.Config.public_base_url_path()
)
)
else
send_resp(conn, 404, "Not found")
end
Expand Down
11 changes: 8 additions & 3 deletions lib/livebook_web/live/js_view_component.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,20 @@ defmodule LivebookWeb.JSViewComponent do

@impl true
def render(assigns) do
assets_base_path =
~p"/public/sessions/#{assigns.session_id}/assets/#{assigns.js_view.assets.hash}/"
|> String.replace(Livebook.Config.base_url_path(), Livebook.Config.public_base_url_path())

assigns =
assign(assigns, :js_view, Map.put(assigns.js_view, :assets_base_path, assets_base_path))

~H"""
<div
id={"js-output-#{@id}-#{@js_view.ref}"}
phx-hook="JSView"
phx-update="ignore"
data-p-ref={hook_prop(@js_view.ref)}
data-p-assets-base-path={
hook_prop(~p"/public/sessions/#{@session_id}/assets/#{@js_view.assets.hash}/")
}
data-p-assets-base-path={hook_prop(@js_view.assets_base_path)}
data-p-assets-cdn-url={hook_prop(cdn_url(@js_view.assets[:cdn_url]))}
data-p-js-path={hook_prop(@js_view.assets.js_path)}
data-p-session-token={hook_prop(session_token(@session_id, @client_id))}
Expand Down
2 changes: 1 addition & 1 deletion static/assets/app.js

Large diffs are not rendered by default.

0 comments on commit 4f7a881

Please sign in to comment.