would anyone be able to tell me how to protect certain static routes? Why? - plug.static does so much under the hood, I would like to utilise its goodness such as caching and concurrent reads to serve js files from protected routes.
You can add a custom plug in your pipeline that would wrap Plug.Static and implement custom protection logic there.
# assuming a Phoenix project
defmodule MyAppWeb.Endpoint do
# ...
# private static files
plug :private_static, Plug.Static.init(at: "/private", from: :my_app)
# public static files
plug Plug.Static, at: "/", from: :my_app, only: MyAppWeb.static_paths()
# ...
defp private_static(%Plug.Conn{path_info: ["private" | _]} = conn, plug_static_opts) do
if private_static_allowed?(conn) do
# NOTE: if it doesn't match any files, then it would pass-through to router
Plug.Static.call(conn, plug_static_opts)
else
conn |> send_resp(403, "forbidden or whatev") |> halt()
end
end
# pass-through non /private/** routes
defp private_static(conn, _opts), do: conn
defp private_static_allowed?(conn) do
# your custom logic
true
end
end
And you can also do it in the router. It might be even easier that way.