Merge branch 'develop' of https://git.pleroma.social/pleroma/pleroma into develop
[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 if Code.ensure_loaded?(Config.Reader) do
16 @spec load(Path.t()) :: keyword()
17 def load(path), do: Config.Reader.read!(path)
18
19 defp do_merge(conf1, conf2), do: Config.Reader.merge(conf1, conf2)
20 else
21 # support for Elixir less than 1.9
22 @spec load(Path.t()) :: keyword()
23 def load(path) do
24 path
25 |> Mix.Config.eval!()
26 |> elem(0)
27 end
28
29 defp do_merge(conf1, conf2), do: Mix.Config.merge(conf1, conf2)
30 end
31
32 @spec load_and_merge() :: keyword()
33 def load_and_merge do
34 all_paths =
35 if Pleroma.Config.get(:release),
36 do: ["config/config.exs", "config/releases.exs"],
37 else: ["config/config.exs"]
38
39 all_paths
40 |> Enum.map(&load(&1))
41 |> Enum.reduce([], &do_merge(&2, &1))
42 |> filter()
43 end
44
45 defp filter(configs) do
46 configs
47 |> Keyword.keys()
48 |> Enum.reduce([], &Keyword.put(&2, &1, filter_group(&1, configs)))
49 end
50
51 @spec filter_group(atom(), keyword()) :: keyword()
52 def filter_group(group, configs) do
53 Enum.reject(configs[group], fn {key, _v} ->
54 key in @reject_keys or (group == :phoenix and key == :serve_endpoints)
55 end)
56 end
57 end