Allow configurable upload dir per env

This commit is contained in:
Chris McCord 2021-11-16 22:11:11 -05:00
parent 2f4940fab6
commit ee571b19a7
8 changed files with 41 additions and 20 deletions

View file

@ -1,15 +1,14 @@
import Config import Config
config :live_beats, :file_host, %{ config :live_beats, :files, [
scheme: "http", uploads_dir: Path.expand("../priv/uploads", __DIR__),
host: "localhost", host: [scheme: "http", host: "localhost", port: 4000],
port: 4000 ]
}
config :live_beats, :github, %{ config :live_beats, :github, [
client_id: System.fetch_env!("LIVE_BEATS_GITHUB_CLIENT_ID"), client_id: System.fetch_env!("LIVE_BEATS_GITHUB_CLIENT_ID"),
client_secret: System.fetch_env!("LIVE_BEATS_GITHUB_CLIENT_SECRET"), client_secret: System.fetch_env!("LIVE_BEATS_GITHUB_CLIENT_SECRET"),
} ]
# Configure your database # Configure your database
config :live_beats, LiveBeats.Repo, config :live_beats, LiveBeats.Repo,

View file

@ -45,14 +45,13 @@ if config_env() == :prod do
secret_key_base: secret_key_base, secret_key_base: secret_key_base,
server: true server: true
config :live_beats, :file_host, %{ config :live_beats, :files, [
scheme: "http", uploads_dir: "/app/uploads",
host: host, host: [scheme: "https", host: host, port: 443],
port: 80 ]
}
config :live_beats, :github, %{ config :live_beats, :github, [
client_id: System.fetch_env!("LIVE_BEATS_GITHUB_CLIENT_ID"), client_id: System.fetch_env!("LIVE_BEATS_GITHUB_CLIENT_ID"),
client_secret: System.fetch_env!("LIVE_BEATS_GITHUB_CLIENT_SECRET"), client_secret: System.fetch_env!("LIVE_BEATS_GITHUB_CLIENT_SECRET"),
} ]
end end

View file

@ -13,7 +13,7 @@ processes = []
[mounts] [mounts]
source="data" source="data"
destination="/app/live_beats/lib/live_beats-0.1.0/priv/uploads" destination="/app/uploads"
[experimental] [experimental]
allowed_public_ports = [] allowed_public_ports = []

View file

@ -4,6 +4,28 @@ defmodule LiveBeats do
""" """
require Logger require Logger
@doc """
Looks up `Application` config or raises if keyspace is not configured.
## Examples
config :live_beats, :files, [
uploads_dir: Path.expand("../priv/uploads", __DIR__),
host: [scheme: "http", host: "localhost", port: 4000],
]
iex> LiveBeats.config([:files, :uploads_dir])
iex> LiveBeats.config([:files, :host, :port])
"""
def config([main_key | rest] = keyspace) when is_list(keyspace) do
main = Application.fetch_env!(:live_beats, main_key)
Enum.reduce(rest, main, fn next_key, current ->
case Keyword.fetch(current, next_key) do
{:ok, val} -> val
:error -> raise ArgumentError, "no config found under #{inspect(keyspace)}"
end
end)
end
@doc """ @doc """
Attaches a modules to another for listening of events. Attaches a modules to another for listening of events.

View file

@ -28,7 +28,7 @@ defmodule LiveBeats.Accounts do
end end
def admin?(%User{} = user) do def admin?(%User{} = user) do
user.email in Application.fetch_env!(:live_beats, :admin_emails) user.email in LiveBeats.config([:admin_emails])
end end
@doc """ @doc """

View file

@ -81,8 +81,8 @@ defmodule LiveBeats.Github do
|> String.replace(["/", "+"], "-") |> String.replace(["/", "+"], "-")
end end
defp client_id, do: Application.fetch_env!(:live_beats, :github)[:client_id] defp client_id, do: LiveBeats.config([:github, :client_id])
defp secret, do: Application.fetch_env!(:live_beats, :github)[:client_secret] defp secret, do: LiveBeats.config([:github, :client_secret])
defp http(host, method, path, query, headers, body \\ "") do defp http(host, method, path, query, headers, body \\ "") do
{:ok, conn} = Mint.HTTP.connect(:https, host, 443) {:ok, conn} = Mint.HTTP.connect(:https, host, 443)

View file

@ -34,7 +34,8 @@ defmodule LiveBeats.MediaLibrary do
end end
def local_filepath(filename_uuid) when is_binary(filename_uuid) do def local_filepath(filename_uuid) when is_binary(filename_uuid) do
Path.join("priv/uploads/songs", filename_uuid) dir = LiveBeats.config([:files, :uploads_dir])
Path.join([dir, "songs", filename_uuid])
end end
def can_control_playback?(%Accounts.User{} = user, %Song{} = song) do def can_control_playback?(%Accounts.User{} = user, %Song{} = song) do

View file

@ -65,7 +65,7 @@ defmodule LiveBeats.MediaLibrary.Song do
end end
defp mp3_url(filename) do defp mp3_url(filename) do
%{scheme: scheme, host: host, port: port} = Application.fetch_env!(:live_beats, :file_host) %{scheme: scheme, host: host, port: port} = Enum.into(LiveBeats.config([:files, :host]), %{})
URI.to_string(%URI{scheme: scheme, host: host, port: port, path: "/files/#{filename}"}) URI.to_string(%URI{scheme: scheme, host: host, port: port, path: "/files/#{filename}"})
end end
end end