From ee571b19a758f73e511e804472a21ffbf32fcb76 Mon Sep 17 00:00:00 2001 From: Chris McCord Date: Tue, 16 Nov 2021 22:11:11 -0500 Subject: [PATCH] Allow configurable upload dir per env --- config/dev.exs | 13 ++++++------- config/runtime.exs | 13 ++++++------- fly.toml | 2 +- lib/live_beats.ex | 22 ++++++++++++++++++++++ lib/live_beats/accounts.ex | 2 +- lib/live_beats/github.ex | 4 ++-- lib/live_beats/media_library.ex | 3 ++- lib/live_beats/media_library/song.ex | 2 +- 8 files changed, 41 insertions(+), 20 deletions(-) diff --git a/config/dev.exs b/config/dev.exs index a906d8a..8d20908 100644 --- a/config/dev.exs +++ b/config/dev.exs @@ -1,15 +1,14 @@ import Config -config :live_beats, :file_host, %{ - scheme: "http", - host: "localhost", - port: 4000 -} +config :live_beats, :files, [ + uploads_dir: Path.expand("../priv/uploads", __DIR__), + host: [scheme: "http", host: "localhost", port: 4000], +] -config :live_beats, :github, %{ +config :live_beats, :github, [ client_id: System.fetch_env!("LIVE_BEATS_GITHUB_CLIENT_ID"), client_secret: System.fetch_env!("LIVE_BEATS_GITHUB_CLIENT_SECRET"), -} +] # Configure your database config :live_beats, LiveBeats.Repo, diff --git a/config/runtime.exs b/config/runtime.exs index 7704f4f..c0200f6 100644 --- a/config/runtime.exs +++ b/config/runtime.exs @@ -45,14 +45,13 @@ if config_env() == :prod do secret_key_base: secret_key_base, server: true - config :live_beats, :file_host, %{ - scheme: "http", - host: host, - port: 80 - } + config :live_beats, :files, [ + uploads_dir: "/app/uploads", + host: [scheme: "https", host: host, port: 443], + ] - config :live_beats, :github, %{ + config :live_beats, :github, [ client_id: System.fetch_env!("LIVE_BEATS_GITHUB_CLIENT_ID"), client_secret: System.fetch_env!("LIVE_BEATS_GITHUB_CLIENT_SECRET"), - } + ] end diff --git a/fly.toml b/fly.toml index 1fe5bc7..e9addac 100644 --- a/fly.toml +++ b/fly.toml @@ -13,7 +13,7 @@ processes = [] [mounts] source="data" - destination="/app/live_beats/lib/live_beats-0.1.0/priv/uploads" + destination="/app/uploads" [experimental] allowed_public_ports = [] diff --git a/lib/live_beats.ex b/lib/live_beats.ex index 67aea0c..a71ffc5 100644 --- a/lib/live_beats.ex +++ b/lib/live_beats.ex @@ -4,6 +4,28 @@ defmodule LiveBeats do """ 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 """ Attaches a modules to another for listening of events. diff --git a/lib/live_beats/accounts.ex b/lib/live_beats/accounts.ex index 50efa35..9b3070e 100644 --- a/lib/live_beats/accounts.ex +++ b/lib/live_beats/accounts.ex @@ -28,7 +28,7 @@ defmodule LiveBeats.Accounts do end def admin?(%User{} = user) do - user.email in Application.fetch_env!(:live_beats, :admin_emails) + user.email in LiveBeats.config([:admin_emails]) end @doc """ diff --git a/lib/live_beats/github.ex b/lib/live_beats/github.ex index 168e83e..85a9a26 100644 --- a/lib/live_beats/github.ex +++ b/lib/live_beats/github.ex @@ -81,8 +81,8 @@ defmodule LiveBeats.Github do |> String.replace(["/", "+"], "-") end - defp client_id, do: Application.fetch_env!(:live_beats, :github)[:client_id] - defp secret, do: Application.fetch_env!(:live_beats, :github)[:client_secret] + defp client_id, do: LiveBeats.config([:github, :client_id]) + defp secret, do: LiveBeats.config([:github, :client_secret]) defp http(host, method, path, query, headers, body \\ "") do {:ok, conn} = Mint.HTTP.connect(:https, host, 443) diff --git a/lib/live_beats/media_library.ex b/lib/live_beats/media_library.ex index 46fbace..d38a68e 100644 --- a/lib/live_beats/media_library.ex +++ b/lib/live_beats/media_library.ex @@ -34,7 +34,8 @@ defmodule LiveBeats.MediaLibrary do end 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 def can_control_playback?(%Accounts.User{} = user, %Song{} = song) do diff --git a/lib/live_beats/media_library/song.ex b/lib/live_beats/media_library/song.ex index 0780bc2..9caff71 100644 --- a/lib/live_beats/media_library/song.ex +++ b/lib/live_beats/media_library/song.ex @@ -65,7 +65,7 @@ defmodule LiveBeats.MediaLibrary.Song do end 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}"}) end end