Merge branch 'docs/clients-apc2s' into 'develop'
[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(Plug.Telemetry, event_prefix: [:phoenix, :endpoint])
13
14 plug(Pleroma.Web.Plugs.SetLocalePlug)
15 plug(CORSPlug)
16 plug(Pleroma.Web.Plugs.HTTPSecurityPlug)
17 plug(Pleroma.Web.Plugs.UploadedMedia)
18
19 @static_cache_control "public, no-cache"
20
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,
25 at: "/",
26 gzip: true,
27 cache_control_for_etags: @static_cache_control,
28 headers: %{
29 "cache-control" => @static_cache_control
30 }
31 )
32
33 # Careful! No `only` restriction here, as we don't know what frontends contain.
34 plug(Pleroma.Web.Plugs.FrontendStatic,
35 at: "/",
36 frontend_type: :primary,
37 gzip: true,
38 cache_control_for_etags: @static_cache_control,
39 headers: %{
40 "cache-control" => @static_cache_control
41 }
42 )
43
44 plug(Plug.Static.IndexHtml, at: "/pleroma/admin/")
45
46 plug(Pleroma.Web.Plugs.FrontendStatic,
47 at: "/pleroma/admin",
48 frontend_type: :admin,
49 gzip: true,
50 cache_control_for_etags: @static_cache_control,
51 headers: %{
52 "cache-control" => @static_cache_control
53 }
54 )
55
56 # Serve at "/" the static files from "priv/static" directory.
57 #
58 # You should set gzip to true if you are running phoenix.digest
59 # when deploying your static files in production.
60 plug(
61 Plug.Static,
62 at: "/",
63 from: :pleroma,
64 only: Pleroma.Constants.static_only_files(),
65 # credo:disable-for-previous-line Credo.Check.Readability.MaxLineLength
66 gzip: true,
67 cache_control_for_etags: @static_cache_control,
68 headers: %{
69 "cache-control" => @static_cache_control
70 }
71 )
72
73 plug(Plug.Static,
74 at: "/pleroma/admin/",
75 from: {:pleroma, "priv/static/adminfe/"}
76 )
77
78 # Code reloading can be explicitly enabled under the
79 # :code_reloader configuration of your endpoint.
80 if code_reloading? do
81 plug(Phoenix.CodeReloader)
82 end
83
84 plug(Pleroma.Web.Plugs.TrailingFormatPlug)
85 plug(Plug.RequestId)
86 plug(Plug.Logger, log: :debug)
87
88 plug(Plug.Parsers,
89 parsers: [
90 :urlencoded,
91 {:multipart, length: {Pleroma.Config, :get, [[:instance, :upload_limit]]}},
92 :json
93 ],
94 pass: ["*/*"],
95 json_decoder: Jason,
96 length: Pleroma.Config.get([:instance, :upload_limit]),
97 body_reader: {Pleroma.Web.Plugs.DigestPlug, :read_body, []}
98 )
99
100 plug(Plug.MethodOverride)
101 plug(Plug.Head)
102
103 secure_cookies = Pleroma.Config.get([__MODULE__, :secure_cookie_flag])
104
105 cookie_name =
106 if secure_cookies,
107 do: "__Host-pleroma_key",
108 else: "pleroma_key"
109
110 extra =
111 Pleroma.Config.get([__MODULE__, :extra_cookie_attrs])
112 |> Enum.join(";")
113
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.
117 plug(
118 Plug.Session,
119 store: :cookie,
120 key: cookie_name,
121 signing_salt: Pleroma.Config.get([__MODULE__, :signing_salt], "CqaoopA2"),
122 http_only: true,
123 secure: secure_cookies,
124 extra: extra
125 )
126
127 plug(Pleroma.Web.Plugs.RemoteIp)
128
129 defmodule Instrumenter do
130 use Prometheus.PhoenixInstrumenter
131 end
132
133 defmodule PipelineInstrumenter do
134 use Prometheus.PlugPipelineInstrumenter
135 end
136
137 defmodule MetricsExporter do
138 use Prometheus.PlugExporter
139 end
140
141 plug(PipelineInstrumenter)
142 plug(MetricsExporter)
143
144 plug(Pleroma.Web.Router)
145
146 @doc """
147 Dynamically loads configuration from the system environment
148 on startup.
149
150 It receives the endpoint configuration from the config files
151 and must return the updated configuration.
152 """
153 def load_from_system_env(config) do
154 port = System.get_env("PORT") || raise "expected the PORT environment variable to be set"
155 {:ok, Keyword.put(config, :http, [:inet6, port: port])}
156 end
157
158 def websocket_url do
159 String.replace_leading(url(), "http", "ws")
160 end
161 end