Pleroma/lib/pleroma/config/loader.ex

62 lines
1.3 KiB
Elixir
Raw Normal View History

2020-01-21 15:49:22 +01:00
# Pleroma: A lightweight social networking server
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
2020-01-21 15:49:22 +01:00
# SPDX-License-Identifier: AGPL-3.0-only
2020-01-18 14:55:33 +01:00
defmodule Pleroma.Config.Loader do
defp reject_keys,
do: [
Pleroma.Repo,
Pleroma.Web.Endpoint,
:env,
:configurable_from_database,
:database,
:swarm
]
defp reject_groups,
do: [
:postgrex,
:tesla
]
2020-07-06 07:48:20 +02:00
2020-01-18 14:55:33 +01:00
if Code.ensure_loaded?(Config.Reader) do
@reader Config.Reader
2020-03-11 14:43:58 +01:00
def read(path), do: @reader.read!(path)
2020-01-18 14:55:33 +01:00
else
# support for Elixir less than 1.9
@reader Mix.Config
2020-03-11 14:43:58 +01:00
def read(path) do
path
|> @reader.eval!()
|> elem(0)
end
2020-01-18 14:55:33 +01:00
end
@spec read(Path.t()) :: keyword()
@spec merge(keyword(), keyword()) :: keyword()
def merge(c1, c2), do: @reader.merge(c1, c2)
2020-01-18 14:55:33 +01:00
@spec default_config() :: keyword()
def default_config do
"config/config.exs"
|> read()
2020-01-21 15:49:22 +01:00
|> filter()
2020-01-18 14:55:33 +01:00
end
2020-01-21 15:49:22 +01:00
defp filter(configs) do
configs
|> Keyword.keys()
|> Enum.reduce([], &Keyword.put(&2, &1, filter_group(&1, configs)))
end
2020-01-18 14:55:33 +01:00
2020-01-21 15:49:22 +01:00
@spec filter_group(atom(), keyword()) :: keyword()
def filter_group(group, configs) do
Enum.reject(configs[group], fn {key, _v} ->
key in reject_keys() or group in reject_groups() or
2020-07-06 07:48:20 +02:00
(group == :phoenix and key == :serve_endpoints)
2020-01-21 15:49:22 +01:00
end)
2020-01-18 14:55:33 +01:00
end
end