From 0981a1ba0b9b85943a4fab6a15e96a2d4a1391a2 Mon Sep 17 00:00:00 2001 From: "byte[]" Date: Wed, 25 Dec 2019 11:56:19 -0500 Subject: [PATCH] dynamic config --- config/config.exs | 5 +- lib/philomena/application.ex | 9 ++- lib/philomena/servers/config.ex | 55 +++++++++++++++++++ .../controllers/stat_controller.ex | 5 +- lib/philomena_web/views/layout_view.ex | 12 +--- lib/philomena_web/views/tag_view.ex | 4 +- 6 files changed, 68 insertions(+), 22 deletions(-) create mode 100644 lib/philomena/servers/config.ex diff --git a/config/config.exs b/config/config.exs index 8c3c3b13..869f0cff 100644 --- a/config/config.exs +++ b/config/config.exs @@ -28,10 +28,7 @@ config :philomena, channel_banner_file_root: "priv/static/system/images", tag_file_root: "priv/static/system/images", cdn_host: "", - proxy_host: nil, - quick_tags_json: File.read!("config/quick_tag_table.json"), - aggregation_json: File.read!("config/aggregation.json"), - footer_json: File.read!("config/footer.json") + proxy_host: nil config :philomena, :pow, user: Philomena.Users.User, diff --git a/lib/philomena/application.ex b/lib/philomena/application.ex index 524a98dd..9a600752 100644 --- a/lib/philomena/application.ex +++ b/lib/philomena/application.ex @@ -10,8 +10,7 @@ defmodule Philomena.Application do children = [ # Start the Ecto repository Philomena.Repo, - # Start the endpoint when the application starts - PhilomenaWeb.Endpoint, + # Starts a worker by calling: Philomena.Worker.start_link(arg) # {Philomena.Worker, arg}, Philomena.Servers.ImageProcessor, @@ -20,8 +19,12 @@ defmodule Philomena.Application do Philomena.Servers.PiczelChannelUpdater, Philomena.Servers.UserFingerprintUpdater, Philomena.Servers.UserIpUpdater, + Philomena.Servers.Config, Pow.Store.Backend.MnesiaCache, - {Redix, name: :redix, host: Application.get_env(:philomena, :redis_host)} + {Redix, name: :redix, host: Application.get_env(:philomena, :redis_host)}, + + # Start the endpoint when the application starts + PhilomenaWeb.Endpoint ] # See https://hexdocs.pm/elixir/Supervisor.html diff --git a/lib/philomena/servers/config.ex b/lib/philomena/servers/config.ex new file mode 100644 index 00000000..c4dc1108 --- /dev/null +++ b/lib/philomena/servers/config.ex @@ -0,0 +1,55 @@ +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("config/#{name}.json"), + {:ok, json} <- Jason.decode(text) + do + json + end + end +end diff --git a/lib/philomena_web/controllers/stat_controller.ex b/lib/philomena_web/controllers/stat_controller.ex index de2c7600..6c024613 100644 --- a/lib/philomena_web/controllers/stat_controller.ex +++ b/lib/philomena_web/controllers/stat_controller.ex @@ -2,6 +2,7 @@ defmodule PhilomenaWeb.StatController do use PhilomenaWeb, :controller alias Philomena.Elasticsearch + alias Philomena.Servers.Config alias Philomena.Images.Image alias Philomena.Comments.Comment alias Philomena.Topics.Topic @@ -48,9 +49,7 @@ defmodule PhilomenaWeb.StatController do end defp aggregations do - data = - Application.get_env(:philomena, :aggregation_json) - |> Jason.decode!() + data = Config.get(:aggregation) { Elasticsearch.search(Image, data["images"]), diff --git a/lib/philomena_web/views/layout_view.ex b/lib/philomena_web/views/layout_view.ex index 1d8105f3..dad5db50 100644 --- a/lib/philomena_web/views/layout_view.ex +++ b/lib/philomena_web/views/layout_view.ex @@ -2,6 +2,7 @@ defmodule PhilomenaWeb.LayoutView do use PhilomenaWeb, :view alias PhilomenaWeb.ImageView + alias Philomena.Servers.Config alias Plug.Conn def layout_class(conn) do @@ -62,16 +63,7 @@ defmodule PhilomenaWeb.LayoutView do end def footer_data do - case Application.get_env(:philomena, :footer) do - nil -> - footer = Jason.decode!(Application.get_env(:philomena, :footer_json)) - Application.put_env(:philomena, :footer, footer) - - footer - - footer -> - footer - end + Config.get(:footer) end def stylesheet_path(conn, %{theme: "dark"}), diff --git a/lib/philomena_web/views/tag_view.ex b/lib/philomena_web/views/tag_view.ex index 03540725..cfd3dd46 100644 --- a/lib/philomena_web/views/tag_view.ex +++ b/lib/philomena_web/views/tag_view.ex @@ -2,6 +2,7 @@ defmodule PhilomenaWeb.TagView do use PhilomenaWeb, :view # this is bad practice, don't copy this. + alias Philomena.Servers.Config alias Philomena.Elasticsearch alias Philomena.Tags.Tag alias Philomena.Repo @@ -27,8 +28,7 @@ defmodule PhilomenaWeb.TagView do case Application.get_env(:philomena, :quick_tags) do nil -> quick_tags = - Application.get_env(:philomena, :quick_tags_json) - |> Jason.decode!() + Config.get(:quick_tag_table) |> lookup_quick_tags() |> render_quick_tags(conn)