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