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(Pleroma.Web.Plugs.SetLocalePlug)
17 plug(Pleroma.Web.Plugs.HTTPSecurityPlug)
18 plug(Pleroma.Web.Plugs.UploadedMedia)
20 @static_cache_control "public, no-cache"
22 # InstanceStatic needs to be before Plug.Static to be able to override shipped-static files
23 # If you're adding new paths to `only:` you'll need to configure them in InstanceStatic as well
24 # Cache-control headers are duplicated in case we turn off etags in the future
26 Pleroma.Web.Plugs.InstanceStatic,
29 only: ["emoji", "images"],
31 cache_control_for_etags: "public, max-age=1209600",
33 "cache-control" => "public, max-age=1209600"
37 plug(Pleroma.Web.Plugs.InstanceStatic,
40 cache_control_for_etags: @static_cache_control,
42 "cache-control" => @static_cache_control
46 # Careful! No `only` restriction here, as we don't know what frontends contain.
47 plug(Pleroma.Web.Plugs.FrontendStatic,
49 frontend_type: :primary,
51 cache_control_for_etags: @static_cache_control,
53 "cache-control" => @static_cache_control
57 plug(Plug.Static.IndexHtml, at: "/pleroma/admin/")
59 plug(Pleroma.Web.Plugs.FrontendStatic,
61 frontend_type: :admin,
63 cache_control_for_etags: @static_cache_control,
65 "cache-control" => @static_cache_control
69 plug(Plug.Static.IndexHtml, at: "/akkoma/swaggerui")
71 plug(Pleroma.Web.Plugs.FrontendStatic,
72 at: "/akkoma/swaggerui",
73 frontend_type: :swagger,
75 if: &Pleroma.Web.Swagger.ui_enabled?/0,
76 cache_control_for_etags: @static_cache_control,
78 "cache-control" => @static_cache_control
82 plug(Pleroma.Web.Plugs.FrontendStatic,
84 frontend_type: :mastodon,
86 cache_control_for_etags: @static_cache_control,
88 "cache-control" => @static_cache_control
92 # Serve at "/" the static files from "priv/static" directory.
94 # You should set gzip to true if you are running phoenix.digest
95 # when deploying your static files in production.
100 only: Pleroma.Constants.static_only_files(),
101 # credo:disable-for-previous-line Credo.Check.Readability.MaxLineLength
103 cache_control_for_etags: @static_cache_control,
105 "cache-control" => @static_cache_control
110 at: "/pleroma/admin/",
111 from: {:pleroma, "priv/static/adminfe/"}
114 # Code reloading can be explicitly enabled under the
115 # :code_reloader configuration of your endpoint.
116 if code_reloading? do
117 plug(Phoenix.CodeReloader)
120 plug(Pleroma.Web.Plugs.TrailingFormatPlug)
122 plug(Plug.Logger, log: :debug)
127 {:multipart, length: {Config, :get, [[:instance, :upload_limit]]}},
132 length: Config.get([:instance, :upload_limit]),
133 body_reader: {Pleroma.Web.Plugs.DigestPlug, :read_body, []}
136 plug(Plug.MethodOverride)
139 secure_cookies = Config.get([__MODULE__, :secure_cookie_flag])
143 do: "__Host-pleroma_key",
147 Config.get([__MODULE__, :extra_cookie_attrs])
150 # The session will be stored in the cookie and signed,
151 # this means its contents can be read but not tampered with.
152 # Set :encryption_salt if you would also like to encrypt it.
157 signing_salt: Config.get([__MODULE__, :signing_salt], "CqaoopA2"),
159 secure: secure_cookies,
163 plug(Pleroma.Web.Plugs.RemoteIp)
165 defmodule Instrumenter do
166 use Prometheus.PhoenixInstrumenter
169 defmodule PipelineInstrumenter do
170 use Prometheus.PlugPipelineInstrumenter
173 defmodule MetricsExporter do
174 use Prometheus.PlugExporter
177 defmodule MetricsExporterCaller do
180 def init(opts), do: opts
182 def call(conn, opts) do
183 prometheus_config = Application.get_env(:prometheus, MetricsExporter, [])
184 ip_whitelist = List.wrap(prometheus_config[:ip_whitelist])
187 !prometheus_config[:enabled] ->
190 ip_whitelist != [] and
191 !Enum.find(ip_whitelist, fn ip ->
192 Pleroma.Helpers.InetHelper.parse_address(ip) == {:ok, conn.remote_ip}
197 MetricsExporter.call(conn, opts)
202 plug(PipelineInstrumenter)
204 plug(MetricsExporterCaller)
206 plug(Pleroma.Web.Router)
209 Dynamically loads configuration from the system environment
212 It receives the endpoint configuration from the config files
213 and must return the updated configuration.
215 def load_from_system_env(config) do
216 port = System.get_env("PORT") || raise "expected the PORT environment variable to be set"
217 {:ok, Keyword.put(config, :http, [:inet6, port: port])}
221 String.replace_leading(url(), "http", "ws")