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 Pleroma.HTML.compile_scrubbers()
39 Config.DeprecationWarnings.warn()
40 Pleroma.Plugs.HTTPSecurityPlug.warn_if_disabled()
41 Pleroma.Repo.check_migrations_applied!()
45 if adapter() == Tesla.Adapter.Gun do
46 if version = Pleroma.OTPVersion.version() do
50 |> Enum.map(&String.to_integer/1)
53 if (major == 22 and minor < 2) or major < 22 do
55 !!!OTP VERSION WARNING!!!
56 You are using gun adapter with OTP version #{version}, which doesn't support correct handling of unordered certificates chains.
61 !!!OTP VERSION WARNING!!!
62 To support correct handling of unordered certificates chains - OTP version must be > 22.2.
67 # Define workers and child supervisors to be supervised
74 Pleroma.Plugs.RateLimiter.Supervisor
77 http_pools_children(Config.get(:env)) ++
80 Pleroma.JobQueueMonitor,
81 {Oban, Config.get(Oban)}
83 task_children(@env) ++
84 streamer_child(@env) ++
85 chat_child(@env, chat_enabled?()) ++
91 # See http://elixir-lang.org/docs/stable/elixir/Supervisor.html
92 # for other strategies and supported options
93 opts = [strategy: :one_for_one, name: Pleroma.Supervisor]
94 Supervisor.start_link(children, opts)
97 def load_custom_modules do
98 dir = Config.get([:modules, :runtime_dir])
100 if dir && File.exists?(dir) do
102 |> Pleroma.Utils.compile_dir()
104 {:error, _errors, _warnings} ->
105 raise "Invalid custom modules"
107 {:ok, modules, _warnings} ->
109 Enum.each(modules, fn mod ->
110 Logger.info("Custom module loaded: #{inspect(mod)}")
119 defp setup_instrumenters do
120 require Prometheus.Registry
122 if Application.get_env(:prometheus, Pleroma.Repo.Instrumenter) do
126 [:pleroma, :repo, :query],
127 &Pleroma.Repo.Instrumenter.handle_event/4,
131 Pleroma.Repo.Instrumenter.setup()
134 Pleroma.Web.Endpoint.MetricsExporter.setup()
135 Pleroma.Web.Endpoint.PipelineInstrumenter.setup()
136 Pleroma.Web.Endpoint.Instrumenter.setup()
139 defp cachex_children do
141 build_cachex("used_captcha", ttl_interval: seconds_valid_interval()),
142 build_cachex("user", default_ttl: 25_000, ttl_interval: 1000, limit: 2500),
143 build_cachex("object", default_ttl: 25_000, ttl_interval: 1000, limit: 2500),
144 build_cachex("rich_media", default_ttl: :timer.minutes(120), limit: 5000),
145 build_cachex("scrubber", limit: 2500),
146 build_cachex("idempotency", expiration: idempotency_expiration(), limit: 2500),
147 build_cachex("web_resp", limit: 2500),
148 build_cachex("emoji_packs", expiration: emoji_packs_expiration(), limit: 10),
149 build_cachex("failed_proxy_url", limit: 2500)
153 defp emoji_packs_expiration,
154 do: expiration(default: :timer.seconds(5 * 60), interval: :timer.seconds(60))
156 defp idempotency_expiration,
157 do: expiration(default: :timer.seconds(6 * 60 * 60), interval: :timer.seconds(60))
159 defp seconds_valid_interval,
160 do: :timer.seconds(Config.get!([Pleroma.Captcha, :seconds_valid]))
162 defp build_cachex(type, opts),
164 id: String.to_atom("cachex_" <> type),
165 start: {Cachex, :start_link, [String.to_atom(type <> "_cache"), opts]},
169 defp chat_enabled?, do: Config.get([:chat, :enabled])
171 defp streamer_child(:test), do: []
173 defp streamer_child(_) do
174 [Pleroma.Web.Streamer.supervisor()]
177 defp chat_child(_env, true) do
178 [Pleroma.Web.ChatChannel.ChatChannelState]
181 defp chat_child(_, _), do: []
183 defp task_children(:test) do
187 start: {Task, :start_link, [&Pleroma.Web.Push.init/0]},
193 defp task_children(_) do
197 start: {Task, :start_link, [&Pleroma.Web.Push.init/0]},
201 id: :internal_fetch_init,
202 start: {Task, :start_link, [&Pleroma.Web.ActivityPub.InternalFetchActor.init/0]},
208 # start hackney and gun pools in tests
209 defp http_pools_children(:test) do
210 hackney_options = Config.get([:hackney_pools, :federation])
211 hackney_pool = :hackney_pool.child_spec(:federation, hackney_options)
212 [hackney_pool, Pleroma.Pool.Supervisor]
215 defp http_pools_children(_), do: http_pools(adapter())
217 defp http_pools(Tesla.Adapter.Hackney) do
218 pools = [:federation, :media]
221 if Config.get([Pleroma.Upload, :proxy_remote]) do
228 options = Config.get([:hackney_pools, pool])
229 :hackney_pool.child_spec(pool, options)
233 defp http_pools(Tesla.Adapter.Gun), do: [Pleroma.Pool.Supervisor]
235 defp http_pools(_), do: []
237 defp adapter, do: Application.get_env(:tesla, :adapter)