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

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
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
kostonstyle
Hi all I want to have a unix time, from the current time plus 1 hour. DateTime.now + 1 hour How to get it in elixir? Thanks
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
Exadra37
Sometimes I want to check if the input into a function is not a blank string. My first approach: defmodule Example do def do_stuff(s...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

Other popular topics Top

chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30048 115
New
peerreynders
Manning 2016 Halloween weekend sale via Deal of the Day Friday, October 28 - Half off all MEAPs - code WM102816LT Saturday, October 29 ...
326 29600 154
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 27727 240
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
AstonJ
by Lance Halvorsen Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
460 27162 124
New

We're in Beta

About us Mission Statement