logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://hacktivis.me/git/pleroma.git

endpoint.ex (5180B)


  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2022 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("/api/v1/streaming", Pleroma.Web.MastodonAPI.WebsocketHandler,
  9. longpoll: false,
  10. websocket: [
  11. path: "/",
  12. compress: false,
  13. error_handler: {Pleroma.Web.MastodonAPI.WebsocketHandler, :handle_error, []},
  14. fullsweep_after: 20
  15. ]
  16. )
  17. socket("/socket", Pleroma.Web.UserSocket,
  18. websocket: [
  19. path: "/websocket",
  20. serializer: [
  21. {Phoenix.Socket.V1.JSONSerializer, "~> 1.0.0"},
  22. {Phoenix.Socket.V2.JSONSerializer, "~> 2.0.0"}
  23. ],
  24. timeout: 60_000,
  25. transport_log: false,
  26. compress: false,
  27. fullsweep_after: 20
  28. ],
  29. longpoll: false
  30. )
  31. socket("/live", Phoenix.LiveView.Socket)
  32. plug(Plug.Telemetry, event_prefix: [:phoenix, :endpoint])
  33. plug(Pleroma.Web.Plugs.SetLocalePlug)
  34. plug(CORSPlug)
  35. plug(Pleroma.Web.Plugs.HTTPSecurityPlug)
  36. plug(Pleroma.Web.Plugs.UploadedMedia)
  37. @static_cache_control "public, max-age=1209600"
  38. @static_cache_disabled "public, no-cache"
  39. # InstanceStatic needs to be before Plug.Static to be able to override shipped-static files
  40. # If you're adding new paths to `only:` you'll need to configure them in InstanceStatic as well
  41. # Cache-control headers are duplicated in case we turn off etags in the future
  42. plug(
  43. Pleroma.Web.Plugs.InstanceStatic,
  44. at: "/",
  45. from: :pleroma,
  46. only: ["emoji", "images"],
  47. gzip: true,
  48. cache_control_for_etags: @static_cache_control,
  49. headers: %{
  50. "cache-control" => @static_cache_control
  51. }
  52. )
  53. plug(Pleroma.Web.Plugs.InstanceStatic,
  54. at: "/",
  55. gzip: true,
  56. cache_control_for_etags: @static_cache_disabled,
  57. headers: %{
  58. "cache-control" => @static_cache_disabled
  59. }
  60. )
  61. plug(Pleroma.Web.Plugs.FrontendStatic,
  62. at: "/",
  63. frontend_type: :primary,
  64. only: ["index.html"],
  65. gzip: true,
  66. cache_control_for_etags: @static_cache_disabled,
  67. headers: %{
  68. "cache-control" => @static_cache_disabled
  69. }
  70. )
  71. plug(Pleroma.Web.Plugs.FrontendStatic,
  72. at: "/",
  73. frontend_type: :primary,
  74. gzip: true,
  75. cache_control_for_etags: @static_cache_control,
  76. headers: %{
  77. "cache-control" => @static_cache_control
  78. }
  79. )
  80. plug(Plug.Static.IndexHtml, at: "/pleroma/admin/")
  81. plug(Pleroma.Web.Plugs.FrontendStatic,
  82. at: "/pleroma/admin",
  83. frontend_type: :admin,
  84. gzip: true,
  85. cache_control_for_etags: @static_cache_disabled,
  86. headers: %{
  87. "cache-control" => @static_cache_disabled
  88. }
  89. )
  90. # Serve at "/" the static files from "priv/static" directory.
  91. #
  92. # You should set gzip to true if you are running phoenix.digest
  93. # when deploying your static files in production.
  94. plug(
  95. Plug.Static,
  96. at: "/",
  97. from: :pleroma,
  98. only: Pleroma.Constants.static_only_files(),
  99. # credo:disable-for-previous-line Credo.Check.Readability.MaxLineLength
  100. gzip: true,
  101. cache_control_for_etags: @static_cache_disabled,
  102. headers: %{
  103. "cache-control" => @static_cache_disabled
  104. }
  105. )
  106. plug(Plug.Static,
  107. at: "/pleroma/admin/",
  108. from: {:pleroma, "priv/static/adminfe/"}
  109. )
  110. # Code reloading can be explicitly enabled under the
  111. # :code_reloader configuration of your endpoint.
  112. if code_reloading? do
  113. plug(Phoenix.CodeReloader)
  114. end
  115. plug(Pleroma.Web.Plugs.TrailingFormatPlug)
  116. plug(Plug.RequestId)
  117. plug(Plug.Logger, log: :debug)
  118. plug(Plug.Parsers,
  119. parsers: [:urlencoded, Pleroma.Web.Multipart, :json],
  120. pass: ["*/*"],
  121. json_decoder: Jason,
  122. # Note: this is compile-time only, won't work for database-config
  123. length: Config.get([:instance, :upload_limit]),
  124. body_reader: {Pleroma.Web.Plugs.DigestPlug, :read_body, []}
  125. )
  126. plug(Plug.MethodOverride)
  127. plug(Plug.Head)
  128. secure_cookies = Config.get([__MODULE__, :secure_cookie_flag])
  129. cookie_name =
  130. if secure_cookies,
  131. do: "__Host-pleroma_key",
  132. else: "pleroma_key"
  133. extra =
  134. Config.get([__MODULE__, :extra_cookie_attrs])
  135. |> Enum.join(";")
  136. # The session will be stored in the cookie and signed,
  137. # this means its contents can be read but not tampered with.
  138. # Set :encryption_salt if you would also like to encrypt it.
  139. plug(
  140. Plug.Session,
  141. store: :cookie,
  142. key: cookie_name,
  143. signing_salt: Config.get([__MODULE__, :signing_salt], "CqaoopA2"),
  144. http_only: true,
  145. secure: secure_cookies,
  146. extra: extra
  147. )
  148. plug(Pleroma.Web.Plugs.RemoteIp)
  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