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
Well, thereās also option to just use module names / functions directly, just canāt import them. So I need to make remote call, directly using module names of the other apps. There is no compile-time check for safety, and compiler gives you warning, but thatās probably fine - none of the other solutions give compile time safety either. But I think its the best and simplest. Iāll go with that. Leaving this thread here so itās helpful for someone in future
Iām not sure that this is the right way to go. Part of the idea of an application is that it can be run independently without anything other than items on its dependency list. If you try to run your admin panel without the user_interface happening to be there itās going to crash.
Which is still good I think. Even if user_interface is not running, if I do:
MyApp.UserInterface.Router.Helpers.some_path()
It will still work as long as the other app is compiled at some point and function exists/matches arity etc. The only problem I see is that compiler will not detect invalid function calls. I.e. I rearrange routes in user interface and my admin interface will crash.
Well I donāt know, it looks like stupid question but surprisingly tough one.
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.
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.
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:
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
And this is in fact how itās going to be deployed/built with distillery. Okay thank you all. Iāll go with not making those apps dependent on each other at all, and hard-code the URLs the way @Gazler suggests.
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.