diff options
Diffstat (limited to 'lib/pleroma/plugs')
-rw-r--r-- | lib/pleroma/plugs/frontend_plug.ex | 51 | ||||
-rw-r--r-- | lib/pleroma/plugs/instance_static.ex | 75 | ||||
-rw-r--r-- | lib/pleroma/plugs/static_fe_plug.ex | 12 |
3 files changed, 105 insertions, 33 deletions
diff --git a/lib/pleroma/plugs/frontend_plug.ex b/lib/pleroma/plugs/frontend_plug.ex new file mode 100644 index 000000000..4db194dc3 --- /dev/null +++ b/lib/pleroma/plugs/frontend_plug.ex @@ -0,0 +1,51 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/> +# SPDX-License-Identifier: AGPL-3.0-only + +defmodule Pleroma.Plugs.FrontendPlug do + @moduledoc """ + Sets private key `:frontend` for the given connection. + It is set to one of admin|mastodon|primary frontends config values based + on `conn.request_path` + """ + + import Plug.Conn + + @behaviour Plug + + @mastodon_paths ~w(web packs sw.js api/web) + @admin_paths ~w(pleroma) + + def init(opts) do + opts + end + + for path <- @mastodon_paths do + def call(%{request_path: "/" <> unquote(path) <> _rest} = conn, _opts) do + fe_config = + Pleroma.Config.get([:frontends], %{mastodon: %{"name" => "mastodon", "ref" => ""}}) + + put_private(conn, :frontend, %{ + config: fe_config[:mastodon], + controller: Pleroma.Web.Frontend.MastodonController, + static: false + }) + end + end + + for path <- @admin_paths do + def call(%{request_path: "/" <> unquote(path) <> _rest} = conn, _opts) do + fe_config = Pleroma.Config.get([:frontends], %{admin: %{"name" => "admin", "ref" => ""}}) + + put_private(conn, :frontend, %{ + config: fe_config[:admin], + controller: Pleroma.Web.Frontend.AdminController, + static: false + }) + end + end + + def call(conn, _opts) do + put_private(conn, :frontend, Pleroma.Frontend.get_primary_fe_opts()) + end +end diff --git a/lib/pleroma/plugs/instance_static.ex b/lib/pleroma/plugs/instance_static.ex index 927fa2663..41d2bc923 100644 --- a/lib/pleroma/plugs/instance_static.ex +++ b/lib/pleroma/plugs/instance_static.ex @@ -7,22 +7,28 @@ defmodule Pleroma.Plugs.InstanceStatic do This is a shim to call `Plug.Static` but with runtime `from` configuration. Mountpoints are defined directly in the module to avoid calling the configuration for every request including non-static ones. + + Files in FE bundles can override files in priv/static, and files in + instance/static can override files in FE bundles: + instance/static > FE bundles > priv/static """ @behaviour Plug - def file_path(path) do - instance_path = - Path.join(Pleroma.Config.get([:instance, :static_dir], "instance/static/"), path) - - if File.exists?(instance_path) do - instance_path - else - Path.join(Application.app_dir(:pleroma, "priv/static/"), path) - end - end + # list of paths to be looked up in intance/static + @instance_overridable_paths ~w(robots.txt emoji sounds images instance favicon.png) - @only ~w(index.html robots.txt static emoji packs sounds images instance favicon.png sw.js - sw-pleroma.js) + # both pleroma/{STATIC_PATH} and pleroma/admin/{STATIC_PATH} can be requested + @fe_prefixed_paths ~w(pleroma/admin pleroma) + @fe_paths [ + # mastodon + "packs", + "sw.js", + # primary frontend + "static", + "index.html", + "sw-pleroma.js", + "static-fe.css" + ] def init(opts) do opts @@ -31,29 +37,42 @@ defmodule Pleroma.Plugs.InstanceStatic do |> Plug.Static.init() end - for only <- @only do - at = Plug.Router.Utils.split("/") + for path <- @fe_prefixed_paths do + def call(%{request_path: "/" <> unquote(path) <> _} = conn, opts) do + fe_path = get_fe_path(conn) + opts = %{opts | at: Plug.Router.Utils.split(unquote(path)), from: fe_path} - def call(%{request_path: "/" <> unquote(only) <> _} = conn, opts) do - call_static( - conn, - opts, - unquote(at), - Pleroma.Config.get([:instance, :static_dir], "instance/static") - ) + Plug.Static.call(conn, opts) end end - def call(conn, _) do - conn + for path <- @fe_paths do + def call(%{request_path: "/" <> unquote(path) <> _} = conn, opts) do + fe_path = get_fe_path(conn) + opts = %{opts | at: [], from: fe_path} + + with ^conn <- call_instance_static(conn, opts) do + Plug.Static.call(conn, opts) + end + end end - defp call_static(conn, opts, at, from) do - opts = - opts - |> Map.put(:from, from) - |> Map.put(:at, at) + for path <- @instance_overridable_paths do + def call(%{request_path: "/" <> unquote(path) <> _} = conn, opts) do + call_instance_static(conn, opts) + end + end + + def call(conn, _), do: conn + defp call_instance_static(conn, opts) do + instance_static_path = Pleroma.Config.get([:instance, :static_dir], "instance/static") + opts = %{opts | at: [], from: instance_static_path} Plug.Static.call(conn, opts) end + + defp get_fe_path(%{private: %{frontend: %{config: conf}}}) do + instance_static_path = Pleroma.Config.get([:instance, :static_dir], "instance/static") + Path.join([instance_static_path, "frontends", conf["name"], conf["ref"]]) + end end diff --git a/lib/pleroma/plugs/static_fe_plug.ex b/lib/pleroma/plugs/static_fe_plug.ex index 156e6788e..c12776f4d 100644 --- a/lib/pleroma/plugs/static_fe_plug.ex +++ b/lib/pleroma/plugs/static_fe_plug.ex @@ -4,21 +4,23 @@ defmodule Pleroma.Plugs.StaticFEPlug do import Plug.Conn - alias Pleroma.Web.StaticFE.StaticFEController def init(options), do: options - def call(conn, _) do - if enabled?() and accepts_html?(conn) do + def call(%{private: %{frontend: %{static: true}}} = conn, _) do + action = Phoenix.Controller.action_name(conn) + + if accepts_html?(conn) and + function_exported?(Pleroma.Web.Frontend.StaticController, action, 2) do conn - |> StaticFEController.call(:show) + |> Pleroma.Web.FrontendController.call(action) |> halt() else conn end end - defp enabled?, do: Pleroma.Config.get([:static_fe, :enabled], false) + def call(conn, _), do: conn defp accepts_html?(conn) do case get_req_header(conn, "accept") do |