Skip to content

Commit

Permalink
Use persistent term form precomputed static configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed Nov 22, 2022
1 parent f095d1f commit 4e57eee
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 198 deletions.
2 changes: 1 addition & 1 deletion lib/phoenix/config.ex
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ defmodule Phoenix.Config do
e ->
case :ets.info(module) do
:undefined ->
raise "could not find ets table for endpoint #{inspect(module)}. Make sure your endpoint is started and note you cannot access endpoint functions at compile-time."
raise "could not find ets table for endpoint #{inspect(module)}. Make sure your endpoint is started and note you cannot access endpoint functions at compile-time"

_ ->
reraise e, __STACKTRACE__
Expand Down
51 changes: 13 additions & 38 deletions lib/phoenix/endpoint.ex
Original file line number Diff line number Diff line change
Expand Up @@ -524,28 +524,25 @@ defmodule Phoenix.Endpoint do
Phoenix.Endpoint.Supervisor.config_change(__MODULE__, changed, removed)
end

defp persistent!() do
:persistent_term.get({Phoenix.Endpoint, __MODULE__}, nil) ||
raise "could not find persistern term for endpoint #{inspect(__MODULE__)}. Make sure your endpoint is started and note you cannot access endpoint functions at compile-time"
end

@doc """
Generates the endpoint base URL without any path information.
It uses the configuration under `:url` to generate such.
"""
def url do
Phoenix.Config.cache(__MODULE__,
:__phoenix_url__,
&Phoenix.Endpoint.Supervisor.url/1)
end
def url, do: persistent!().url

@doc """
Generates the static URL without any path information.
It uses the configuration under `:static_url` to generate
such. It falls back to `:url` if `:static_url` is not set.
"""
def static_url do
Phoenix.Config.cache(__MODULE__,
:__phoenix_static_url__,
&Phoenix.Endpoint.Supervisor.static_url/1)
end
def static_url, do: persistent!().static_url

@doc """
Generates the endpoint base URL but as a `URI` struct.
Expand All @@ -554,55 +551,33 @@ defmodule Phoenix.Endpoint do
Useful for manipulating the URL data and passing it to
URL helpers.
"""
def struct_url do
Phoenix.Config.cache(__MODULE__,
:__phoenix_struct_url__,
&Phoenix.Endpoint.Supervisor.struct_url/1)
end
def struct_url, do: persistent!().struct_url

@doc """
Returns the host for the given endpoint.
"""
def host do
Phoenix.Config.cache(__MODULE__,
:__phoenix_host__,
&Phoenix.Endpoint.Supervisor.host/1)
end
def host, do: persistent!().host

@doc """
Generates the path information when routing to this endpoint.
"""
def path(path) do
Phoenix.Config.cache(__MODULE__,
:__phoenix_path__,
&Phoenix.Endpoint.Supervisor.path/1) <> path
end
def path(path), do: persistent!().path <> path

@doc """
Generates the script name.
"""
def script_name do
Phoenix.Config.cache(__MODULE__,
:__phoenix_script_name__,
&Phoenix.Endpoint.Supervisor.script_name/1)
end
def script_name, do: persistent!().script_name

@doc """
Generates a route to a static file in `priv/static`.
"""
def static_path(path) do
Phoenix.Config.cache(__MODULE__, :__phoenix_static__,
&Phoenix.Endpoint.Supervisor.static_path/1) <>
elem(static_lookup(path), 0)
end
def static_path(path), do: persistent!().static_path <> elem(static_lookup(path), 0)

@doc """
Generates a base64-encoded cryptographic hash (sha512) to a static file
in `priv/static`. Meant to be used for Subresource Integrity with CDNs.
"""
def static_integrity(path) do
elem(static_lookup(path), 1)
end
def static_integrity(path), do: elem(static_lookup(path), 1)

@doc """
Returns a two item tuple with the first item being the `static_path`
Expand Down
Loading

0 comments on commit 4e57eee

Please sign in to comment.