2017-10-20 35 views
1

我正在嘗試設置一個圖書館的配置,使其指向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中實現這個功能

回答

1

我猜這是不可能的。所以我最終做的是這樣的:

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