Merge remote-tracking branch 'remotes/origin/develop' into restricted-relations-embedding
[akkoma] / lib / pleroma / web / endpoint.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 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 socket("/socket", Pleroma.Web.UserSocket)
11
12 plug(Pleroma.Plugs.SetLocalePlug)
13 plug(CORSPlug)
14 plug(Pleroma.Plugs.HTTPSecurityPlug)
15 plug(Pleroma.Plugs.UploadedMedia)
16
17 @static_cache_control "public, no-cache"
18
19 # InstanceStatic needs to be before Plug.Static to be able to override shipped-static files
20 # If you're adding new paths to `only:` you'll need to configure them in InstanceStatic as well
21 # Cache-control headers are duplicated in case we turn off etags in the future
22 plug(Pleroma.Plugs.InstanceStatic,
23 at: "/",
24 gzip: true,
25 cache_control_for_etags: @static_cache_control,
26 headers: %{
27 "cache-control" => @static_cache_control
28 }
29 )
30
31 # Serve at "/" the static files from "priv/static" directory.
32 #
33 # You should set gzip to true if you are running phoenix.digest
34 # when deploying your static files in production.
35 plug(
36 Plug.Static,
37 at: "/",
38 from: :pleroma,
39 only: Pleroma.Constants.static_only_files(),
40 # credo:disable-for-previous-line Credo.Check.Readability.MaxLineLength
41 gzip: true,
42 cache_control_for_etags: @static_cache_control,
43 headers: %{
44 "cache-control" => @static_cache_control
45 }
46 )
47
48 plug(Plug.Static.IndexHtml, at: "/pleroma/admin/")
49
50 plug(Plug.Static,
51 at: "/pleroma/admin/",
52 from: {:pleroma, "priv/static/adminfe/"}
53 )
54
55 # Code reloading can be explicitly enabled under the
56 # :code_reloader configuration of your endpoint.
57 if code_reloading? do
58 plug(Phoenix.CodeReloader)
59 end
60
61 plug(Pleroma.Plugs.TrailingFormatPlug)
62 plug(Plug.RequestId)
63 plug(Plug.Logger, log: :debug)
64
65 plug(Plug.Parsers,
66 parsers: [
67 :urlencoded,
68 {:multipart, length: {Pleroma.Config, :get, [[:instance, :upload_limit]]}},
69 :json
70 ],
71 pass: ["*/*"],
72 json_decoder: Jason,
73 length: Pleroma.Config.get([:instance, :upload_limit]),
74 body_reader: {Pleroma.Web.Plugs.DigestPlug, :read_body, []}
75 )
76
77 plug(Plug.MethodOverride)
78 plug(Plug.Head)
79
80 secure_cookies = Pleroma.Config.get([__MODULE__, :secure_cookie_flag])
81
82 cookie_name =
83 if secure_cookies,
84 do: "__Host-pleroma_key",
85 else: "pleroma_key"
86
87 extra =
88 Pleroma.Config.get([__MODULE__, :extra_cookie_attrs])
89 |> Enum.join(";")
90
91 # The session will be stored in the cookie and signed,
92 # this means its contents can be read but not tampered with.
93 # Set :encryption_salt if you would also like to encrypt it.
94 plug(
95 Plug.Session,
96 store: :cookie,
97 key: cookie_name,
98 signing_salt: Pleroma.Config.get([__MODULE__, :signing_salt], "CqaoopA2"),
99 http_only: true,
100 secure: secure_cookies,
101 extra: extra
102 )
103
104 plug(Pleroma.Plugs.RemoteIp)
105
106 defmodule Instrumenter do
107 use Prometheus.PhoenixInstrumenter
108 end
109
110 defmodule PipelineInstrumenter do
111 use Prometheus.PlugPipelineInstrumenter
112 end
113
114 defmodule MetricsExporter do
115 use Prometheus.PlugExporter
116 end
117
118 plug(PipelineInstrumenter)
119 plug(MetricsExporter)
120
121 plug(Pleroma.Web.Router)
122
123 @doc """
124 Dynamically loads configuration from the system environment
125 on startup.
126
127 It receives the endpoint configuration from the config files
128 and must return the updated configuration.
129 """
130 def load_from_system_env(config) do
131 port = System.get_env("PORT") || raise "expected the PORT environment variable to be set"
132 {:ok, Keyword.put(config, :http, [:inet6, port: port])}
133 end
134
135 def websocket_url do
136 String.replace_leading(url(), "http", "ws")
137 end
138 end