Skip to content
This repository has been archived by the owner on Jun 30, 2021. It is now read-only.

Commit

Permalink
Fix assets path in releases (#763)
Browse files Browse the repository at this point in the history
  • Loading branch information
sirn authored Feb 4, 2019
1 parent c8a0b97 commit 0b3a197
Show file tree
Hide file tree
Showing 38 changed files with 313 additions and 228 deletions.
7 changes: 0 additions & 7 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,6 @@ docker-compose.override.yml
# Erlang
erl_crash.dump

# Public
/apps/admin_api/public/uploads
/private
/public/docs
/public/uploads
!/public/uploads/test.txt

# Dev
.DS_Store
.elixir_ls
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ RUN set -xe \
ARG release_version

ADD _build/prod/rel/ewallet/releases/${release_version}/ewallet.tar.gz /app
RUN chown -R ewallet:ewallet /app
RUN chown -R "${uid}:${gid}" /app
WORKDIR /app

# eWallet app is using PORT environment variable to determine which port to run
Expand Down
13 changes: 10 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ deps-assets:
# Cleaning
#

clean: clean-ewallet clean-assets
clean: clean-ewallet clean-assets clean-test-assets

clean-ewallet:
rm -rf _build/
Expand All @@ -34,7 +34,14 @@ clean-assets:
rm -rf apps/admin_panel/assets/node_modules
rm -rf apps/admin_panel/priv/static

.PHONY: clean clean-ewallet clean-assets
clean-test-assets:
rm -rf private/
rm -rf public/
rm -rf _build/test/lib/url_dispatcher/priv/static/private/*
rm -rf _build/test/lib/url_dispatcher/priv/static/public/test-*
rm -rf _build/test/lib/url_dispatcher/priv/static/public/test/

.PHONY: clean clean-ewallet clean-assets clean-test-assets

#
# Linting
Expand Down Expand Up @@ -80,7 +87,7 @@ build-test: deps-ewallet

test: test-ewallet test-assets

test-ewallet: build-test
test-ewallet: clean-test-assets build-test
env MIX_ENV=test mix do ecto.create, ecto.migrate, test

test-assets: build-assets
Expand Down
11 changes: 0 additions & 11 deletions apps/admin_api/lib/admin_api/endpoint.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,6 @@ defmodule AdminAPI.Endpoint do
use Appsignal.Phoenix
use Sentry.Phoenix.Endpoint

plug(
Plug.Static,
at: "/public/",
from: Path.join(File.cwd!(), "../../public/"),
only: ~w(uploads)
)

plug(AdminAPI.AssetNotFoundPlug)

# Code reloading can be explicitly enabled under the
# :code_reloader configuration of your endpoint.
if code_reloading? do
plug(Phoenix.CodeReloader)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ defmodule AdminAPI.V1.ExportController do
alias EWallet.{ExportGate, ExportPolicy}
alias EWallet.Web.{Originator, Orchestrator, Paginator, V1.ExportOverlay}
alias EWalletDB.Export
alias Utils.Helpers.PathResolver

def all(conn, attrs) do
storage_adapter = Application.get_env(:admin_api, :file_storage_adapter)
Expand Down Expand Up @@ -54,7 +55,7 @@ defmodule AdminAPI.V1.ExportController do
:ok <- permit(:get, conn.assigns, export),
true <- export.adapter == "local" || {:error, :export_not_local},
true <- export.adapter == storage_adapter || {:error, :invalid_storage_adapter},
path <- Path.join(Application.get_env(:ewallet, :root), export.path),
path <- Path.join(PathResolver.static_dir(:url_dispatcher), export.path),
true <- File.exists?(path) || {:error, :file_not_found} do
send_download(
conn,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@

defmodule AdminAPI.V1.AdminAuth.TransactionExportControllerTest do
use AdminAPI.ConnCase
alias ActivityLogger.System
alias EWalletConfig.Config
alias EWalletDB.Uploaders
alias Utils.Helper.PidHelper
alias EWalletConfig.Config
alias ActivityLogger.System
alias Utils.Helpers.PathResolver

def setup do
assert Application.get_env(:admin_api, :file_storage_adapter) == "local"
Expand Down Expand Up @@ -76,7 +77,7 @@ defmodule AdminAPI.V1.AdminAuth.TransactionExportControllerTest do

{:ok, _} =
[
Application.get_env(:ewallet, :root),
PathResolver.static_dir(:url_dispatcher),
Uploaders.File.storage_dir(nil, nil)
]
|> Path.join()
Expand Down
2 changes: 1 addition & 1 deletion apps/admin_panel/config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use Mix.Config
config :admin_panel,
namespace: AdminPanel,
ecto_repos: [],
dist_path: Path.expand("../priv/static/", __DIR__),
dist_path: {:apply, {Utils.Helpers.PathResolver, :static_dir, [:admin_panel]}},
webpack_watch: {:system, "WEBPACK_WATCH", false}

# Configures the endpoint
Expand Down
2 changes: 0 additions & 2 deletions apps/admin_panel/config/prod.exs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use Mix.Config

# We need to serve admin assets using a precompiled static
# cache manifest in production.
config :admin_panel, AdminPanel.Endpoint, cache_static_manifest: "priv/static/cache_manifest.json"
7 changes: 5 additions & 2 deletions apps/admin_panel/lib/admin_panel/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ defmodule AdminPanel.Application do
use Application
require Logger
alias AdminPanel.Endpoint
alias EWallet.Helper
alias Phoenix.Endpoint.Watcher
alias Utils.Helpers.Normalize
import Supervisor.Spec

# See https://hexdocs.pm/elixir/Application.html
Expand All @@ -39,10 +39,13 @@ defmodule AdminPanel.Application do

children =
children ++
case Helper.to_boolean(webpack_watch) do
case Normalize.to_boolean(webpack_watch) do
true ->
_ = Logger.info("Enabling webpack watcher.")

# Webpack watcher is only for development, and rely on assets path
# being present (which doesn't in production); so this is using
# __DIR__ to make it expand to source path rather than compiled path.
[
worker(
Watcher,
Expand Down
2 changes: 1 addition & 1 deletion apps/admin_panel/lib/admin_panel/endpoint.ex
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ defmodule AdminPanel.Endpoint do
Plug.Static,
at: "/admin",
from: {:admin_panel, "priv/static"},
gzip: false
gzip: true
)

# Code reloading can be explicitly enabled under the
Expand Down
3 changes: 2 additions & 1 deletion apps/ewallet/lib/ewallet/adapters/adapter_helper.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ defmodule EWallet.AdapterHelper do
alias EWallet.Exporter
alias EWalletDB.{Repo, Export, Uploaders}
alias EWalletConfig.{FileStorageSupervisor, Storage.Local}
alias Utils.Helpers.PathResolver

@rows_count 500
@timeout_milliseconds 1 * 60 * 60 * 1000
Expand Down Expand Up @@ -80,7 +81,7 @@ defmodule EWallet.AdapterHelper do

def local_dir do
[
Application.get_env(:ewallet, :root),
PathResolver.static_dir(:url_dispatcher),
Uploaders.File.storage_dir(nil, nil)
]
|> Path.join()
Expand Down
6 changes: 3 additions & 3 deletions apps/ewallet/lib/ewallet/cli.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ defmodule EWallet.CLI do
"""
import IO
import IO.ANSI
alias EWallet.Helper
alias IO.ANSI.Docs
alias Utils.Helpers.Normalize

@yes_params ["-y", "--yes", "--assume_yes"]

Expand Down Expand Up @@ -57,13 +57,13 @@ defmodule EWallet.CLI do
# Returns the given fallback if the input is an empty string.
defp confirmed?("", fallback), do: fallback

defp confirmed?(input, _), do: Helper.to_boolean(input)
defp confirmed?(input, _), do: Normalize.to_boolean(input)

@spec configure_logger() :: :ok
def configure_logger do
"DEBUG"
|> System.get_env()
|> Helper.to_boolean()
|> Normalize.to_boolean()
|> case do
true -> Logger.configure(level: :debug)
false -> Logger.configure(level: :warn)
Expand Down
12 changes: 0 additions & 12 deletions apps/ewallet/lib/ewallet/helper.ex
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,6 @@ defmodule EWallet.Helper do
ArgumentError -> atom_list
end

def to_boolean(s) when is_boolean(s), do: s
def to_boolean(s) when is_binary(s), do: string_to_boolean(s)
def to_boolean(s) when is_integer(s) and s >= 1, do: true
def to_boolean(_), do: false

def string_to_integer(string) do
case Integer.parse(string, 10) do
{amount, ""} ->
Expand Down Expand Up @@ -75,13 +70,6 @@ defmodule EWallet.Helper do
end
end

def string_to_boolean(<<"T", _::binary>>), do: true
def string_to_boolean(<<"Y", _::binary>>), do: true
def string_to_boolean(<<"t", _::binary>>), do: true
def string_to_boolean(<<"y", _::binary>>), do: true
def string_to_boolean(<<"1", _::binary>>), do: true
def string_to_boolean(_), do: false

@doc """
Checks if all `elements` exist within the `enumerable`.
Expand Down
32 changes: 23 additions & 9 deletions apps/ewallet/test/ewallet/adapters/adapter_helper_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ defmodule EWallet.AdapterHelperTest do
alias EWallet.AdapterHelper
alias EWallet.Web.V1.CSV.TransactionSerializer
alias EWalletDB.{Export, Transaction}
alias Ecto.UUID
alias Utils.Helper.PidHelper
alias Utils.Helpers.PathResolver

setup do
# Insert transactions with a newly inserted token to avoid side effects.
Expand Down Expand Up @@ -50,18 +52,34 @@ defmodule EWallet.AdapterHelperTest do

query = from(t in Transaction, where: t.to_token_uuid == ^token.uuid)

# File things
root = PathResolver.static_dir(:url_dispatcher)
uuid = UUID.generate()
path = Path.join(["private", uuid])
path_abs = Path.join([root, path])

:ok = File.mkdir_p!(path_abs)

on_exit(fn ->
_ = File.rm_rf!(path_abs)
end)

%{
export: export,
query: query,
serializer: serializer,
transactions: transactions,
chunk_size: chunk_size
chunk_size: chunk_size,
root: root,
uuid: uuid,
path: path,
path_abs: path_abs
}
end

describe "stream_to_file/5" do
test "streams the data to the given file path", context do
path = test_file_path("test-stream-to-file-#{:rand.uniform(999_999)}.txt")
path = Path.join(context.path_abs, "test-stream-to-file-#{:rand.uniform(999_999)}.txt")

refute File.exists?(path)

Expand All @@ -77,9 +95,6 @@ defmodule EWallet.AdapterHelperTest do
assert res == :ok
assert result == :ok
assert File.exists?(path)

# Clean up the created file after testing
:ok = File.rm(path)
end
end

Expand All @@ -105,17 +120,16 @@ defmodule EWallet.AdapterHelperTest do
end

describe "local_dir/0" do
test "returns a string starting with the root dir" do
test "returns a string containing url_dispatcher path" do
path = AdapterHelper.local_dir()
assert String.starts_with?(path, Application.get_env(:ewallet, :root))
assert String.match?(path, ~r/^.+\/url_dispatcher\/priv\/static\//)
end
end

describe "build_local_path/1" do
test "returns a string starting with the root dir and ends with the given file name" do
path = AdapterHelper.build_local_path("local_file_name.txt")
assert String.starts_with?(path, Application.get_env(:ewallet, :root))
assert String.ends_with?(path, "local_file_name.txt")
assert String.match?(path, ~r/^.+\/url_dispatcher\/priv\/static\/.*\/local_file_name\.txt$/)
end
end

Expand Down
51 changes: 0 additions & 51 deletions apps/ewallet/test/ewallet/helper_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -28,37 +28,6 @@ defmodule EWallet.HelperTest do
end
end

describe "to_boolean/1" do
test "converts strings to boolean" do
assert Helper.to_boolean("yes")
assert Helper.to_boolean("Yes")
assert Helper.to_boolean("true")
assert Helper.to_boolean("True")
assert Helper.to_boolean("yup")
assert Helper.to_boolean("yo")
assert Helper.to_boolean("yawn")
assert Helper.to_boolean("1")
refute Helper.to_boolean("nope")
refute Helper.to_boolean("no")
refute Helper.to_boolean("false")
refute Helper.to_boolean("0")
end

test "converts boolean to boolean" do
assert Helper.to_boolean(true)
refute Helper.to_boolean(false)
end

test "converts integer to boolean" do
assert Helper.to_boolean(1)
assert Helper.to_boolean(2)
assert Helper.to_boolean(65_535)
assert Helper.to_boolean(99_999)
refute Helper.to_boolean(0)
refute Helper.to_boolean(-1)
end
end

describe "members?/2" do
test "returns true if all elements exist in the enumerable" do
assert Helper.members?([1, 2, 3, 4, 5], [3, 4])
Expand All @@ -68,24 +37,4 @@ defmodule EWallet.HelperTest do
refute Helper.members?([3, 4], [1, 2, 3, 4, 5])
end
end

describe "string_to_boolean/1" do
test "converts strings to boolean" do
assert Helper.string_to_boolean("yes")
assert Helper.string_to_boolean("Yes")
assert Helper.string_to_boolean("true")
assert Helper.string_to_boolean("True")
assert Helper.string_to_boolean("yup")
assert Helper.string_to_boolean("yo")
assert Helper.string_to_boolean("yawn")
assert Helper.string_to_boolean("1")
refute Helper.string_to_boolean("nope")
refute Helper.string_to_boolean("no")
refute Helper.string_to_boolean("false")
refute Helper.string_to_boolean("0")
refute Helper.string_to_boolean(1)
refute Helper.string_to_boolean(true)
refute Helper.string_to_boolean(false)
end
end
end
Loading

0 comments on commit 0b3a197

Please sign in to comment.