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(Pleroma.Web.Plugs.SetLocalePlug)
16 plug(Pleroma.Web.Plugs.HTTPSecurityPlug)
17 plug(Pleroma.Web.Plugs.UploadedMedia)
19 @static_cache_control "public, no-cache"
21 # InstanceStatic needs to be before Plug.Static to be able to override shipped-static files
22 # If you're adding new paths to `only:` you'll need to configure them in InstanceStatic as well
23 # Cache-control headers are duplicated in case we turn off etags in the future
24 plug(Pleroma.Web.Plugs.InstanceStatic,
27 cache_control_for_etags: @static_cache_control,
29 "cache-control" => @static_cache_control
33 # Careful! No `only` restriction here, as we don't know what frontends contain.
34 plug(Pleroma.Web.Plugs.FrontendStatic,
36 frontend_type: :primary,
38 cache_control_for_etags: @static_cache_control,
40 "cache-control" => @static_cache_control
44 plug(Plug.Static.IndexHtml, at: "/pleroma/admin/")
46 plug(Pleroma.Web.Plugs.FrontendStatic,
48 frontend_type: :admin,
50 cache_control_for_etags: @static_cache_control,
52 "cache-control" => @static_cache_control
56 # Serve at "/" the static files from "priv/static" directory.
58 # You should set gzip to true if you are running phoenix.digest
59 # when deploying your static files in production.
64 only: Pleroma.Constants.static_only_files(),
65 # credo:disable-for-previous-line Credo.Check.Readability.MaxLineLength
67 cache_control_for_etags: @static_cache_control,
69 "cache-control" => @static_cache_control
74 at: "/pleroma/admin/",
75 from: {:pleroma, "priv/static/adminfe/"}
78 # Code reloading can be explicitly enabled under the
79 # :code_reloader configuration of your endpoint.
81 plug(Phoenix.CodeReloader)
84 plug(Pleroma.Web.Plugs.TrailingFormatPlug)
86 plug(Plug.Logger, log: :debug)
91 {:multipart, length: {Config, :get, [[:instance, :upload_limit]]}},
96 length: Config.get([:instance, :upload_limit]),
97 body_reader: {Pleroma.Web.Plugs.DigestPlug, :read_body, []}
100 plug(Plug.MethodOverride)
103 secure_cookies = Config.get([__MODULE__, :secure_cookie_flag])
107 do: "__Host-pleroma_key",
111 Config.get([__MODULE__, :extra_cookie_attrs])
114 # The session will be stored in the cookie and signed,
115 # this means its contents can be read but not tampered with.
116 # Set :encryption_salt if you would also like to encrypt it.
121 signing_salt: Config.get([__MODULE__, :signing_salt], "CqaoopA2"),
123 secure: secure_cookies,
127 plug(Pleroma.Web.Plugs.RemoteIp)
129 defmodule Instrumenter do
130 use Prometheus.PhoenixInstrumenter
133 defmodule PipelineInstrumenter do
134 use Prometheus.PlugPipelineInstrumenter
137 defmodule MetricsExporter do
138 use Prometheus.PlugExporter
141 defmodule MetricsExporterCaller do
144 def init(opts), do: opts
146 def call(conn, opts) do
147 prometheus_config = Application.get_env(:prometheus, MetricsExporter, [])
148 ip_whitelist = List.wrap(prometheus_config[:ip_whitelist])
151 !prometheus_config[:enabled] ->
154 ip_whitelist != [] and
155 !Enum.find(ip_whitelist, fn ip ->
156 Pleroma.Helpers.InetHelper.parse_address(ip) == {:ok, conn.remote_ip}
161 MetricsExporter.call(conn, opts)
166 plug(PipelineInstrumenter)
168 plug(MetricsExporterCaller)
170 plug(Pleroma.Web.Router)
173 Dynamically loads configuration from the system environment
176 It receives the endpoint configuration from the config files
177 and must return the updated configuration.
179 def load_from_system_env(config) do
180 port = System.get_env("PORT") || raise "expected the PORT environment variable to be set"
181 {:ok, Keyword.put(config, :http, [:inet6, port: port])}
185 String.replace_leading(url(), "http", "ws")