1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 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)
13 socket("/live", Phoenix.LiveView.Socket)
15 plug(Plug.Telemetry, event_prefix: [:phoenix, :endpoint])
17 plug(Pleroma.Web.Plugs.SetLocalePlug)
19 plug(Pleroma.Web.Plugs.HTTPSecurityPlug)
20 plug(Pleroma.Web.Plugs.UploadedMedia)
22 @static_cache_control "public, no-cache"
24 # InstanceStatic needs to be before Plug.Static to be able to override shipped-static files
25 # If you're adding new paths to `only:` you'll need to configure them in InstanceStatic as well
26 # Cache-control headers are duplicated in case we turn off etags in the future
28 Pleroma.Web.Plugs.InstanceStatic,
31 only: ["emoji", "images"],
33 cache_control_for_etags: "public, max-age=1209600",
35 "cache-control" => "public, max-age=1209600"
39 plug(Pleroma.Web.Plugs.InstanceStatic,
42 cache_control_for_etags: @static_cache_control,
44 "cache-control" => @static_cache_control
48 # Careful! No `only` restriction here, as we don't know what frontends contain.
49 plug(Pleroma.Web.Plugs.FrontendStatic,
51 frontend_type: :primary,
53 cache_control_for_etags: @static_cache_control,
55 "cache-control" => @static_cache_control
59 plug(Plug.Static.IndexHtml, at: "/pleroma/admin/")
61 plug(Pleroma.Web.Plugs.FrontendStatic,
63 frontend_type: :admin,
65 cache_control_for_etags: @static_cache_control,
67 "cache-control" => @static_cache_control
71 plug(Plug.Static.IndexHtml, at: "/pleroma/fedife/")
73 plug(Pleroma.Web.Plugs.FrontendStatic,
74 at: "/pleroma/fedife",
75 frontend_type: :fedife,
77 cache_control_for_etags: @static_cache_control,
79 "cache-control" => @static_cache_control
83 # Serve at "/" the static files from "priv/static" directory.
85 # You should set gzip to true if you are running phoenix.digest
86 # when deploying your static files in production.
91 only: Pleroma.Constants.static_only_files(),
92 # credo:disable-for-previous-line Credo.Check.Readability.MaxLineLength
94 cache_control_for_etags: @static_cache_control,
96 "cache-control" => @static_cache_control
101 at: "/pleroma/admin/",
102 from: {:pleroma, "priv/static/adminfe/"}
105 # Code reloading can be explicitly enabled under the
106 # :code_reloader configuration of your endpoint.
107 if code_reloading? do
108 plug(Phoenix.CodeReloader)
111 plug(Pleroma.Web.Plugs.TrailingFormatPlug)
113 plug(Plug.Logger, log: :debug)
118 {:multipart, length: {Config, :get, [[:instance, :upload_limit]]}},
123 length: Config.get([:instance, :upload_limit]),
124 body_reader: {Pleroma.Web.Plugs.DigestPlug, :read_body, []}
127 plug(Plug.MethodOverride)
130 secure_cookies = Config.get([__MODULE__, :secure_cookie_flag])
134 do: "__Host-pleroma_key",
138 Config.get([__MODULE__, :extra_cookie_attrs])
141 # The session will be stored in the cookie and signed,
142 # this means its contents can be read but not tampered with.
143 # Set :encryption_salt if you would also like to encrypt it.
148 signing_salt: Config.get([__MODULE__, :signing_salt], "CqaoopA2"),
150 secure: secure_cookies,
154 plug(Pleroma.Web.Plugs.RemoteIp)
156 defmodule Instrumenter do
157 use Prometheus.PhoenixInstrumenter
160 defmodule PipelineInstrumenter do
161 use Prometheus.PlugPipelineInstrumenter
164 defmodule MetricsExporter do
165 use Prometheus.PlugExporter
168 defmodule MetricsExporterCaller do
171 def init(opts), do: opts
173 def call(conn, opts) do
174 prometheus_config = Application.get_env(:prometheus, MetricsExporter, [])
175 ip_whitelist = List.wrap(prometheus_config[:ip_whitelist])
178 !prometheus_config[:enabled] ->
181 ip_whitelist != [] and
182 !Enum.find(ip_whitelist, fn ip ->
183 Pleroma.Helpers.InetHelper.parse_address(ip) == {:ok, conn.remote_ip}
188 MetricsExporter.call(conn, opts)
193 plug(PipelineInstrumenter)
195 plug(MetricsExporterCaller)
197 plug(Pleroma.Web.Router)
200 Dynamically loads configuration from the system environment
203 It receives the endpoint configuration from the config files
204 and must return the updated configuration.
206 def load_from_system_env(config) do
207 port = System.get_env("PORT") || raise "expected the PORT environment variable to be set"
208 {:ok, Keyword.put(config, :http, [:inet6, port: port])}
212 String.replace_leading(url(), "http", "ws")