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