Skip to content

Commit

Permalink
Allow paths to be given with app in Plug.Static
Browse files Browse the repository at this point in the history
  • Loading branch information
José Valim committed Feb 2, 2015
1 parent 0576b9b commit a97936e
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions lib/plug/static.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ defmodule Plug.Static do
It must be a string.
* `:from` - the filesystem path to read static assets from.
It must be a string, containing a file system path, or an
It must be a string, containing a file system path, an
atom representing the application name, where assets will
be served from the priv/static.
be served from the priv/static, or a tuple containing the
application name and directory to serve them besides
priv/static.
The preferred form is to use `:from` with an atom, since
it will make your application independent from the starting
directory.
The preferred form is to use `:from` with an atom or tuple,
since it will make your application independent from the
starting directory.
If a static asset cannot be found, `Plug.Static` simply forwards
the connection to the rest of the pipeline.
Expand Down Expand Up @@ -90,9 +92,13 @@ defmodule Plug.Static do
qs_cache = Keyword.get(opts, :cache_control_for_vsn_requests, "public, max-age=31536000")
et_cache = Keyword.get(opts, :cache_control_for_etags, "public")

unless is_atom(from) or is_binary(from) do
raise ArgumentError, message: ":from must be an atom or a binary"
end
from =
case from do
{_, _} -> from
_ when is_atom(from) -> {from, "priv/static"}
_ when is_binary(from) -> from
_ -> raise ArgumentError, ":from must be an atom or a binary"
end

{Plug.Router.Utils.split(at), from, gzip, qs_cache, et_cache, only}
end
Expand Down Expand Up @@ -200,8 +206,8 @@ defmodule Plug.Static do
end
end

defp path(from, segments) when is_atom(from),
do: Path.join([Application.app_dir(from), "priv/static" | segments])
defp path({app, from}, segments) when is_atom(app) and is_binary(from),
do: Path.join([Application.app_dir(app), from | segments])
defp path(from, segments),
do: Path.join([from | segments])

Expand Down

0 comments on commit a97936e

Please sign in to comment.