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.Web.Endpoint do
6 use Phoenix.Endpoint, otp_app: :pleroma
8 require Pleroma.Constants
12 socket("/socket", Pleroma.Web.UserSocket)
14 plug(Plug.Telemetry, event_prefix: [:phoenix, :endpoint])
16 plug(Pleroma.Web.Plugs.SetLocalePlug)
18 plug(Pleroma.Web.Plugs.HTTPSecurityPlug)
19 plug(Pleroma.Web.Plugs.UploadedMedia)
21 @static_cache_control "public, no-cache"
23 # InstanceStatic needs to be before Plug.Static to be able to override shipped-static files
24 # If you're adding new paths to `only:` you'll need to configure them in InstanceStatic as well
25 # Cache-control headers are duplicated in case we turn off etags in the future
26 plug(Pleroma.Web.Plugs.InstanceStatic,
29 cache_control_for_etags: @static_cache_control,
31 "cache-control" => @static_cache_control
35 # Careful! No `only` restriction here, as we don't know what frontends contain.
36 plug(Pleroma.Web.Plugs.FrontendStatic,
38 frontend_type: :primary,
40 cache_control_for_etags: @static_cache_control,
42 "cache-control" => @static_cache_control
46 plug(Plug.Static.IndexHtml, at: "/pleroma/admin/")
48 plug(Pleroma.Web.Plugs.FrontendStatic,
50 frontend_type: :admin,
52 cache_control_for_etags: @static_cache_control,
54 "cache-control" => @static_cache_control
58 # Serve at "/" the static files from "priv/static" directory.
60 # You should set gzip to true if you are running phoenix.digest
61 # when deploying your static files in production.
66 only: Pleroma.Constants.static_only_files(),
67 # credo:disable-for-previous-line Credo.Check.Readability.MaxLineLength
69 cache_control_for_etags: @static_cache_control,
71 "cache-control" => @static_cache_control
76 at: "/pleroma/admin/",
77 from: {:pleroma, "priv/static/adminfe/"}
80 # Code reloading can be explicitly enabled under the
81 # :code_reloader configuration of your endpoint.
83 plug(Phoenix.CodeReloader)
86 plug(Pleroma.Web.Plugs.TrailingFormatPlug)
88 plug(Plug.Logger, log: :debug)
93 {:multipart, length: {Config, :get, [[:instance, :upload_limit]]}},
98 length: Config.get([:instance, :upload_limit]),
99 body_reader: {Pleroma.Web.Plugs.DigestPlug, :read_body, []}
102 plug(Plug.MethodOverride)
105 secure_cookies = Config.get([__MODULE__, :secure_cookie_flag])
109 do: "__Host-pleroma_key",
113 Config.get([__MODULE__, :extra_cookie_attrs])
116 # The session will be stored in the cookie and signed,
117 # this means its contents can be read but not tampered with.
118 # Set :encryption_salt if you would also like to encrypt it.
123 signing_salt: Config.get([__MODULE__, :signing_salt], "CqaoopA2"),
125 secure: secure_cookies,
129 plug(Pleroma.Web.Plugs.RemoteIp)
131 defmodule Instrumenter do
132 use Prometheus.PhoenixInstrumenter
135 defmodule PipelineInstrumenter do
136 use Prometheus.PlugPipelineInstrumenter
139 defmodule MetricsExporter do
140 use Prometheus.PlugExporter
143 defmodule MetricsExporterCaller do
146 def init(opts), do: opts
148 def call(conn, opts) do
149 prometheus_config = Application.get_env(:prometheus, MetricsExporter, [])
150 ip_whitelist = List.wrap(prometheus_config[:ip_whitelist])
153 !prometheus_config[:enabled] ->
156 ip_whitelist != [] and
157 !Enum.find(ip_whitelist, fn ip ->
158 Pleroma.Helpers.InetHelper.parse_address(ip) == {:ok, conn.remote_ip}
163 MetricsExporter.call(conn, opts)
168 plug(PipelineInstrumenter)
170 plug(MetricsExporterCaller)
172 plug(Pleroma.Web.Router)
175 Dynamically loads configuration from the system environment
178 It receives the endpoint configuration from the config files
179 and must return the updated configuration.
181 def load_from_system_env(config) do
182 port = System.get_env("PORT") || raise "expected the PORT environment variable to be set"
183 {:ok, Keyword.put(config, :http, [:inet6, port: port])}
187 String.replace_leading(url(), "http", "ws")