使用Application.app_dir:在config.exs(my_app应用, “私法”)
问题描述:
我正在尝试设置一个图书馆的配置,使其指向priv
文件路径(geoip的DB):”使用Application.app_dir:在config.exs(my_app应用, “私法”)
config :geolix, databases: [
%{
id: :city,
adapter: Geolix.Adapter.MMDB2,
source: Application.app_dir(:zipbooks, "priv") |> Path.join("data/GeoLite2-City.mmdb")
}
]
但我的
config :zipbooks, …
位于顶部的同一个文件中。我得到这个错误:
** (Mix.Config.LoadError) could not load config config/config.exs
** (ArgumentError) unknown application: :zipbooks
我使用的版本,所以我不能硬编码priv
路径,因为它的相对位置将会改变。我过去可靠地使用了Application.app_dir(:zipbooks, "priv")
,所以我很好奇如何在config.exs中实现这个功能
答
我猜这是不可能的。所以我最终做的是这样的:
def start(_type, _args) do
# See http://elixir-lang.org/docs/stable/elixir/Supervisor.html
# for other strategies and supported options
opts = [strategy: :one_for_one, name: ZB.Supervisor]
Application.get_env(:zipbooks, :env)
|> children
|> Supervisor.start_link(opts)
|> after_start
end
defp after_start({:ok, _} = result) do
Geolix.load_database(%{
id: :city,
adapter: Geolix.Adapter.MMDB2,
source: Application.app_dir(:zipbooks, "priv") |> Path.join("data/GeoLite2-City.mmdb")
})
result
end
defp after_start(result), do: result