Fork of Pleroma with site-specific changes and feature branches https://git.pleroma.social/pleroma/pleroma
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

160 line
4.2KB

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