From 9340edb2ea9b66eec1c849e41b78a7130a4ec24d Mon Sep 17 00:00:00 2001 From: Leandro Pereira Date: Tue, 10 Dec 2024 21:45:46 -0500 Subject: [PATCH] =?UTF-8?q?fix:=20boot=20beacon=20even=20without=20defined?= =?UTF-8?q?=20beacon=5Fsite=20routes=20in=20host=20app=20=E2=80=A6=20(#699?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: boot beacon even without defined beacon_site routes in host app router * clean up * docs --- guides/general/troubleshooting.md | 8 ++++++-- lib/beacon/router.ex | 3 +++ test/beacon/registry_test.exs | 1 + test/beacon/router_test.exs | 5 +++++ test/support/{router.ex => routers.ex} | 4 ++++ test/test_helper.exs | 7 +++++++ 6 files changed, 26 insertions(+), 2 deletions(-) rename test/support/{router.ex => routers.ex} (91%) diff --git a/guides/general/troubleshooting.md b/guides/general/troubleshooting.md index 20b9f3d8..e11b5ec4 100644 --- a/guides/general/troubleshooting.md +++ b/guides/general/troubleshooting.md @@ -29,10 +29,14 @@ a site prefix can never match and it will never receive requests. That's is not necessarily an error if you have multiple sites in the same project and each scope is filtering requests on the `:host` option. +But it may indicate: -But it may indicate an invalid configuration, as a preceding route matching the prefix +1. An invalid configuration, as a preceding route matching the prefix that was supposed to be handled by this site, or an invalid `:host` value. +2. Missing `use Beacon.Router` and/or missing `beacon_site` in your +app's router file. + Note that if you're using `:host` on the scope and running in `localhost`, consider adding `"localhost"` to the list of allowed hosts. @@ -41,4 +45,4 @@ Also check the [Beacon.Router](https://hexdocs.pm/beacon/Beacon.Router.html) for ## 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. \ No newline at end of file +and make sure is declared after the endpoint. diff --git a/lib/beacon/router.ex b/lib/beacon/router.ex index e01b0aa6..d5d1cd44 100644 --- a/lib/beacon/router.ex +++ b/lib/beacon/router.ex @@ -270,7 +270,10 @@ defmodule Beacon.Router do # match and invalidate the `beacon_site` mount. 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) + end + defp reachable?(site, endpoint, router, opts) do host = Keyword.get_lazy(opts, :host, fn -> endpoint.host() end) prefix = diff --git a/test/beacon/registry_test.exs b/test/beacon/registry_test.exs index 340cb8bd..b353cf08 100644 --- a/test/beacon/registry_test.exs +++ b/test/beacon/registry_test.exs @@ -12,6 +12,7 @@ defmodule Beacon.RegistryTest do :lifecycle_test, :lifecycle_test_fail, :my_site, + :no_routes, :not_booted, :raw_schema_test, :s3_site diff --git a/test/beacon/router_test.exs b/test/beacon/router_test.exs index c53e3e2f..25833863 100644 --- a/test/beacon/router_test.exs +++ b/test/beacon/router_test.exs @@ -86,5 +86,10 @@ defmodule Beacon.RouterTest do test "do not match any existing host/path", %{config: config} do refute Router.reachable?(config, host: nil, prefix: "/nested/invalid") end + + test "router without beacon routes" do + config = Beacon.Config.fetch!(:no_routes) + refute Router.reachable?(config) + end end end diff --git a/test/support/router.ex b/test/support/routers.ex similarity index 91% rename from test/support/router.ex rename to test/support/routers.ex index ba17b83b..e4c97810 100644 --- a/test/support/router.ex +++ b/test/support/routers.ex @@ -1,3 +1,7 @@ +defmodule Beacon.BeaconTest.NoRoutesRouter do + use Beacon.BeaconTest.Web, :router +end + defmodule Beacon.BeaconTest.Router do use Beacon.BeaconTest.Web, :router use Beacon.Router diff --git a/test/test_helper.exs b/test/test_helper.exs index 9a4a85e9..078d69b1 100644 --- a/test/test_helper.exs +++ b/test/test_helper.exs @@ -50,6 +50,13 @@ Supervisor.start_link( router: Beacon.BeaconTest.Router, repo: Beacon.BeaconTest.Repo ], + [ + site: :no_routes, + mode: :testing, + endpoint: Beacon.BeaconTest.Endpoint, + router: Beacon.BeaconTest.NoRoutesRouter, + repo: Beacon.BeaconTest.Repo + ], [ site: :not_booted, mode: :testing,