Fork of Pleroma with site-specific changes and feature branches https://git.pleroma.social/pleroma/pleroma
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

202 řádky
5.1KB

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