MRF: create MRF.Policy behaviour separate from MRF module
[akkoma] / lib / pleroma / config / loader.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Config.Loader do
6 defp reject_keys,
7 do: [
8 Pleroma.Repo,
9 Pleroma.Web.Endpoint,
10 :env,
11 :configurable_from_database,
12 :database,
13 :swarm
14 ]
15
16 defp reject_groups,
17 do: [
18 :postgrex,
19 :tesla
20 ]
21
22 if Code.ensure_loaded?(Config.Reader) do
23 @reader Config.Reader
24
25 def read(path), do: @reader.read!(path)
26 else
27 # support for Elixir less than 1.9
28 @reader Mix.Config
29 def read(path) do
30 path
31 |> @reader.eval!()
32 |> elem(0)
33 end
34 end
35
36 @spec read(Path.t()) :: keyword()
37
38 @spec merge(keyword(), keyword()) :: keyword()
39 def merge(c1, c2), do: @reader.merge(c1, c2)
40
41 @spec default_config() :: keyword()
42 def default_config do
43 "config/config.exs"
44 |> read()
45 |> filter()
46 end
47
48 defp filter(configs) do
49 configs
50 |> Keyword.keys()
51 |> Enum.reduce([], &Keyword.put(&2, &1, filter_group(&1, configs)))
52 end
53
54 @spec filter_group(atom(), keyword()) :: keyword()
55 def filter_group(group, configs) do
56 Enum.reject(configs[group], fn {key, _v} ->
57 key in reject_keys() or group in reject_groups() or
58 (group == :phoenix and key == :serve_endpoints)
59 end)
60 end
61 end