Avoiding circular dependencies between 2 umbrella Phoenix applications using each other's routers

In this instance, I’d probably hard-code the URLs on the basis that they are acting as 2 independent systems. I’d probably configure the base url for each so that it can be easily changed.

Assuming the admin interface is running on port 5000 and the web interface on 4000, then I’d configure it like:

config :admin, Admin.WebRouteHelpers,
  base_url: "http://localhost:4000"

config :web, Web.AdminRouteHelpers,
  base_url: "http://localhost:5000"

Then have a module like:

defmodule Admin.WebRouteHelpers,

# you can even ignore the first argument so it looks like a "normal" phoenix path helper
def admin_users_path(_, :index) do
  base_url  = Application.get_env(:admin, __MODULE__) |> Keyword.get(:base_url)
  base_url <> "/users"
end

If I was paranoid about these going out of sync then I’d have a 3rd application that is just for testing, that includes both applications as a dependency and assert the urls match.

This would ensure that things work even if you deploy your admin interface independently from the web interface e.g. admin.myapp.com

1 Like