Skip to content

Commit

Permalink
fix: boot beacon even without defined beacon_site routes in host app … (
Browse files Browse the repository at this point in the history
#699)

* fix: boot beacon even without defined beacon_site routes in host app router

* clean up

* docs
  • Loading branch information
leandrocp authored Dec 11, 2024
1 parent 6b05085 commit 9340edb
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 2 deletions.
8 changes: 6 additions & 2 deletions guides/general/troubleshooting.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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.
and make sure is declared after the endpoint.
3 changes: 3 additions & 0 deletions lib/beacon/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
1 change: 1 addition & 0 deletions test/beacon/registry_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ defmodule Beacon.RegistryTest do
:lifecycle_test,
:lifecycle_test_fail,
:my_site,
:no_routes,
:not_booted,
:raw_schema_test,
:s3_site
Expand Down
5 changes: 5 additions & 0 deletions test/beacon/router_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 4 additions & 0 deletions test/support/router.ex → test/support/routers.ex
Original file line number Diff line number Diff line change
@@ -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
Expand Down
7 changes: 7 additions & 0 deletions test/test_helper.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 9340edb

Please sign in to comment.