2021-03-04 05:56:28 +08:00
|
|
|
defmodule LivebookWeb.Endpoint do
|
|
|
|
use Phoenix.Endpoint, otp_app: :livebook
|
2021-01-08 03:55:45 +08:00
|
|
|
|
|
|
|
# The session will be stored in the cookie and signed,
|
|
|
|
# this means its contents can be read but not tampered with.
|
|
|
|
# Set :encryption_salt if you would also like to encrypt it.
|
|
|
|
@session_options [
|
|
|
|
store: :cookie,
|
2021-03-04 05:56:28 +08:00
|
|
|
key: "_livebook_key",
|
2021-12-30 05:06:19 +08:00
|
|
|
signing_salt: "deadbook",
|
|
|
|
same_site: "Lax"
|
2021-01-08 03:55:45 +08:00
|
|
|
]
|
|
|
|
|
2022-01-06 23:31:26 +08:00
|
|
|
socket "/live", LivebookWeb.Socket,
|
2021-03-17 18:16:40 +08:00
|
|
|
# Don't check the origin as we don't know how the web app is gonna be accessed.
|
|
|
|
# It runs locally, but may be exposed via IP or domain name.
|
|
|
|
# The WebSocket connection is already protected from CSWSH by using CSRF token.
|
2022-01-06 23:31:26 +08:00
|
|
|
websocket: [check_origin: false, connect_info: [:user_agent, :uri, session: @session_options]]
|
2021-01-08 04:16:54 +08:00
|
|
|
|
2021-10-19 04:32:09 +08:00
|
|
|
# We use Escript for distributing Livebook, so we don't have access to the static
|
|
|
|
# files at runtime in the prod environment. To overcome this we load contents of
|
|
|
|
# those files at compilation time, so that they become a part of the executable
|
|
|
|
# and can be served from memory.
|
2021-03-17 08:53:44 +08:00
|
|
|
defmodule AssetsMemoryProvider do
|
|
|
|
use LivebookWeb.MemoryProvider,
|
2021-10-19 04:32:09 +08:00
|
|
|
from: Path.expand("../../static", __DIR__),
|
2021-03-17 08:53:44 +08:00
|
|
|
gzip: true
|
|
|
|
end
|
|
|
|
|
|
|
|
defmodule AssetsFileSystemProvider do
|
|
|
|
use LivebookWeb.FileSystemProvider,
|
2021-05-20 18:27:30 +08:00
|
|
|
from: "tmp/static_dev"
|
2021-03-17 08:53:44 +08:00
|
|
|
end
|
|
|
|
|
2021-10-31 14:14:35 +08:00
|
|
|
# Serve static files at "/"
|
2021-03-24 00:46:33 +08:00
|
|
|
|
|
|
|
if code_reloading? do
|
2021-05-20 05:50:18 +08:00
|
|
|
# In development we use assets from tmp/static_dev (rebuilt dynamically on every change).
|
2021-10-19 04:32:09 +08:00
|
|
|
# Note that this directory doesn't contain predefined files (e.g. images), so we also
|
|
|
|
# use `AssetsMemoryProvider` to serve those from static/.
|
2021-03-24 00:46:33 +08:00
|
|
|
plug LivebookWeb.StaticPlug,
|
|
|
|
at: "/",
|
|
|
|
file_provider: AssetsFileSystemProvider,
|
|
|
|
gzip: false
|
|
|
|
end
|
|
|
|
|
2021-03-17 08:53:44 +08:00
|
|
|
plug LivebookWeb.StaticPlug,
|
2021-01-08 03:55:45 +08:00
|
|
|
at: "/",
|
2021-03-24 00:46:33 +08:00
|
|
|
file_provider: AssetsMemoryProvider,
|
2021-03-17 08:53:44 +08:00
|
|
|
gzip: true
|
2021-01-08 03:55:45 +08:00
|
|
|
|
|
|
|
# Code reloading can be explicitly enabled under the
|
|
|
|
# :code_reloader configuration of your endpoint.
|
|
|
|
if code_reloading? do
|
|
|
|
socket "/phoenix/live_reload/socket", Phoenix.LiveReloader.Socket
|
|
|
|
plug Phoenix.LiveReloader
|
|
|
|
plug Phoenix.CodeReloader
|
|
|
|
end
|
|
|
|
|
2021-04-15 02:10:25 +08:00
|
|
|
plug Phoenix.LiveDashboard.RequestLogger,
|
|
|
|
param_key: "request_logger",
|
|
|
|
cookie_key: "request_logger"
|
|
|
|
|
2021-01-08 03:55:45 +08:00
|
|
|
plug Plug.RequestId
|
|
|
|
plug Plug.Telemetry, event_prefix: [:phoenix, :endpoint]
|
|
|
|
|
|
|
|
plug Plug.Parsers,
|
|
|
|
parsers: [:urlencoded, :multipart, :json],
|
|
|
|
pass: ["*/*"],
|
|
|
|
json_decoder: Phoenix.json_library()
|
|
|
|
|
|
|
|
plug Plug.MethodOverride
|
|
|
|
plug Plug.Head
|
|
|
|
plug Plug.Session, @session_options
|
2021-10-05 06:44:27 +08:00
|
|
|
|
|
|
|
# Run custom plugs from the app configuration
|
|
|
|
plug LivebookWeb.ConfiguredPlug
|
|
|
|
|
2021-03-04 05:56:28 +08:00
|
|
|
plug LivebookWeb.Router
|
2021-07-01 18:17:49 +08:00
|
|
|
|
2022-01-21 16:18:02 +08:00
|
|
|
def access_struct_url() do
|
|
|
|
base =
|
|
|
|
case struct_url() do
|
|
|
|
%URI{scheme: "https", port: 0} = uri ->
|
|
|
|
%{uri | port: get_port(__MODULE__.HTTPS, 433)}
|
|
|
|
|
|
|
|
%URI{scheme: "http", port: 0} = uri ->
|
|
|
|
%{uri | port: get_port(__MODULE__.HTTP, 80)}
|
|
|
|
|
|
|
|
%URI{} = uri ->
|
|
|
|
uri
|
|
|
|
end
|
|
|
|
|
|
|
|
base = update_in(base.path, &(&1 || "/"))
|
2021-07-01 18:17:49 +08:00
|
|
|
|
|
|
|
if Livebook.Config.auth_mode() == :token do
|
|
|
|
token = Application.fetch_env!(:livebook, :token)
|
2022-01-21 16:18:02 +08:00
|
|
|
%{base | query: "token=" <> token}
|
|
|
|
else
|
|
|
|
base
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def access_url do
|
|
|
|
URI.to_string(access_struct_url())
|
|
|
|
end
|
|
|
|
|
|
|
|
defp get_port(ref, default) do
|
|
|
|
try do
|
|
|
|
:ranch.get_addr(ref)
|
|
|
|
rescue
|
|
|
|
_ -> default
|
2021-07-01 18:17:49 +08:00
|
|
|
else
|
2022-01-21 16:18:02 +08:00
|
|
|
{_, port} when is_integer(port) -> port
|
|
|
|
_ -> default
|
2021-07-01 18:17:49 +08:00
|
|
|
end
|
|
|
|
end
|
2021-01-08 03:55:45 +08:00
|
|
|
end
|