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