static-fe overhaul (#236)
[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 # These modules are only being used as keys here (for equality check),
7 # so it's okay to use `Module.concat/1` to have the compiler ignore them.
8 @reject_keys [
9 Module.concat(["Pleroma.Repo"]),
10 Module.concat(["Pleroma.Web.Endpoint"]),
11 :env,
12 :configurable_from_database,
13 :database,
14 :swarm
15 ]
16
17 @reject_groups [
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