Skip to content

Commit

Permalink
fetch endpoint from site config
Browse files Browse the repository at this point in the history
  • Loading branch information
leandrocp committed Dec 12, 2024
1 parent 29f43e0 commit 1943905
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions lib/beacon/proxy_endpoint.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,12 @@ defmodule Beacon.ProxyEndpoint do
"""
defmacro __using__(opts) do
quote location: :keep, generated: true do
otp_app = Keyword.get(unquote(opts), :otp_app) || raise Beacon.RuntimeError, "FIXME 1"
otp_app = Keyword.get(unquote(opts), :otp_app) || raise Beacon.RuntimeError, "FIXME missing otp_app"

proxy_options = Application.compile_env!(otp_app, :endpoint) || raise Beacon.RuntimeError, "FIXME 2"

session_options = proxy_options[:session] || raise Beacon.RuntimeError, "FIXME 3"
session_options = Keyword.get(unquote(opts), :session_options) || raise Beacon.RuntimeError, "FIXME missing session_options"
Module.put_attribute(__MODULE__, :session_options, session_options)

endpoints = Keyword.get(unquote(opts), :endpoints, [])
Module.put_attribute(__MODULE__, :__beacon_proxy_endpoints__, endpoints)

fallback = Keyword.get(unquote(opts), :fallback) || raise Beacon.RuntimeError, "FIXME 4"
fallback = Keyword.get(unquote(opts), :fallback) || raise Beacon.RuntimeError, "FIXME missing fallback"
Module.put_attribute(__MODULE__, :__beacon_proxy_fallback__, fallback)

use Phoenix.Endpoint, otp_app: otp_app
Expand All @@ -36,8 +31,18 @@ defmodule Beacon.ProxyEndpoint do

def proxy(conn, opts) do
%{host: host} = conn
# TODO: generate pattern match clauses
endpoint = Enum.find(@__beacon_proxy_endpoints__, @__beacon_proxy_fallback__, &(&1.host() == host))

endpoint =
Enum.reduce_while(Beacon.Registry.running_sites(), @__beacon_proxy_fallback__, fn site, default ->
%{endpoint: endpoint} = Beacon.Config.fetch!(site)

if endpoint.host() == host do
{:halt, endpoint}
else
{:cont, default}
end
end)

endpoint.call(conn, endpoint.init(opts))
end
end
Expand Down

0 comments on commit 1943905

Please sign in to comment.