From 495396206e868af3337aaee42e013c2aff4f0108 Mon Sep 17 00:00:00 2001 From: "byte[]" Date: Mon, 26 Oct 2020 23:03:08 -0400 Subject: [PATCH] remove config server (philomena-dev/philomena#56) --- config/runtime.exs | 20 ++++++- lib/philomena/application.ex | 1 - lib/philomena/config.ex | 5 ++ lib/philomena/images/tag_validator.ex | 2 +- lib/philomena/servers/config.ex | 59 ------------------- lib/philomena_web/stats_updater.ex | 2 +- .../views/avatar_generator_view.ex | 2 +- lib/philomena_web/views/layout_view.ex | 2 +- lib/philomena_web/views/tag_view.ex | 2 +- 9 files changed, 28 insertions(+), 67 deletions(-) create mode 100644 lib/philomena/config.ex delete mode 100644 lib/philomena/servers/config.ex diff --git a/config/runtime.exs b/config/runtime.exs index 65fc4d90..d097f3ea 100644 --- a/config/runtime.exs +++ b/config/runtime.exs @@ -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"), diff --git a/lib/philomena/application.ex b/lib/philomena/application.ex index f26f2023..e3a99861 100644 --- a/lib/philomena/application.ex +++ b/lib/philomena/application.ex @@ -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, [ diff --git a/lib/philomena/config.ex b/lib/philomena/config.ex new file mode 100644 index 00000000..a6a35bd3 --- /dev/null +++ b/lib/philomena/config.ex @@ -0,0 +1,5 @@ +defmodule Philomena.Config do + def get(key) do + Application.get_env(:philomena, :config)[key] + end +end diff --git a/lib/philomena/images/tag_validator.ex b/lib/philomena/images/tag_validator.ex index 6ffafbac..887b5daa 100644 --- a/lib/philomena/images/tag_validator.ex +++ b/lib/philomena/images/tag_validator.ex @@ -1,5 +1,5 @@ defmodule Philomena.Images.TagValidator do - alias Philomena.Servers.Config + alias Philomena.Config import Ecto.Changeset def validate_tags(changeset) do diff --git a/lib/philomena/servers/config.ex b/lib/philomena/servers/config.ex deleted file mode 100644 index 58455d40..00000000 --- a/lib/philomena/servers/config.ex +++ /dev/null @@ -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 diff --git a/lib/philomena_web/stats_updater.ex b/lib/philomena_web/stats_updater.ex index 11054ab4..e4bd4e17 100644 --- a/lib/philomena_web/stats_updater.ex +++ b/lib/philomena_web/stats_updater.ex @@ -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 diff --git a/lib/philomena_web/views/avatar_generator_view.ex b/lib/philomena_web/views/avatar_generator_view.ex index 14f6ff4d..3ed0964e 100644 --- a/lib/philomena_web/views/avatar_generator_view.ex +++ b/lib/philomena_web/views/avatar_generator_view.ex @@ -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() diff --git a/lib/philomena_web/views/layout_view.ex b/lib/philomena_web/views/layout_view.ex index fd50d87b..aec16af8 100644 --- a/lib/philomena_web/views/layout_view.ex +++ b/lib/philomena_web/views/layout_view.ex @@ -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 diff --git a/lib/philomena_web/views/tag_view.ex b/lib/philomena_web/views/tag_view.ex index 4c3ba759..dd0b12eb 100644 --- a/lib/philomena_web/views/tag_view.ex +++ b/lib/philomena_web/views/tag_view.ex @@ -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