CI: Add auto-deployment via dokku.
[akkoma] / lib / pleroma / web / endpoint.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 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 # Serve at "/" the static files from "priv/static" directory.
11 #
12 # You should set gzip to true if you are running phoenix.digest
13 # when deploying your static files in production.
14 plug(CORSPlug)
15 plug(Pleroma.Plugs.HTTPSecurityPlug)
16
17 plug(Pleroma.Plugs.UploadedMedia)
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 plug(Pleroma.Plugs.InstanceStatic, at: "/")
22
23 plug(
24 Plug.Static,
25 at: "/",
26 from: :pleroma,
27 only:
28 ~w(index.html robots.txt static finmoji emoji packs sounds images instance sw.js sw-pleroma.js favicon.png schemas doc)
29 # credo:disable-for-previous-line Credo.Check.Readability.MaxLineLength
30 )
31
32 plug(Plug.Static.IndexHtml, at: "/pleroma/admin/")
33
34 plug(Plug.Static,
35 at: "/pleroma/admin/",
36 from: {:pleroma, "priv/static/adminfe/"}
37 )
38
39 # Code reloading can be explicitly enabled under the
40 # :code_reloader configuration of your endpoint.
41 if code_reloading? do
42 plug(Phoenix.CodeReloader)
43 end
44
45 plug(TrailingFormatPlug)
46 plug(Plug.RequestId)
47 plug(Plug.Logger)
48
49 plug(
50 Plug.Parsers,
51 parsers: [:urlencoded, :multipart, :json],
52 pass: ["*/*"],
53 json_decoder: Jason,
54 length: Application.get_env(:pleroma, :instance) |> Keyword.get(:upload_limit),
55 body_reader: {Pleroma.Web.Plugs.DigestPlug, :read_body, []}
56 )
57
58 plug(Plug.MethodOverride)
59 plug(Plug.Head)
60
61 secure_cookies = Pleroma.Config.get([__MODULE__, :secure_cookie_flag])
62
63 cookie_name =
64 if secure_cookies,
65 do: "__Host-pleroma_key",
66 else: "pleroma_key"
67
68 extra =
69 Pleroma.Config.get([__MODULE__, :extra_cookie_attrs])
70 |> Enum.join(";")
71
72 # The session will be stored in the cookie and signed,
73 # this means its contents can be read but not tampered with.
74 # Set :encryption_salt if you would also like to encrypt it.
75 plug(
76 Plug.Session,
77 store: :cookie,
78 key: cookie_name,
79 signing_salt: {Pleroma.Config, :get, [[__MODULE__, :signing_salt], "CqaoopA2"]},
80 http_only: true,
81 secure: secure_cookies,
82 extra: extra
83 )
84
85 # Note: the plug and its configuration is compile-time this can't be upstreamed yet
86 if proxies = Pleroma.Config.get([__MODULE__, :reverse_proxies]) do
87 plug(RemoteIp, proxies: proxies)
88 end
89
90 defmodule Instrumenter do
91 use Prometheus.PhoenixInstrumenter
92 end
93
94 defmodule PipelineInstrumenter do
95 use Prometheus.PlugPipelineInstrumenter
96 end
97
98 defmodule MetricsExporter do
99 use Prometheus.PlugExporter
100 end
101
102 plug(PipelineInstrumenter)
103 plug(MetricsExporter)
104
105 plug(Pleroma.Web.Router)
106
107 @doc """
108 Dynamically loads configuration from the system environment
109 on startup.
110
111 It receives the endpoint configuration from the config files
112 and must return the updated configuration.
113 """
114 def load_from_system_env(config) do
115 port = System.get_env("PORT") || raise "expected the PORT environment variable to be set"
116 {:ok, Keyword.put(config, :http, [:inet6, port: port])}
117 end
118
119 def websocket_url do
120 String.replace_leading(url(), "http", "ws")
121 end
122 end