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 case Config.get([:http, :user_agent], :default) do
27 info = "#{Pleroma.Web.base_url()} <#{Config.get([:instance, :email], "")}>"
28 named_version() <> "; " <> info
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 # Scrubbers are compiled at runtime and therefore will cause a conflict
39 # every time the application is restarted, so we disable module
40 # conflicts at runtime
41 Code.compiler_options(ignore_module_conflict: true)
42 Pleroma.Telemetry.Logger.attach()
43 Config.Holder.save_default()
44 Pleroma.HTML.compile_scrubbers()
45 Config.DeprecationWarnings.warn()
46 Pleroma.Plugs.HTTPSecurityPlug.warn_if_disabled()
47 Pleroma.ApplicationRequirements.verify!()
50 Pleroma.Docs.JSON.compile()
52 adapter = Application.get_env(:tesla, :adapter)
54 if adapter == Tesla.Adapter.Gun do
55 if version = Pleroma.OTPVersion.version() do
59 |> Enum.map(&String.to_integer/1)
62 if (major == 22 and minor < 2) or major < 22 do
64 !!!OTP VERSION WARNING!!!
65 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.
70 !!!OTP VERSION WARNING!!!
71 To support correct handling of unordered certificates chains - OTP version must be > 22.2.
76 # Define workers and child supervisors to be supervised
82 Pleroma.Plugs.RateLimiter.Supervisor
85 http_children(adapter, @env) ++
88 Pleroma.JobQueueMonitor,
89 {Oban, Config.get(Oban)}
91 task_children(@env) ++
92 streamer_child(@env) ++
93 chat_child(@env, chat_enabled?()) ++
99 # See http://elixir-lang.org/docs/stable/elixir/Supervisor.html
100 # for other strategies and supported options
101 opts = [strategy: :one_for_one, name: Pleroma.Supervisor]
102 Supervisor.start_link(children, opts)
105 def load_custom_modules do
106 dir = Config.get([:modules, :runtime_dir])
108 if dir && File.exists?(dir) do
110 |> Pleroma.Utils.compile_dir()
112 {:error, _errors, _warnings} ->
113 raise "Invalid custom modules"
115 {:ok, modules, _warnings} ->
117 Enum.each(modules, fn mod ->
118 Logger.info("Custom module loaded: #{inspect(mod)}")
127 defp setup_instrumenters do
128 require Prometheus.Registry
130 if Application.get_env(:prometheus, Pleroma.Repo.Instrumenter) do
134 [:pleroma, :repo, :query],
135 &Pleroma.Repo.Instrumenter.handle_event/4,
139 Pleroma.Repo.Instrumenter.setup()
142 Pleroma.Web.Endpoint.MetricsExporter.setup()
143 Pleroma.Web.Endpoint.PipelineInstrumenter.setup()
144 Pleroma.Web.Endpoint.Instrumenter.setup()
147 defp cachex_children do
149 build_cachex("used_captcha", ttl_interval: seconds_valid_interval()),
150 build_cachex("user", default_ttl: 25_000, ttl_interval: 1000, limit: 2500),
151 build_cachex("object", default_ttl: 25_000, ttl_interval: 1000, limit: 2500),
152 build_cachex("rich_media", default_ttl: :timer.minutes(120), limit: 5000),
153 build_cachex("scrubber", limit: 2500),
154 build_cachex("idempotency", expiration: idempotency_expiration(), limit: 2500),
155 build_cachex("web_resp", limit: 2500),
156 build_cachex("emoji_packs", expiration: emoji_packs_expiration(), limit: 10),
157 build_cachex("failed_proxy_url", limit: 2500),
158 build_cachex("banned_urls", default_ttl: :timer.hours(24 * 30), limit: 5_000)
162 defp emoji_packs_expiration,
163 do: expiration(default: :timer.seconds(5 * 60), interval: :timer.seconds(60))
165 defp idempotency_expiration,
166 do: expiration(default: :timer.seconds(6 * 60 * 60), interval: :timer.seconds(60))
168 defp seconds_valid_interval,
169 do: :timer.seconds(Config.get!([Pleroma.Captcha, :seconds_valid]))
171 @spec build_cachex(String.t(), keyword()) :: map()
172 def build_cachex(type, opts),
174 id: String.to_atom("cachex_" <> type),
175 start: {Cachex, :start_link, [String.to_atom(type <> "_cache"), opts]},
179 defp chat_enabled?, do: Config.get([:chat, :enabled])
181 defp streamer_child(env) when env in [:test, :benchmark], do: []
183 defp streamer_child(_) do
187 name: Pleroma.Web.Streamer.registry(),
189 partitions: System.schedulers_online()
194 defp chat_child(_env, true) do
195 [Pleroma.Web.ChatChannel.ChatChannelState]
198 defp chat_child(_, _), do: []
200 defp task_children(:test) do
204 start: {Task, :start_link, [&Pleroma.Web.Push.init/0]},
210 defp task_children(_) do
214 start: {Task, :start_link, [&Pleroma.Web.Push.init/0]},
218 id: :internal_fetch_init,
219 start: {Task, :start_link, [&Pleroma.Web.ActivityPub.InternalFetchActor.init/0]},
225 # start hackney and gun pools in tests
226 defp http_children(_, :test) do
227 http_children(Tesla.Adapter.Hackney, nil) ++ http_children(Tesla.Adapter.Gun, nil)
230 defp http_children(Tesla.Adapter.Hackney, _) do
231 pools = [:federation, :media]
234 if Config.get([Pleroma.Upload, :proxy_remote]) do
241 options = Config.get([:hackney_pools, pool])
242 :hackney_pool.child_spec(pool, options)
246 defp http_children(Tesla.Adapter.Gun, _) do
247 Pleroma.Gun.ConnectionPool.children() ++
248 [{Task, &Pleroma.HTTP.AdapterHelper.Gun.limiter_setup/0}]
251 defp http_children(_, _), do: []