livebook/lib/livebook_web/verified_routes.ex
José Valim edefa6649a Avoid compile-connected dependencies
Before this commit, LivebookWeb had runtime dependencies into
the project, causing large compilation cycles.

Using the following command in Elixir v1.17.3+

    $ mix xref graph --format stats --label compile-connected

Would reveal:

    Top 10 files with most incoming dependencies:
      * lib/livebook_web.ex (97)
      * lib/livebook/config.ex (3)
      * proto/lib/livebook_proto/deployment_group.pb.ex (2)

After this patch:

    Top 10 files with most incoming dependencies:
      * lib/livebook/config.ex (3)
      * proto/lib/livebook_proto/deployment_group.pb.ex (2)
      * lib/livebook_web/plugs/memory_provider.ex (2)
2024-08-07 01:16:30 +02:00

49 lines
1.6 KiB
Elixir

defmodule LivebookWeb.VerifiedRoutes do
require Phoenix.VerifiedRoutes
defmacro sigil_p({:<<>>, _meta, ["/public/" <> _ | _]} = route, extra) do
# We allow configuring a base path for all routes and we configure
# Phoenix to use it. However, we have an additional configuration
# for base path applying only to /public. We use a custom sigil_p
# to insert this base path if needed.
quote do
path = Phoenix.VerifiedRoutes.sigil_p(unquote(route), unquote(extra))
LivebookWeb.VerifiedRoutes.__rewrite_public_base_path__(path)
end
end
defmacro sigil_p(route, extra) do
quote do
Phoenix.VerifiedRoutes.sigil_p(unquote(route), unquote(extra))
end
end
@doc """
Returns the base url path for the Livebook endpoint.
"""
@spec base_url_path() :: String.t()
def base_url_path() do
# Use fully qualified module name to avoid compile time dependencies
path = Application.get_env(:livebook, :"Elixir.LivebookWeb.Endpoint")[:url][:path]
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) do
nil -> base_url_path()
path -> String.trim_trailing(path, "/")
end
end
@doc false
def __rewrite_public_base_path__(path) do
base_url_path = base_url_path()
public_base_url_path = public_base_url_path()
^base_url_path <> rest = path
public_base_url_path <> rest
end
end