hubertlepicki

hubertlepicki

So I have the umbrella project that consists of user_interface and admin applications. I want to display some links from user_interface back to appropriate admin panel section when logged in user is admin. And I also want to display links to public facing URLs in user_interface, from within admin_panel.

As you can guess I am creating circular dependency here. One application has to be compiled before the other, so I think I need to decouple these.

As far as I see it, I have the options to:

  • move the routes resolution to runtime. I think either exposing GenServers with registered names on each end that expose underlying router functions will do, or I an use :erlang.apply
  • use dependency injection where I would figure out the appropriate router module at run time
  • hardcode the URLs :wink:

Any other ideas? What do you think would be best?

Marked As Solved

Gazler

Gazler

Phoenix Core Team

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

Also Liked

josevalim

josevalim

Creator of Elixir

Agreed. Cyclic dependencies should be avoided and it is very likely you are going to run into issues, such as xref warnings. Ideally you would want to move this behind a configuration and have a explicit dependency from a → b or b → a, but not both ways (which is not even possible Mix-wise).

If you expecting too many options, then it likely means you have an artificial boundary between your applications and they should likely stick together.

michalmuskala

michalmuskala

For me this is a similar case to having, for example, front-end URLs on back-end in a SPA application (for example a URL in an email).

My preferred way of dealing with things like that is through configuration, similar to a way that @Gazler showed. Alternatively, you could have the configuration keep an MFA you’d call at runtime to produce the link. This, of course, also has the disadvantage of not being checked at compile-time, similar to other dynamic functions.

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

It’s this assumption that is the issue. Build a release with just your admin app in it. It’s only going to have the admin app and the code from the admin apps dependencies. It isn’t going to have any code from the user_interface app.

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews