fireproofsocks

fireproofsocks

Customize location of priv directory

Is it possible to customize the location of the priv/ directory? From what I understand, this is part of the OTP directory structure, so it may be hard-coded out of necessity. There are some cases (particularly when code interacts with other languages e.g. NodeJS) when it would be helpful to be able to configure the location of static assets to be somewhere other than priv/. I’ve had some success using the overlays option with releases, but it’s not quite the same as having the assurance that an entire directory will exist in a predictable place.

Can anyone shed some light on this? TIA!

Most Liked

Eiji

Eiji

This is more Erlang-related question than Elixir. On one hand having a path to some directory in let’s say environment variable is good idea. Elixir on its own is flexible and does not force much things like many languages/frameworks. However it’s based on Erlang and this is a bit different story.

Returns the path to the priv directory in an application. Equivalent to code:lib_dir(Name, priv)

Source: https://www.erlang.org/doc/man/code#priv_dir-1

Returns the path to a subdirectory directly under the top directory of an application. Normally the subdirectories reside under the top directory for the application, but when applications at least partly reside in an archive, the situation is different. Some of the subdirectories can reside as regular directories while others reside in an archive file. It is not checked whether this directory exists.

Source: https://www.erlang.org/doc/man/code#lib_dir-2

Both lib_dir and priv_dir functions are essential in Erlang and trying to changing priv path may cause unexpected problems. Also priv files are usually packages into release, so even if it would be in different directory on dev/build machine then you would need to alter the packaging logic, so the files would be extracted in another location on prod machine.

Other solutions may also have their own downsides. Symbolic links may work without any problem or be not supported at all. The biggest problem here would be for example an existing symbolic link to not existing path. Creating directory would not work as same as without symlink.

Also have in mind that in priv directory there could be much more files than just assets. Trying to change priv location at least in long term perspective seems not worth the effort.

However that’s not all options we have, right? Elixir is flexible, so let’s try find something on Elixir side that works better in your use case … I would go in a bit different way of thinking. I see 2 options:

  1. Add a code which at runtime, right after application starts, changes some configuration files. That should be simple to do and this way other applications do not need to worry about paths as all of them could simply fetch a value of environment variable.

  2. Plug.Static-based solution - if assets are all you need then you should be fine with the plug’s flexibility.

  • :from - the file system path to read static assets from. It can be either: a string containing a file system path, an atom representing the application name (where assets will be served from priv/static), a tuple containing the application name and the directory to serve assets from (besides priv/static), or an MFA tuple.

Source: https://hexdocs.pm/plug/Plug.Static.html

This way allows not only to specify a different location for each environment, but also it’s easy to add checks and fall back just in case something bad would happen. That’s said … since it happens on runtime it could (but not need) to increase a response time …

plug Plug.Static, from: {MyApp.ModuleName, :func_name, [Mix.env()])

# …

def func_name(env) when env in ~w[dev test]a do
  # path on dev and CI machines
  :my_app
  |> :code.priv_dir()
  |> Path.join("static")
end

def func_name(_env) do
  # for example fetch an environment variable
  prod_path = get_prod_path()

  if File.exists?(prod_path) do
    # prod-only path
    prod_path
  else
    # fallback
    func_name(:dev)
  end
end

Where Next?

Popular in Questions Top

vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New

Other popular topics Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 54006 488
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New

We're in Beta

About us Mission Statement