Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using Application.app_dir(:my_app, "priv") in config.exs

Tags:

elixir

I'm trying to set a library config to point to a file path (a geoip db) in priv:'

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

but my

config :zipbooks, …

is in the same file at the top. I get this error:

** (Mix.Config.LoadError) could not load config config/config.exs
    ** (ArgumentError) unknown application: :zipbooks

I use releases, so I can't hard code the priv path because the relative location of it will change. I've used Application.app_dir(:zipbooks, "priv") reliably in the past so I'm wonder how to accomplish this in config.exs

like image 877
atomkirk Avatar asked Oct 20 '17 18:10

atomkirk


2 Answers

I'm guessing this isn't possible. So what I ended up doing was this:

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
like image 143
atomkirk Avatar answered Sep 26 '22 20:09

atomkirk


I ended up using the following in my config.ex. Seems to work fine for me in dev builds so far.

config :zippy, :assets_folder, Path.join(Path.dirname(__DIR__), "priv/assets")

Found this solution over here: https://groups.google.com/forum/#!topic/phoenix-talk/teaQI8fzHuQ

like image 28
splatte Avatar answered Sep 25 '22 20:09

splatte