livebook/lib/livebook_web/plugs/file_system_provider.ex
Jonatan Kłosko 8b37e32e3a
Escript (#77)
* Set up Escript packaging

* Use MD5 digest sa ETAG

* Make sure changes to the static files recompile the relevant module

* Manually start the application in Escript

* Set up basic CLI

* Run formatter

* Start Elixir app before anything else

* Improve version output

* Build Escript to project root directory

* Improve assets handling

* Move plug related modules under plugs directory

* Include bundled assets in the repository

* Use the same plug with different static providers in prod and dev

* Refactor providers

* Rename StaticProvidedPlug to StaticPlug
2021-03-17 01:53:44 +01:00

38 lines
1.1 KiB
Elixir

defmodule LivebookWeb.FileSystemProvider do
@moduledoc false
# Configurable implementation of `LivebookWeb.StaticPlug.Provider` behaviour,
# that loads files directly from the file system.
#
# ## `use` options
#
# * `:from` (**required**) - where to read the static files from. See `Plug.Static` for more details.
defmacro __using__(opts) do
quote bind_quoted: [opts: opts] do
@behaviour LivebookWeb.StaticPlug.Provider
from = Keyword.fetch!(opts, :from)
static_path = LivebookWeb.StaticPlug.Provider.static_path(from)
@impl true
def get_file(segments, compression) do
LivebookWeb.FileSystemProvider.__get_file__(unquote(static_path), segments, compression)
end
end
end
def __get_file__(static_path, segments, nil) do
abs_path = Path.join([static_path | segments])
if File.regular?(abs_path) do
content = File.read!(abs_path)
digest = content |> :erlang.md5() |> Base.encode16(case: :lower)
%LivebookWeb.StaticPlug.File{content: content, digest: digest}
else
nil
end
end
def __get_file__(_static_path, _segments, _compression), do: nil
end