Giter VIP home page Giter VIP logo

Comments (5)

AndrewDryga avatar AndrewDryga commented on May 27, 2024 1

Hi.

Ecto has init callback, we use it in all our projects with Confex:

defmodule MyApp do
  use Ecto.Repo, otp_app: :my_app

  @doc """
  Dynamically loads the repository configuration from the environment variables.
  """
  def init(_, config) do
    url = System.get_env("DATABASE_URL")
    config = if url, do: Ecto.Repo.Supervisor.parse_url(url), else: Confex.process_env(config)

    unless config[:database] do
      raise "Set DB_NAME environment variable!"
    end

    unless config[:username] do
      raise "Set DB_USER environment variable!"
    end

    unless config[:password] do
      raise "Set DB_PASSWORD environment variable!"
    end

    unless config[:hostname] do
      raise "Set DB_HOST environment variable!"
    end

    unless config[:port] do
      raise "Set DB_PORT environment variable!"
    end

    {:ok, config}
  end
end

from confex.

AndrewDryga avatar AndrewDryga commented on May 27, 2024

@dropfen I've added this sample to README.md, thanks using Confex 👍

from confex.

 avatar commented on May 27, 2024

@AndrewDryga I just saw it, will try it out ASAP. I guess, I just had the aha moment, for how it works and so on. Let me express:

  • The config.exs is compiled at build time, so there is no way to pass values at runtime
  • So, we use callbacks/config-handlers where we are checking the compiled-in configs, and load them if they are missing from Confex (its System.get_env under the hood)

Still missing point:

  • how to handle other libs, where we do not have callbacks? Like guardian for example, or whatever xyz-lib

Thank you Andrew!

Boris

from confex.

 avatar commented on May 27, 2024

Thanks Andrew, it worked! 👍

from confex.

AndrewDryga avatar AndrewDryga commented on May 27, 2024

Unfortunately It's a question that should be pointed to libraries developers. Usually you have one of few options:

  1. You can define default config in confix.ex, and pass overrides in supervisor args (when adding external supervisor to your app's supervision tree);
  2. Lib can provide callbacks;
  3. Some libs have their own {:system, ..} tuples implementation.

If you using something that don't have those options, only way for you is to replace ENV variables via REPLACE_OS_VARS=true. You can read more about it at Distillery docs.

from confex.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.