hey guys
I want fetch password of mysql from a remote config center by http request, I try to do this, get and set when my application start, but not works, looks like deps start before my app, what should I do, thanks.
use Application
@impl true
def start(_type, _args) do
children = [
# Starts a worker by calling: T2.Worker.start_link(arg)
# {T2.Worker, arg}
]
# See https://hexdocs.pm/elixir/Supervisor.html
# for other strategies and supported options
opts = [strategy: :one_for_one, name: T2.Supervisor]
Supervisor.start_link(children, opts)
end
Yes, dependencies start before your application is started (that’s what they’re dependencies for). You likely want to use runtime.exs or create a Config.Provider. The latter is more suited to your need for an http client.
Also please use code blocks with text and not images for code. Images are neither accessible nor great for mobile users.
No. releases.exs used to work only with releases, but was later replaced with runtime.exs and Config.Provider, which works with mix as well as releases.
defmodule JSONConfigProvider do
@behaviour Config.Provider
# Let's pass the path to the JSON file as config
@impl true
def init(path) when is_binary(path), do: path
@impl true
def load(config, path) do
# We need to start any app we may depend on.
{:ok, _} = Application.ensure_all_started(:jason)
json = path |> File.read!() |> Jason.decode!()
Config.Reader.merge(
config,
my_app: [
some_value: json["my_app_some_value"],
another_value: json["my_app_another_value"],
]
)
end
end
Ah sorry. Seems like runtime.exs doesn’t require releases, but custom Config.Providers do. Afaik runtime.exs uses a Config.Provider under the hood as well, so I thought they align on that.