This can be handled with an argument to the supervisor. For example, the Ecto supervisor uses the adapter argument to indicate which type of database to use:
# https:
You can do the same in your application, maybe one argument for start_link will be enough - call it backend
# my_app/lib/my_app/supervisor.ex defmodule MyApp.Supervisor do def start_link(backend) do
Now you can, of course, set this argument dynamically when deploying the application based on the configuration file:
# my_app/lib/my_app.ex defmodule MyApp do use Application def start(_type, _args) do MyApp.Supervisor.start_link(backend) end
Now the only part that is missing is how to get the backend from the configuration file. There is no single answer to this, because there are several ways to do this.
Mix Configuration
You can simply use the existing Mix configuration, but it has the disadvantage that you need to recompile the application every time you change the configuration:
Then configure the application to read in the specified backend:
# my_app/lib/my_app.ex defmodule MyApp do use Application def start(_type, _args) do
Create your own
You can also implement your own configuration file. I will not go into details here, but this is a rough idea:
- Store the configuration file somewhere
- Read the parsing in Elixir
- Convert string to module name using
String.to_existing_atom("Elixir.#{module_name}") - This will throw an error if the atom (thus the module name) does not exist
- use it in your
def backend function
Use Existing Runtime Configuration Library
Basically an illustrious version of the previous solution. Several times I searched for a library called Conform . It looks interesting, but I can not do promises because I never used it myself.
source share