Set default application configs for Elixir packages

血红的双手。 提交于 2019-12-10 21:45:46

问题


I'm writing an Elixir package, and I want to specify a default application configuration (that the user can override by specifying custom values in their config.exs). I was originally putting them in my project's config.exs until I realized that the config file won't be loaded for projects that depend on this library.

The config file itself tells you that:

This configuration is loaded before any dependency and is restricted to this project. If another project depends on this project, this file won't be loaded nor affect the parent project. For this reason, if you want to provide default values for your application for 3rd-party users, it should be done in your "mix.exs" file.


I've been struggling to understand how to specify application defaults in my mix.exs and use them. My current solution is to use Application.get_env/3 with a default argument but that doesn't seem right to me as the application defaults would be scattered through out the code.

Application.get_env(:my_library, :arg, "default value")

So, How can I specify application defaults in mix.exs?


回答1:


You can set default config values for your application in mix.exs - these will also be available when used as dependency in another project. For example:

def applications do
  [applications: [:logger, ...],
   mod: {MyLibrary.Application, []},
   env: [arg: "default value"]]
end


来源:https://stackoverflow.com/questions/43302779/set-default-application-configs-for-elixir-packages

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!