在 config.exs 中使用 Application.app_dir(:my_app, "priv")

ato*_*irk 5 elixir

我正在尝试将库配置设置为指向以下文件路径(geoip db)priv:'

config :geolix, databases: [
  %{
    id:      :city,
    adapter: Geolix.Adapter.MMDB2,
    source:  Application.app_dir(:zipbooks, "priv") |> Path.join("data/GeoLite2-City.mmdb")
  }
]
Run Code Online (Sandbox Code Playgroud)

但我的

config :zipbooks, …
Run Code Online (Sandbox Code Playgroud)

位于顶部的同一个文件中。我收到此错误:

** (Mix.Config.LoadError) could not load config config/config.exs
    ** (ArgumentError) unknown application: :zipbooks
Run Code Online (Sandbox Code Playgroud)

我使用发行版,所以我不能硬编码priv路径,因为它的相对位置会改变。我过去Application.app_dir(:zipbooks, "priv")可靠地使用过,所以我想知道如何在 config.exs 中完成此操作

ato*_*irk 5

我猜这是不可能的。所以我最终做的是这样的:

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
Run Code Online (Sandbox Code Playgroud)