Base url path configuration variable (#1549)

This commit is contained in:
Jacques Lorentz 2022-11-29 23:34:38 +01:00 committed by GitHub
parent 1c8a0c72b5
commit f4e836a5da
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
17 changed files with 51 additions and 28 deletions

View file

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

View file

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

View file

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

View file

@ -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: {

View file

@ -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"]

View file

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

View file

@ -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.
"""

View file

@ -49,7 +49,7 @@ defmodule LivebookWeb.Hub.NewLive do
<.card_item id="enterprise" selected={@selected_type} title="Livebook Enterprise">
<:logo>
<img
src="/images/enterprise.png"
src={Routes.static_path(@socket, "/images/enterprise.png")}
class="max-h-full max-w-[75%]"
alt="Livebook Enterprise logo"
/>

View file

@ -82,7 +82,7 @@ defmodule LivebookWeb.LayoutHelpers do
<div class="space-y-3">
<div class="flex items-center mb-5">
<%= live_redirect to: Routes.home_path(@socket, :page), class: "flex items-center border-l-4 border-gray-900 group" do %>
<img src="/images/logo.png" class="mx-2" height="40" width="40" alt="logo livebook" />
<img src={Routes.static_path(@socket, "/images/logo.png")} class="mx-2" height="40" width="40" alt="logo livebook" />
<span class="text-gray-300 text-2xl font-logo ml-[-1px] group-hover:text-white pt-1">
Livebook
</span>

View file

@ -12,7 +12,7 @@ defmodule LivebookWeb.LearnHelpers do
class: "flex flex-col border-2 border-gray-100 hover:border-gray-200 rounded-2xl" do %>
<div class="flex items-center justify-center p-6 border-b-2 border-gray-100 rounded-t-2xl h-[150px]">
<img
src={@notebook_info.details.cover_url}
src={Routes.static_path(@socket, @notebook_info.details.cover_url)}
class="max-h-full max-w-[75%]"
alt={"#{@notebook_info.title} logo"}
/>

View file

@ -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"
>
<img src={@lead_notebook_info.details.cover_url} width="100" alt="livebook" />
<img src={Routes.static_path(@socket, @lead_notebook_info.details.cover_url)} width="100" alt="livebook" />
<div>
<h3 class="text-xl text-gray-50 font-semibold">
<%= @lead_notebook_info.title %>
@ -76,7 +76,7 @@ defmodule LivebookWeb.LearnLive do
~H"""
<div>
<div class="p-8 mt-16 rounded-2xl border border-gray-300 flex flex-col sm:flex-row space-y-8 sm:space-y-0 space-x-0 sm:space-x-8 items-center">
<img src={@group_info.cover_url} width="100" />
<img src={Routes.static_path(@socket, @group_info.cover_url)} width="100" />
<div>
<div class="inline-flex px-2 py-0.5 bg-gray-200 rounded-3xl text-gray-700 text-xs font-medium">
<%= length(@group_info.notebook_infos) %> notebooks

View file

@ -109,7 +109,7 @@ defmodule LivebookWeb.SessionLive do
>
<span>
<%= live_redirect to: Routes.home_path(@socket, :page), aria_label: "go to homepage" do %>
<img src="/images/logo.png" height="40" width="40" alt="" />
<img src={Routes.static_path(@socket, "/images/logo.png")} height="40" width="40" alt="" />
<% end %>
</span>

View file

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

View file

@ -1,7 +1,7 @@
<div class="h-screen flex items-center justify-center bg-gray-900">
<div class="flex flex-col space-y-4 items-center">
<a href="/">
<img src="/images/logo.png" height="128" width="128" alt="livebook" />
<a href="<%= Routes.path(@conn, "/") %>">
<img src="<%= Routes.static_path(@conn, "/images/logo.png") %>" height="128" width="128" alt="livebook" />
</a>
<div class="text-2xl text-gray-50">
Authentication required

View file

@ -4,15 +4,16 @@
<meta charset="utf-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<link rel="icon" type="image/svg+xml" href="/favicon.svg" />
<link rel="icon" type="image/svg+xml" href="<%= Routes.static_path(@conn, "/favicon.svg") %>" />
<link rel="alternate icon" type="image/png" href="<%= Routes.static_path(@conn, "/favicon.png") %>" />
<title><%= @status %> - Livebook</title>
<link rel="stylesheet" href="<%= Routes.static_path(@conn, "/css/app.css") %>"/>
</head>
<body>
<div class="h-screen flex items-center justify-center bg-gray-900">
<div class="flex flex-col space-y-4 items-center">
<a href="/">
<img src="/images/logo.png" height="128" width="128" alt="livebook" />
<a href="<%= Routes.path(@conn, "/") %>">
<img src="<%= Routes.static_path(@conn, "/images/logo.png") %>" height="128" width="128" alt="livebook" />
</a>
<div class="text-2xl text-gray-50">
No Numbats here!

View file

@ -4,15 +4,16 @@
<meta charset="utf-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<link rel="icon" type="image/svg+xml" href="/favicon.svg" />
<link rel="icon" type="image/svg+xml" href="<%= Routes.static_path(@conn, "/favicon.svg") %>" />
<link rel="alternate icon" type="image/png" href="<%= Routes.static_path(@conn, "/favicon.png") %>" />
<title><%= @status %> - Livebook</title>
<link rel="stylesheet" href="<%= Routes.static_path(@conn, "/css/app.css") %>"/>
</head>
<body>
<div class="h-screen flex items-center justify-center bg-gray-900">
<div class="flex flex-col space-y-4 items-center">
<a href="/">
<img src="/images/logo.png" height="128" width="128" alt="livebook" />
<a href="<%= Routes.path(@conn, "/") %>">
<img src="<%= Routes.static_path(@conn, "/images/logo.png") %>" height="128" width="128" alt="livebook" />
</a>
<div class="text-2xl text-gray-50">
Something went wrong.

View file

@ -5,17 +5,12 @@
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="csrf-token" content={get_csrf_token()} />
<link rel="icon" type="image/svg+xml" href="/favicon.svg" />
<link rel="alternate icon" type="image/png" href="/favicon.png" />
<link rel="icon" type="image/svg+xml" href={Routes.static_path(@conn, "/favicon.svg")} />
<link rel="alternate icon" type="image/png" href={Routes.static_path(@conn, "/favicon.png")} />
<%= live_title_tag(assigns[:page_title] || "Livebook") %>
<link phx-track-static rel="stylesheet" href={Routes.static_path(@conn, "/css/app.css")} />
<script
defer
phx-track-static
type="text/javascript"
src={Routes.static_path(@conn, "/js/app.js")}
>
</script>
<script>window.LIVEBOOK_BASE_URL_PATH = "<%= Livebook.Config.base_url_path() %>";</script>
<script phx-track-static defer type="text/javascript" src={Routes.static_path(@conn, "/js/app.js")}></script>
</head>
<body class="bg-white">
<%= @inner_content %>