1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Application do
14 @name Mix.Project.config()[:name]
15 @version Mix.Project.config()[:version]
16 @repository Mix.Project.config()[:source_url]
20 def version, do: @version
21 def named_version, do: @name <> " " <> @version
22 def repository, do: @repository
25 if Process.whereis(Pleroma.Web.Endpoint) do
26 case Config.get([:http, :user_agent], :default) do
28 info = "#{Pleroma.Web.base_url()} <#{Config.get([:instance, :email], "")}>"
29 named_version() <> "; " <> info
35 # fallback, if endpoint is not started yet
40 # See http://elixir-lang.org/docs/stable/elixir/Application.html
41 # for more information on OTP Applications
42 def start(_type, _args) do
43 # Scrubbers are compiled at runtime and therefore will cause a conflict
44 # every time the application is restarted, so we disable module
45 # conflicts at runtime
46 Code.compiler_options(ignore_module_conflict: true)
47 # Disable warnings_as_errors at runtime, it breaks Phoenix live reload
48 # due to protocol consolidation warnings
49 Code.compiler_options(warnings_as_errors: false)
50 Pleroma.Telemetry.Logger.attach()
51 Config.Holder.save_default()
52 Pleroma.HTML.compile_scrubbers()
53 Pleroma.Config.Oban.warn()
54 Config.DeprecationWarnings.warn()
55 Pleroma.Web.Plugs.HTTPSecurityPlug.warn_if_disabled()
56 Pleroma.ApplicationRequirements.verify!()
59 Pleroma.Docs.JSON.compile()
61 adapter = Application.get_env(:tesla, :adapter)
63 if adapter == Tesla.Adapter.Gun do
64 if version = Pleroma.OTPVersion.version() do
68 |> Enum.map(&String.to_integer/1)
71 if (major == 22 and minor < 2) or major < 22 do
73 !!!OTP VERSION WARNING!!!
74 You are using gun adapter with OTP version #{version}, which doesn't support correct handling of unordered certificates chains. Please update your Erlang/OTP to at least 22.2.
79 !!!OTP VERSION WARNING!!!
80 To support correct handling of unordered certificates chains - OTP version must be > 22.2.
85 # Define workers and child supervisors to be supervised
91 Pleroma.Web.Plugs.RateLimiter.Supervisor
94 http_children(adapter, @env) ++
97 Pleroma.JobQueueMonitor,
98 {Majic.Pool, [name: Pleroma.MajicPool, pool_size: Config.get([:majic_pool, :size], 2)]},
99 {Oban, Config.get(Oban)}
101 task_children(@env) ++
102 dont_run_in_test(@env) ++
103 chat_child(chat_enabled?()) ++
105 Pleroma.Web.Endpoint,
106 Pleroma.Gopher.Server
109 # See http://elixir-lang.org/docs/stable/elixir/Supervisor.html
110 # for other strategies and supported options
111 opts = [strategy: :one_for_one, name: Pleroma.Supervisor]
112 Supervisor.start_link(children, opts)
115 def load_custom_modules do
116 dir = Config.get([:modules, :runtime_dir])
118 if dir && File.exists?(dir) do
120 |> Pleroma.Utils.compile_dir()
122 {:error, _errors, _warnings} ->
123 raise "Invalid custom modules"
125 {:ok, modules, _warnings} ->
127 Enum.each(modules, fn mod ->
128 Logger.info("Custom module loaded: #{inspect(mod)}")
137 defp setup_instrumenters do
138 require Prometheus.Registry
140 if Application.get_env(:prometheus, Pleroma.Repo.Instrumenter) do
144 [:pleroma, :repo, :query],
145 &Pleroma.Repo.Instrumenter.handle_event/4,
149 Pleroma.Repo.Instrumenter.setup()
152 Pleroma.Web.Endpoint.MetricsExporter.setup()
153 Pleroma.Web.Endpoint.PipelineInstrumenter.setup()
155 # Note: disabled until prometheus-phx is integrated into prometheus-phoenix:
156 # Pleroma.Web.Endpoint.Instrumenter.setup()
157 PrometheusPhx.setup()
160 defp cachex_children do
162 build_cachex("used_captcha", ttl_interval: seconds_valid_interval()),
163 build_cachex("user", default_ttl: 25_000, ttl_interval: 1000, limit: 2500),
164 build_cachex("object", default_ttl: 25_000, ttl_interval: 1000, limit: 2500),
165 build_cachex("rich_media", default_ttl: :timer.minutes(120), limit: 5000),
166 build_cachex("scrubber", limit: 2500),
167 build_cachex("idempotency", expiration: idempotency_expiration(), limit: 2500),
168 build_cachex("web_resp", limit: 2500),
169 build_cachex("emoji_packs", expiration: emoji_packs_expiration(), limit: 10),
170 build_cachex("failed_proxy_url", limit: 2500),
171 build_cachex("banned_urls", default_ttl: :timer.hours(24 * 30), limit: 5_000),
172 build_cachex("chat_message_id_idempotency_key",
173 expiration: chat_message_id_idempotency_key_expiration(),
179 defp emoji_packs_expiration,
180 do: expiration(default: :timer.seconds(5 * 60), interval: :timer.seconds(60))
182 defp idempotency_expiration,
183 do: expiration(default: :timer.seconds(6 * 60 * 60), interval: :timer.seconds(60))
185 defp chat_message_id_idempotency_key_expiration,
186 do: expiration(default: :timer.minutes(2), interval: :timer.seconds(60))
188 defp seconds_valid_interval,
189 do: :timer.seconds(Config.get!([Pleroma.Captcha, :seconds_valid]))
191 @spec build_cachex(String.t(), keyword()) :: map()
192 def build_cachex(type, opts),
194 id: String.to_atom("cachex_" <> type),
195 start: {Cachex, :start_link, [String.to_atom(type <> "_cache"), opts]},
199 defp chat_enabled?, do: Config.get([:chat, :enabled])
201 defp dont_run_in_test(env) when env in [:test, :benchmark], do: []
203 defp dont_run_in_test(_) do
207 name: Pleroma.Web.Streamer.registry(),
209 partitions: System.schedulers_online()
211 Pleroma.Web.FedSockets.Supervisor
215 defp chat_child(true) do
217 Pleroma.Web.ChatChannel.ChatChannelState,
218 {Phoenix.PubSub, [name: Pleroma.PubSub, adapter: Phoenix.PubSub.PG2]}
222 defp chat_child(_), do: []
224 defp task_children(:test) do
228 start: {Task, :start_link, [&Pleroma.Web.Push.init/0]},
234 defp task_children(_) do
238 start: {Task, :start_link, [&Pleroma.Web.Push.init/0]},
242 id: :internal_fetch_init,
243 start: {Task, :start_link, [&Pleroma.Web.ActivityPub.InternalFetchActor.init/0]},
249 # start hackney and gun pools in tests
250 defp http_children(_, :test) do
251 http_children(Tesla.Adapter.Hackney, nil) ++ http_children(Tesla.Adapter.Gun, nil)
254 defp http_children(Tesla.Adapter.Hackney, _) do
255 pools = [:federation, :media]
258 if Config.get([Pleroma.Upload, :proxy_remote]) do
265 options = Config.get([:hackney_pools, pool])
266 :hackney_pool.child_spec(pool, options)
270 defp http_children(Tesla.Adapter.Gun, _) do
271 Pleroma.Gun.ConnectionPool.children() ++
272 [{Task, &Pleroma.HTTP.AdapterHelper.Gun.limiter_setup/0}]
275 defp http_children(_, _), do: []