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",
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,

View file

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

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
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"]),

View file

@ -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"}),

View file

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