Skip to content

Commit

Permalink
Merge branch 'main' into apb/js-hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
leandrocp committed Jan 17, 2025
2 parents dc59c4d + b2dea54 commit 833c83c
Show file tree
Hide file tree
Showing 11 changed files with 68 additions and 29 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:

# latest
- otp: "27"
elixir: "1.17"
elixir: "1.18"
phoenix: "~> 1.7"
phoenix-live-view: "~> 1.0"
postgres: '16.4-alpine'
Expand Down Expand Up @@ -96,7 +96,7 @@ jobs:
include:
# latest
- otp: "27"
elixir: "1.17"
elixir: "1.18"
phoenix: "~> 1.7"
phoenix-live-view: "~> 1.0"

Expand Down
4 changes: 2 additions & 2 deletions dev.exs
Original file line number Diff line number Diff line change
Expand Up @@ -1220,8 +1220,8 @@ Task.start(fn ->
children = [
Demo.Repo,
{Phoenix.PubSub, [name: Demo.PubSub]},
DemoWeb.Endpoint,
{Beacon, sites: [dev_site, dy_site]}
{Beacon, sites: [dev_site, dy_site]},
DemoWeb.Endpoint
]

{:ok, _} = Supervisor.start_link(children, strategy: :one_for_one)
Expand Down
5 changes: 0 additions & 5 deletions guides/general/troubleshooting.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,3 @@ Note that if you're using `:host` on the scope and running in `localhost`,
consider adding `"localhost"` to the list of allowed hosts.

Also check the [Beacon.Router](https://hexdocs.pm/beacon/Beacon.Router.html) for more information.

## RuntimeError - could not find persistent term for endpoint

`Beacon` should be started after your host's `Endpoint`, please review the application children
and make sure is declared after the endpoint.
19 changes: 18 additions & 1 deletion guides/upgrading/v0.4.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,21 @@ Then, within the generated module, call Beacon's migration helpers:
use Ecto.Migration
def up, do: Beacon.Migration.up()
def down, do: Beacon.Migration.down()
```
```

## Move `Beacon` before the endpoint in application.ex

Open the file `application.ex`, find the Beacon tuple in the list of `children`,
and move it to before the endpoint(s) declarations:

```elixir
@impl true
def start(_type, _args) do
children = [
# ...
{Beacon, [sites: [Application.fetch_env!(:beacon, :my_site)]]}, # <- moved to before `MyAppWeb.Endpoint`
MyAppWeb.Endpoint
]
```

In v0.3.0 the order was inverted which caused a regression on crash recoveries.
13 changes: 6 additions & 7 deletions lib/beacon/beacon.ex
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,12 @@ defmodule Beacon do
children = [
MyApp.Repo,
{Phoenix.PubSub, name: MyApp.PubSub},
MyAppWeb.Endpoint,
{Beacon,
[
sites: [
Application.fetch_env!(:beacon, :my_site)
]
]}
{Beacon, [
sites: [
Application.fetch_env!(:beacon, :my_site)
]
]},
MyAppWeb.Endpoint
]
opts = [strategy: :one_for_one, name: MyApp.Supervisor]
Expand Down
22 changes: 22 additions & 0 deletions lib/beacon/private.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ defmodule Beacon.Private do

# Should be avoided as much as possible.

@doc """
Fetch the host app `:otp_app` from the Repo config.
"""
def otp_app!(%Beacon.Config{repo: repo}) do
repo.config()[:otp_app] || raise Beacon.RuntimeError, "failed to discover :otp_app"
rescue
_ -> reraise Beacon.RuntimeError, [message: "failed to discover :otp_app, make sure Repo is started before Beacon"], __STACKTRACE__
end

@phoenix_live_view_version to_string(Application.spec(:phoenix_live_view)[:vsn])

@doc """
Expand Down Expand Up @@ -48,4 +57,17 @@ defmodule Beacon.Private do
Phoenix.LiveView.Route.live_link_info(endpoint, router, url)
end
end

def endpoint_config(otp_app, endpoint) do
Phoenix.Endpoint.Supervisor.config(otp_app, endpoint)
end

def endpoint_host(otp_app, endpoint) do
url_config = endpoint_config(otp_app, endpoint)[:url]
host_to_binary(url_config[:host] || "localhost")
end

# https://github.com/phoenixframework/phoenix/blob/4ebefb9d1f710c576f08c517f5852498dd9b935c/lib/phoenix/endpoint/supervisor.ex#L301-L302
defp host_to_binary({:system, env_var}), do: host_to_binary(System.get_env(env_var))
defp host_to_binary(host), do: host
end
7 changes: 4 additions & 3 deletions lib/beacon/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -257,15 +257,16 @@ defmodule Beacon.Router do
#
# It's considered reachable if a dynamic page can be served on the site prefix.
def reachable?(%Beacon.Config{} = config, opts \\ []) do
otp_app = Beacon.Private.otp_app!(config)
%{site: site, endpoint: endpoint, router: router} = config
reachable?(site, endpoint, router, opts)
reachable?(site, endpoint, router, otp_app, opts)
rescue
# missing router or missing beacon macros in the router
_ -> false
end

defp reachable?(site, endpoint, router, opts) do
host = Keyword.get_lazy(opts, :host, fn -> endpoint.host() end)
defp reachable?(site, endpoint, router, otp_app, opts) do
host = Keyword.get_lazy(opts, :host, fn -> Beacon.Private.endpoint_host(otp_app, endpoint) end)

prefix =
Keyword.get_lazy(opts, :prefix, fn ->
Expand Down
3 changes: 1 addition & 2 deletions lib/beacon/web/cache/stale.ex
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ defimpl Beacon.Web.Cache.Stale, for: Any do
[last_modified, Any.recurse_fields(schema, assocs, &Stale.last_modified/1)]
end

defp fetch_last_modified(_schema, nil), do: ~N[0000-01-01 00:00:00]
defp fetch_last_modified(schema, key), do: Map.fetch!(schema, key)
defp fetch_last_modified(schema, key), do: Map.get(schema, key) || ~N[0000-01-01 00:00:00]
end
end
end
Expand Down
3 changes: 2 additions & 1 deletion lib/mix/tasks/beacon.gen.site.ex
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,8 @@ defmodule Mix.Tasks.Beacon.Gen.Site do
quote do
[sites: [Application.fetch_env!(:beacon, unquote(site))]]
end}},
after: [repo, endpoint],
after: [repo],
before: [endpoint],
opts_updater: fn zipper ->
with {:ok, zipper} <-
Igniter.Code.Keyword.put_in_keyword(
Expand Down
4 changes: 2 additions & 2 deletions mix.lock
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
%{
"accent": {:hex, :accent, "1.1.1", "20257356446d45078b19b91608f74669b407b39af891ee3db9ee6824d1cae19d", [:mix], [{:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:plug, "~> 1.3", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "6d5afa50d4886e3370e04fa501468cbaa6c4b5fe926f72ccfa844ad9e259adae"},
"bandit": {:hex, :bandit, "1.6.4", "59cbc8e02f84fcad967bfed6b8a8261821c93a7ec4f835b46d1846b1120a91ec", [:mix], [{:hpax, "~> 1.0", [hex: :hpax, repo: "hexpm", optional: false]}, {:plug, "~> 1.14", [hex: :plug, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}, {:thousand_island, "~> 1.0", [hex: :thousand_island, repo: "hexpm", optional: false]}, {:websock, "~> 0.5", [hex: :websock, repo: "hexpm", optional: false]}], "hexpm", "8e156c009a77bb100fd78d5408684d01df1526f549c42614f8f9f27f44f1f7a7"},
"bandit": {:hex, :bandit, "1.6.5", "24096d6232e0d050096acec96a0a382c44de026f9b591b883ed45497e1ef4916", [:mix], [{:hpax, "~> 1.0", [hex: :hpax, repo: "hexpm", optional: false]}, {:plug, "~> 1.14", [hex: :plug, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}, {:thousand_island, "~> 1.0", [hex: :thousand_island, repo: "hexpm", optional: false]}, {:websock, "~> 0.5", [hex: :websock, repo: "hexpm", optional: false]}], "hexpm", "b6b91f630699c8b41f3f0184bd4f60b281e19a336ad9dc1a0da90637b6688332"},
"bunt": {:hex, :bunt, "1.0.0", "081c2c665f086849e6d57900292b3a161727ab40431219529f13c4ddcf3e7a44", [:mix], [], "hexpm", "dc5f86aa08a5f6fa6b8096f0735c4e76d54ae5c9fa2c143e5a1fc7c1cd9bb6b5"},
"bypass": {:hex, :bypass, "2.1.0", "909782781bf8e20ee86a9cabde36b259d44af8b9f38756173e8f5e2e1fabb9b1", [:mix], [{:plug, "~> 1.7", [hex: :plug, repo: "hexpm", optional: false]}, {:plug_cowboy, "~> 2.0", [hex: :plug_cowboy, repo: "hexpm", optional: false]}, {:ranch, "~> 1.3", [hex: :ranch, repo: "hexpm", optional: false]}], "hexpm", "d9b5df8fa5b7a6efa08384e9bbecfe4ce61c77d28a4282f79e02f1ef78d96b80"},
"castore": {:hex, :castore, "1.0.11", "4bbd584741601eb658007339ea730b082cc61f3554cf2e8f39bf693a11b49073", [:mix], [], "hexpm", "e03990b4db988df56262852f20de0f659871c35154691427a5047f4967a16a62"},
Expand Down Expand Up @@ -44,7 +44,7 @@
"mimerl": {:hex, :mimerl, "1.3.0", "d0cd9fc04b9061f82490f6581e0128379830e78535e017f7780f37fea7545726", [:rebar3], [], "hexpm", "a1e15a50d1887217de95f0b9b0793e32853f7c258a5cd227650889b38839fe9d"},
"mint": {:hex, :mint, "1.6.2", "af6d97a4051eee4f05b5500671d47c3a67dac7386045d87a904126fd4bbcea2e", [:mix], [{:castore, "~> 0.1.0 or ~> 1.0", [hex: :castore, repo: "hexpm", optional: true]}, {:hpax, "~> 0.1.1 or ~> 0.2.0 or ~> 1.0", [hex: :hpax, repo: "hexpm", optional: false]}], "hexpm", "5ee441dffc1892f1ae59127f74afe8fd82fda6587794278d924e4d90ea3d63f9"},
"nimble_options": {:hex, :nimble_options, "1.1.1", "e3a492d54d85fc3fd7c5baf411d9d2852922f66e69476317787a7b2bb000a61b", [:mix], [], "hexpm", "821b2470ca9442c4b6984882fe9bb0389371b8ddec4d45a9504f00a66f650b44"},
"nimble_parsec": {:hex, :nimble_parsec, "1.4.0", "51f9b613ea62cfa97b25ccc2c1b4216e81df970acd8e16e8d1bdc58fef21370d", [:mix], [], "hexpm", "9c565862810fb383e9838c1dd2d7d2c437b3d13b267414ba6af33e50d2d1cf28"},
"nimble_parsec": {:hex, :nimble_parsec, "1.4.1", "f41275a0354c736db4b1d255b5d2a27c91028e55c21ea3145b938e22649ffa3f", [:mix], [], "hexpm", "605e44204998f138d6e13be366c8e81af860e726c8177caf50067e1b618fe522"},
"nimble_pool": {:hex, :nimble_pool, "1.1.0", "bf9c29fbdcba3564a8b800d1eeb5a3c58f36e1e11d7b7fb2e084a643f645f06b", [:mix], [], "hexpm", "af2e4e6b34197db81f7aad230c1118eac993acc0dae6bc83bac0126d4ae0813a"},
"oembed": {:hex, :oembed, "0.5.0", "6fa2b54966771f1b9f8007d0b96c425de05db761161e794ed36d7a5f6e47713b", [:mix], [{:exconstructor, ">= 1.0.0", [hex: :exconstructor, repo: "hexpm", optional: false]}, {:floki, ">= 0.24.0", [hex: :floki, repo: "hexpm", optional: false]}, {:httpoison, ">= 0.9.0", [hex: :httpoison, repo: "hexpm", optional: false]}, {:poison, ">= 1.5.0", [hex: :poison, repo: "hexpm", optional: false]}], "hexpm", "c4006bc27d957ca39fa1378817bb8af28f992526300d35d444416d13151cef32"},
"parse_trans": {:hex, :parse_trans, "3.4.1", "6e6aa8167cb44cc8f39441d05193be6e6f4e7c2946cb2759f015f8c56b76e5ff", [:rebar3], [], "hexpm", "620a406ce75dada827b82e453c19cf06776be266f5a67cff34e1ef2cbb60e49a"},
Expand Down
13 changes: 9 additions & 4 deletions test/mix/tasks/gen_site_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,13 @@ defmodule Mix.Tasks.Beacon.GenSiteTest do
project
|> Igniter.compose_task("beacon.gen.site", @opts_my_site)
|> assert_has_patch("lib/test/application.ex", """
20 + | TestWeb.Endpoint,
21 + | {Beacon, [sites: [Application.fetch_env!(:beacon, :my_site)]]}
...|
12 12 | Test.Repo,
13 13 | {DNSCluster, query: Application.get_env(:test, :dns_cluster_query) || :ignore},
14 + | {Beacon, [sites: [Application.fetch_env!(:beacon, :my_site)]]},
14 15 | {Phoenix.PubSub, name: Test.PubSub},
15 16 | # Start the Finch HTTP client for sending emails
...|
""")
end

Expand All @@ -148,8 +153,8 @@ defmodule Mix.Tasks.Beacon.GenSiteTest do
|> apply_igniter!()
|> Igniter.compose_task("beacon.gen.site", @opts_other_site)
|> assert_has_patch("lib/test/application.ex", """
21 - | {Beacon, [sites: [Application.fetch_env!(:beacon, :my_site)]]}
21 + | {Beacon, [sites: [Application.fetch_env!(:beacon, :my_site), Application.fetch_env!(:beacon, :other)]]}
14 - | {Beacon, [sites: [Application.fetch_env!(:beacon, :my_site)]]},
14 + | {Beacon, [sites: [Application.fetch_env!(:beacon, :my_site), Application.fetch_env!(:beacon, :other)]]},
""")
end
end
Expand Down

0 comments on commit 833c83c

Please sign in to comment.