This commit is contained in:
byte[] 2020-10-26 23:03:08 -04:00
parent fb39379051
commit 495396206e
9 changed files with 28 additions and 67 deletions

View file

@ -34,8 +34,24 @@ config :philomena,
proxy_host: System.get_env("PROXY_HOST"),
camo_host: System.get_env("CAMO_HOST"),
camo_key: System.get_env("CAMO_KEY"),
cdn_host: System.fetch_env!("CDN_HOST"),
app_dir: System.get_env("APP_DIR", File.cwd!())
cdn_host: System.fetch_env!("CDN_HOST")
app_dir = System.get_env("APP_DIR", File.cwd!())
json_config =
%{
aggregation: "aggregation.json",
avatar: "avatar.json",
footer: "footer.json",
quick_tag_table: "quick_tag_table.json",
tag: "tag.json"
}
|> Map.new(fn {name, file} ->
{name, Jason.decode!(File.read!("#{app_dir}/config/#{file}"))}
end)
config :philomena,
config: json_config
config :exq,
host: System.get_env("REDIS_HOST", "localhost"),

View file

@ -16,7 +16,6 @@ defmodule Philomena.Application do
# Starts a worker by calling: Philomena.Worker.start_link(arg)
# {Philomena.Worker, arg},
Philomena.Servers.Config,
{Redix, name: :redix, host: Application.get_env(:philomena, :redis_host)},
{Phoenix.PubSub,
[

5
lib/philomena/config.ex Normal file
View file

@ -0,0 +1,5 @@
defmodule Philomena.Config do
def get(key) do
Application.get_env(:philomena, :config)[key]
end
end

View file

@ -1,5 +1,5 @@
defmodule Philomena.Images.TagValidator do
alias Philomena.Servers.Config
alias Philomena.Config
import Ecto.Changeset
def validate_tags(changeset) do

View file

@ -1,59 +0,0 @@
defmodule Philomena.Servers.Config do
use GenServer
@process_name :philomena_config
def start_link([]) do
GenServer.start_link(__MODULE__, [])
end
def get(key) do
pid = Process.whereis(@process_name)
GenServer.call(pid, {:get, key})
end
def reload do
pid = Process.whereis(@process_name)
GenServer.cast(pid, :reload)
end
@impl true
def init([]) do
Process.register(self(), @process_name)
{:ok, %{}}
end
@impl true
def handle_call({:get, key}, _from, state) do
state = maybe_update_state(state, key, Map.has_key?(state, key))
{:reply, state[key], state}
end
@impl true
def handle_cast(:reload, _state) do
{:noreply, %{}}
end
@impl true
def code_change(_old_vsn, _state, _extra) do
{:ok, %{}}
end
defp maybe_update_state(state, key, false) do
Map.put(state, key, load_config(key))
end
defp maybe_update_state(state, _key, _true), do: state
defp load_config(name) do
with {:ok, text} <- File.read("#{app_dir()}/config/#{name}.json"),
{:ok, json} <- Jason.decode(text) do
json
end
end
defp app_dir do
Application.get_env(:philomena, :app_dir)
end
end

View file

@ -1,6 +1,6 @@
defmodule PhilomenaWeb.StatsUpdater do
alias Philomena.Config
alias Philomena.Elasticsearch
alias Philomena.Servers.Config
alias Philomena.Images.Image
alias Philomena.Comments.Comment
alias Philomena.Topics.Topic

View file

@ -2,7 +2,7 @@ defmodule PhilomenaWeb.AvatarGeneratorView do
use PhilomenaWeb, :view
use Bitwise
alias Philomena.Servers.Config
alias Philomena.Config
def generated_avatar(displayed_name) do
config = config()

View file

@ -2,7 +2,7 @@ defmodule PhilomenaWeb.LayoutView do
use PhilomenaWeb, :view
alias PhilomenaWeb.ImageView
alias Philomena.Servers.Config
alias Philomena.Config
alias Plug.Conn
def layout_class(conn) do

View file

@ -2,7 +2,7 @@ defmodule PhilomenaWeb.TagView do
use PhilomenaWeb, :view
# this is bad practice, don't copy this.
alias Philomena.Servers.Config
alias Philomena.Config
alias Philomena.Elasticsearch
alias Philomena.Tags.Tag
alias Philomena.Repo