Merge branch 'develop' into gun
[akkoma] / lib / pleroma / application.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.Application do
6 use Application
7
8 import Cachex.Spec
9
10 alias Pleroma.Config
11
12 require Logger
13
14 @name Mix.Project.config()[:name]
15 @version Mix.Project.config()[:version]
16 @repository Mix.Project.config()[:source_url]
17 @env Mix.env()
18
19 def name, do: @name
20 def version, do: @version
21 def named_version, do: @name <> " " <> @version
22 def repository, do: @repository
23
24 def user_agent do
25 case Config.get([:http, :user_agent], :default) do
26 :default ->
27 info = "#{Pleroma.Web.base_url()} <#{Config.get([:instance, :email], "")}>"
28 named_version() <> "; " <> info
29
30 custom ->
31 custom
32 end
33 end
34
35 # See http://elixir-lang.org/docs/stable/elixir/Application.html
36 # for more information on OTP Applications
37 def start(_type, _args) do
38 Pleroma.Config.Holder.save_default()
39 Pleroma.HTML.compile_scrubbers()
40 Config.DeprecationWarnings.warn()
41 Pleroma.Plugs.HTTPSecurityPlug.warn_if_disabled()
42 Pleroma.Repo.check_migrations_applied!()
43 setup_instrumenters()
44 load_custom_modules()
45
46 adapter = Application.get_env(:tesla, :adapter)
47
48 if adapter == Tesla.Adapter.Gun do
49 if version = Pleroma.OTPVersion.version() do
50 [major, minor] =
51 version
52 |> String.split(".")
53 |> Enum.map(&String.to_integer/1)
54 |> Enum.take(2)
55
56 if (major == 22 and minor < 2) or major < 22 do
57 raise "
58 !!!OTP VERSION WARNING!!!
59 You are using gun adapter with OTP version #{version}, which doesn't support correct handling of unordered certificates chains.
60 "
61 end
62 else
63 raise "
64 !!!OTP VERSION WARNING!!!
65 To support correct handling of unordered certificates chains - OTP version must be > 22.2.
66 "
67 end
68 end
69
70 # Define workers and child supervisors to be supervised
71 children =
72 [
73 Pleroma.Repo,
74 Config.TransferTask,
75 Pleroma.Emoji,
76 Pleroma.Captcha,
77 Pleroma.Plugs.RateLimiter.Supervisor
78 ] ++
79 cachex_children() ++
80 http_children(adapter, @env) ++
81 [
82 Pleroma.Stats,
83 Pleroma.JobQueueMonitor,
84 {Oban, Config.get(Oban)}
85 ] ++
86 task_children(@env) ++
87 streamer_child(@env) ++
88 chat_child(@env, chat_enabled?()) ++
89 [
90 Pleroma.Web.Endpoint,
91 Pleroma.Gopher.Server
92 ]
93
94 # See http://elixir-lang.org/docs/stable/elixir/Supervisor.html
95 # for other strategies and supported options
96 opts = [strategy: :one_for_one, name: Pleroma.Supervisor]
97 Supervisor.start_link(children, opts)
98 end
99
100 def load_custom_modules do
101 dir = Config.get([:modules, :runtime_dir])
102
103 if dir && File.exists?(dir) do
104 dir
105 |> Pleroma.Utils.compile_dir()
106 |> case do
107 {:error, _errors, _warnings} ->
108 raise "Invalid custom modules"
109
110 {:ok, modules, _warnings} ->
111 if @env != :test do
112 Enum.each(modules, fn mod ->
113 Logger.info("Custom module loaded: #{inspect(mod)}")
114 end)
115 end
116
117 :ok
118 end
119 end
120 end
121
122 defp setup_instrumenters do
123 require Prometheus.Registry
124
125 if Application.get_env(:prometheus, Pleroma.Repo.Instrumenter) do
126 :ok =
127 :telemetry.attach(
128 "prometheus-ecto",
129 [:pleroma, :repo, :query],
130 &Pleroma.Repo.Instrumenter.handle_event/4,
131 %{}
132 )
133
134 Pleroma.Repo.Instrumenter.setup()
135 end
136
137 Pleroma.Web.Endpoint.MetricsExporter.setup()
138 Pleroma.Web.Endpoint.PipelineInstrumenter.setup()
139 Pleroma.Web.Endpoint.Instrumenter.setup()
140 end
141
142 defp cachex_children do
143 [
144 build_cachex("used_captcha", ttl_interval: seconds_valid_interval()),
145 build_cachex("user", default_ttl: 25_000, ttl_interval: 1000, limit: 2500),
146 build_cachex("object", default_ttl: 25_000, ttl_interval: 1000, limit: 2500),
147 build_cachex("rich_media", default_ttl: :timer.minutes(120), limit: 5000),
148 build_cachex("scrubber", limit: 2500),
149 build_cachex("idempotency", expiration: idempotency_expiration(), limit: 2500),
150 build_cachex("web_resp", limit: 2500),
151 build_cachex("emoji_packs", expiration: emoji_packs_expiration(), limit: 10),
152 build_cachex("failed_proxy_url", limit: 2500)
153 ]
154 end
155
156 defp emoji_packs_expiration,
157 do: expiration(default: :timer.seconds(5 * 60), interval: :timer.seconds(60))
158
159 defp idempotency_expiration,
160 do: expiration(default: :timer.seconds(6 * 60 * 60), interval: :timer.seconds(60))
161
162 defp seconds_valid_interval,
163 do: :timer.seconds(Config.get!([Pleroma.Captcha, :seconds_valid]))
164
165 defp build_cachex(type, opts),
166 do: %{
167 id: String.to_atom("cachex_" <> type),
168 start: {Cachex, :start_link, [String.to_atom(type <> "_cache"), opts]},
169 type: :worker
170 }
171
172 defp chat_enabled?, do: Config.get([:chat, :enabled])
173
174 defp streamer_child(:test), do: []
175
176 defp streamer_child(_) do
177 [Pleroma.Web.Streamer.supervisor()]
178 end
179
180 defp chat_child(_env, true) do
181 [Pleroma.Web.ChatChannel.ChatChannelState]
182 end
183
184 defp chat_child(_, _), do: []
185
186 defp task_children(:test) do
187 [
188 %{
189 id: :web_push_init,
190 start: {Task, :start_link, [&Pleroma.Web.Push.init/0]},
191 restart: :temporary
192 }
193 ]
194 end
195
196 defp task_children(_) do
197 [
198 %{
199 id: :web_push_init,
200 start: {Task, :start_link, [&Pleroma.Web.Push.init/0]},
201 restart: :temporary
202 },
203 %{
204 id: :internal_fetch_init,
205 start: {Task, :start_link, [&Pleroma.Web.ActivityPub.InternalFetchActor.init/0]},
206 restart: :temporary
207 }
208 ]
209 end
210
211 # start hackney and gun pools in tests
212 defp http_children(_, :test) do
213 hackney_options = Config.get([:hackney_pools, :federation])
214 hackney_pool = :hackney_pool.child_spec(:federation, hackney_options)
215 [hackney_pool, Pleroma.Pool.Supervisor]
216 end
217
218 defp http_children(Tesla.Adapter.Hackney, _) do
219 pools = [:federation, :media]
220
221 pools =
222 if Config.get([Pleroma.Upload, :proxy_remote]) do
223 [:upload | pools]
224 else
225 pools
226 end
227
228 for pool <- pools do
229 options = Config.get([:hackney_pools, pool])
230 :hackney_pool.child_spec(pool, options)
231 end
232 end
233
234 defp http_children(Tesla.Adapter.Gun, _), do: [Pleroma.Pool.Supervisor]
235
236 defp http_children(_, _), do: []
237 end