Comments (5)
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.
@dropfen I've added this sample to README.md, thanks using Confex 👍
from confex.
@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.
Thanks Andrew, it worked! 👍
from confex.
Unfortunately It's a question that should be pointed to libraries developers. Usually you have one of few options:
- You can define default config in
confix.ex
, and pass overrides in supervisor args (when adding external supervisor to your app's supervision tree); - Lib can provide callbacks;
- 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)
- Breakage when using configs with nil values
- Typespec is incorrect for get_env HOT 1
- Add CHANGELOG file
- Arrays not works HOT 1
- `get_env/2` inconsistent with `Application` for type range HOT 1
- RFC: Address compile/runtime issues in raised on Elixir Forum
- Lists of lists of config HOT 2
- Confex AWS Parameter store plugin HOT 3
- proposal: resolve envs in runtime HOT 3
- Confex in complex structures HOT 2
- Question on differences from Application.get_env HOT 3
- Credo lint for compile time variables HOT 1
- start confex (and my application) before logger ? HOT 3
- config() returns nil when resolving configuration fails
- Link to Distillery documentation is broken
- No default and nil as default aren't the same HOT 1
- More flexible adapter interface HOT 3
- Unable to read config from file HOT 1
- Nested data structs are not resolved as expected
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from confex.