Skip to content

Commit

Permalink
Merge branch 'main' into lp-endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
leandrocp committed Dec 11, 2024
2 parents bde1a82 + c760ca4 commit 29f43e0
Show file tree
Hide file tree
Showing 9 changed files with 155 additions and 121 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

## 0.3.2 (2024-12-11)

### Fixes

- Make the logic to find reachable sites less strict
Expand Down
4 changes: 2 additions & 2 deletions assets/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion assets/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "beacon",
"version": "0.3.1",
"version": "0.3.2",
"license": "MIT",
"repository": {},
"scripts": {
Expand Down
39 changes: 12 additions & 27 deletions lib/beacon/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@ defmodule Beacon.Router do
get "/__beacon_assets__/css-:md5", Beacon.Web.AssetsController, :css, assigns: %{site: opts[:site]}
get "/__beacon_assets__/js-:md5", Beacon.Web.AssetsController, :js, assigns: %{site: opts[:site]}

# simulate a beacon page inside site prefix so we can check this site is reachable?/2
get "/__beacon_check__", Beacon.Web.CheckController, :check, metadata: %{site: opts[:site]}

live "/*path", Beacon.Web.PageLive, :path
end
end
Expand Down Expand Up @@ -250,27 +253,13 @@ defmodule Beacon.Router do
@doc false
# Tells if a `beacon_site` is reachable in the current environment.
#
# Supposed the following router:
#
# scope "/", MyAppWeb, host: ["beacon-site-a.fly.dev"] do
# pipe_through [:browser]
# beacon_site "/", site: :site_a
# end
#
# scope "/", MyAppWeb, host: ["beacon-site-b.fly.dev"] do
# pipe_through [:browser]
# beacon_site "/", site: :site_b
# end
#
# On a node deployed to beacon-site-a.fly.dev, the second `beacon_site`
# will never match, so starting `:site_b` is a waste of resources
# and a common cause of problems on BeaconLiveAdmin since we can't resolve URLs properly.
#
# Similarly, if a `get "/"` is added _before_ either `beacon_site` that `get` would always
# match and invalidate the `beacon_site` mount.
# It's considered reachable if a dynamic page can be served on the site prefix.
def reachable?(%Beacon.Config{} = config, opts \\ []) do
%{site: site, endpoint: endpoint, router: router} = config
function_exported?(router, :__beacon_scoped_prefix_for_site__, 1) && reachable?(site, endpoint, router, opts)
reachable?(site, endpoint, router, opts)
rescue
# missing router or missing beacon macros in the router
_ -> false
end

defp reachable?(site, endpoint, router, opts) do
Expand All @@ -281,14 +270,10 @@ defmodule Beacon.Router do
router.__beacon_scoped_prefix_for_site__(site)
end)

case Phoenix.Router.route_info(router, "GET", prefix, host) do
# bypass and allow booting beacon sites even though there's a route conflict
# but only for root paths, for example:
# live /
# beacon_site /
# that's because even though they share the same prefix,
# beacon can still serve pages at /:page
%{route: "/"} ->
path = Beacon.Router.sanitize_path(prefix <> "/__beacon_check__")

case Phoenix.Router.route_info(router, "GET", path, host) do
%{site: ^site, plug: Beacon.Web.CheckController} ->
true

%{phoenix_live_view: {Beacon.Web.PageLive, _, _, %{extra: %{session: %{"beacon_site" => ^site}}}}} ->
Expand Down
16 changes: 16 additions & 0 deletions lib/beacon/web/controllers/check_controller.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
defmodule Beacon.Web.CheckController do
@moduledoc false
# TODO: replace CheckController with a plug in https://github.com/BeaconCMS/beacon/pull/694

import Plug.Conn

def init(conn), do: conn

def call(conn, _) do
conn
|> put_resp_header("content-type", "text/plain")
|> put_resp_header("cache-control", "public, max-age=31536000, immutable")
|> send_resp(200, "")
|> halt()
end
end
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule Beacon.MixProject do
use Mix.Project

@version "0.3.1"
@version "0.3.2"
@source_url "https://github.com/BeaconCMS/beacon"
@homepage_url "https://beaconcms.org"

Expand Down
Loading

0 comments on commit 29f43e0

Please sign in to comment.