Downgrade Plug to 1.10.x, revert upload_limit tuple to function change
[akkoma] / lib / pleroma / web / endpoint.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.Endpoint do
6 use Phoenix.Endpoint, otp_app: :pleroma
7
8 require Pleroma.Constants
9
10 alias Pleroma.Config
11
12 socket("/socket", Pleroma.Web.UserSocket)
13
14 plug(Plug.Telemetry, event_prefix: [:phoenix, :endpoint])
15
16 plug(Pleroma.Web.Plugs.SetLocalePlug)
17 plug(CORSPlug)
18 plug(Pleroma.Web.Plugs.HTTPSecurityPlug)
19 plug(Pleroma.Web.Plugs.UploadedMedia)
20
21 @static_cache_control "public, no-cache"
22
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(
27 Pleroma.Web.Plugs.InstanceStatic,
28 at: "/",
29 from: :pleroma,
30 only: ["emoji", "images"],
31 gzip: true,
32 cache_control_for_etags: "public, max-age=1209600",
33 headers: %{
34 "cache-control" => "public, max-age=1209600"
35 }
36 )
37
38 plug(Pleroma.Web.Plugs.InstanceStatic,
39 at: "/",
40 gzip: true,
41 cache_control_for_etags: @static_cache_control,
42 headers: %{
43 "cache-control" => @static_cache_control
44 }
45 )
46
47 # Careful! No `only` restriction here, as we don't know what frontends contain.
48 plug(Pleroma.Web.Plugs.FrontendStatic,
49 at: "/",
50 frontend_type: :primary,
51 gzip: true,
52 cache_control_for_etags: @static_cache_control,
53 headers: %{
54 "cache-control" => @static_cache_control
55 }
56 )
57
58 plug(Plug.Static.IndexHtml, at: "/pleroma/admin/")
59
60 plug(Pleroma.Web.Plugs.FrontendStatic,
61 at: "/pleroma/admin",
62 frontend_type: :admin,
63 gzip: true,
64 cache_control_for_etags: @static_cache_control,
65 headers: %{
66 "cache-control" => @static_cache_control
67 }
68 )
69
70 # Serve at "/" the static files from "priv/static" directory.
71 #
72 # You should set gzip to true if you are running phoenix.digest
73 # when deploying your static files in production.
74 plug(
75 Plug.Static,
76 at: "/",
77 from: :pleroma,
78 only: Pleroma.Constants.static_only_files(),
79 # credo:disable-for-previous-line Credo.Check.Readability.MaxLineLength
80 gzip: true,
81 cache_control_for_etags: @static_cache_control,
82 headers: %{
83 "cache-control" => @static_cache_control
84 }
85 )
86
87 plug(Plug.Static,
88 at: "/pleroma/admin/",
89 from: {:pleroma, "priv/static/adminfe/"}
90 )
91
92 # Code reloading can be explicitly enabled under the
93 # :code_reloader configuration of your endpoint.
94 if code_reloading? do
95 plug(Phoenix.CodeReloader)
96 end
97
98 plug(Pleroma.Web.Plugs.TrailingFormatPlug)
99 plug(Plug.RequestId)
100 plug(Plug.Logger, log: :debug)
101
102 plug(Plug.Parsers,
103 parsers: [
104 :urlencoded,
105 {:multipart, length: {Config, :get, [[:instance, :upload_limit]]}},
106 :json
107 ],
108 pass: ["*/*"],
109 json_decoder: Jason,
110 length: Config.get([:instance, :upload_limit]),
111 body_reader: {Pleroma.Web.Plugs.DigestPlug, :read_body, []}
112 )
113
114 plug(Plug.MethodOverride)
115 plug(Plug.Head)
116
117 secure_cookies = Config.get([__MODULE__, :secure_cookie_flag])
118
119 cookie_name =
120 if secure_cookies,
121 do: "__Host-pleroma_key",
122 else: "pleroma_key"
123
124 extra =
125 Config.get([__MODULE__, :extra_cookie_attrs])
126 |> Enum.join(";")
127
128 # The session will be stored in the cookie and signed,
129 # this means its contents can be read but not tampered with.
130 # Set :encryption_salt if you would also like to encrypt it.
131 plug(
132 Plug.Session,
133 store: :cookie,
134 key: cookie_name,
135 signing_salt: Config.get([__MODULE__, :signing_salt], "CqaoopA2"),
136 http_only: true,
137 secure: secure_cookies,
138 extra: extra
139 )
140
141 plug(Pleroma.Web.Plugs.RemoteIp)
142
143 defmodule Instrumenter do
144 use Prometheus.PhoenixInstrumenter
145 end
146
147 defmodule PipelineInstrumenter do
148 use Prometheus.PlugPipelineInstrumenter
149 end
150
151 defmodule MetricsExporter do
152 use Prometheus.PlugExporter
153 end
154
155 defmodule MetricsExporterCaller do
156 @behaviour Plug
157
158 def init(opts), do: opts
159
160 def call(conn, opts) do
161 prometheus_config = Application.get_env(:prometheus, MetricsExporter, [])
162 ip_whitelist = List.wrap(prometheus_config[:ip_whitelist])
163
164 cond do
165 !prometheus_config[:enabled] ->
166 conn
167
168 ip_whitelist != [] and
169 !Enum.find(ip_whitelist, fn ip ->
170 Pleroma.Helpers.InetHelper.parse_address(ip) == {:ok, conn.remote_ip}
171 end) ->
172 conn
173
174 true ->
175 MetricsExporter.call(conn, opts)
176 end
177 end
178 end
179
180 plug(PipelineInstrumenter)
181
182 plug(MetricsExporterCaller)
183
184 plug(Pleroma.Web.Router)
185
186 @doc """
187 Dynamically loads configuration from the system environment
188 on startup.
189
190 It receives the endpoint configuration from the config files
191 and must return the updated configuration.
192 """
193 def load_from_system_env(config) do
194 port = System.get_env("PORT") || raise "expected the PORT environment variable to be set"
195 {:ok, Keyword.put(config, :http, [:inet6, port: port])}
196 end
197
198 def websocket_url do
199 String.replace_leading(url(), "http", "ws")
200 end
201 end