How does Elixir work when using a remote config center?

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.

3 Likes

thank you for your reply
does config have to be used with release?

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.

2 Likes
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
def project do
  [
    app: :t2,
    version: "0.1.0",
    elixir: "~> 1.12",
    start_permanent: Mix.env() == :prod,
    deps: deps(),
    releases: [
      demo: [
        config_providers: [
          {JSONConfigProvider, "/etc/config.json"}
        ]
      ]
    ]
  ]
end

I do this in my project refer to the official document, but not works. I don’t use release, just iex -S mix start

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.

use Mix.Config

config :t2, :passwd, MyModule.test()

MyModule.test is my function to fetch configs, but this can not work, what should I do in runtime.exs,
thanks