dynamic config

This commit is contained in:
byte[] 2019-12-25 11:56:19 -05:00
parent 860de7615f
commit 0981a1ba0b
6 changed files with 68 additions and 22 deletions

View file

@ -28,10 +28,7 @@ config :philomena,
channel_banner_file_root: "priv/static/system/images", channel_banner_file_root: "priv/static/system/images",
tag_file_root: "priv/static/system/images", tag_file_root: "priv/static/system/images",
cdn_host: "", cdn_host: "",
proxy_host: nil, 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")
config :philomena, :pow, config :philomena, :pow,
user: Philomena.Users.User, user: Philomena.Users.User,

View file

@ -10,8 +10,7 @@ defmodule Philomena.Application do
children = [ children = [
# Start the Ecto repository # Start the Ecto repository
Philomena.Repo, Philomena.Repo,
# Start the endpoint when the application starts
PhilomenaWeb.Endpoint,
# Starts a worker by calling: Philomena.Worker.start_link(arg) # Starts a worker by calling: Philomena.Worker.start_link(arg)
# {Philomena.Worker, arg}, # {Philomena.Worker, arg},
Philomena.Servers.ImageProcessor, Philomena.Servers.ImageProcessor,
@ -20,8 +19,12 @@ defmodule Philomena.Application do
Philomena.Servers.PiczelChannelUpdater, Philomena.Servers.PiczelChannelUpdater,
Philomena.Servers.UserFingerprintUpdater, Philomena.Servers.UserFingerprintUpdater,
Philomena.Servers.UserIpUpdater, Philomena.Servers.UserIpUpdater,
Philomena.Servers.Config,
Pow.Store.Backend.MnesiaCache, 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 # See https://hexdocs.pm/elixir/Supervisor.html

View file

@ -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

View file

@ -2,6 +2,7 @@ defmodule PhilomenaWeb.StatController do
use PhilomenaWeb, :controller use PhilomenaWeb, :controller
alias Philomena.Elasticsearch alias Philomena.Elasticsearch
alias Philomena.Servers.Config
alias Philomena.Images.Image alias Philomena.Images.Image
alias Philomena.Comments.Comment alias Philomena.Comments.Comment
alias Philomena.Topics.Topic alias Philomena.Topics.Topic
@ -48,9 +49,7 @@ defmodule PhilomenaWeb.StatController do
end end
defp aggregations do defp aggregations do
data = data = Config.get(:aggregation)
Application.get_env(:philomena, :aggregation_json)
|> Jason.decode!()
{ {
Elasticsearch.search(Image, data["images"]), Elasticsearch.search(Image, data["images"]),

View file

@ -2,6 +2,7 @@ defmodule PhilomenaWeb.LayoutView do
use PhilomenaWeb, :view use PhilomenaWeb, :view
alias PhilomenaWeb.ImageView alias PhilomenaWeb.ImageView
alias Philomena.Servers.Config
alias Plug.Conn alias Plug.Conn
def layout_class(conn) do def layout_class(conn) do
@ -62,16 +63,7 @@ defmodule PhilomenaWeb.LayoutView do
end end
def footer_data do def footer_data do
case Application.get_env(:philomena, :footer) do Config.get(:footer)
nil ->
footer = Jason.decode!(Application.get_env(:philomena, :footer_json))
Application.put_env(:philomena, :footer, footer)
footer
footer ->
footer
end
end end
def stylesheet_path(conn, %{theme: "dark"}), def stylesheet_path(conn, %{theme: "dark"}),

View file

@ -2,6 +2,7 @@ defmodule PhilomenaWeb.TagView do
use PhilomenaWeb, :view use PhilomenaWeb, :view
# this is bad practice, don't copy this. # this is bad practice, don't copy this.
alias Philomena.Servers.Config
alias Philomena.Elasticsearch alias Philomena.Elasticsearch
alias Philomena.Tags.Tag alias Philomena.Tags.Tag
alias Philomena.Repo alias Philomena.Repo
@ -27,8 +28,7 @@ defmodule PhilomenaWeb.TagView do
case Application.get_env(:philomena, :quick_tags) do case Application.get_env(:philomena, :quick_tags) do
nil -> nil ->
quick_tags = quick_tags =
Application.get_env(:philomena, :quick_tags_json) Config.get(:quick_tag_table)
|> Jason.decode!()
|> lookup_quick_tags() |> lookup_quick_tags()
|> render_quick_tags(conn) |> render_quick_tags(conn)