From 6675c62d9418c16ca27a6d5d069d50c8877d0bbb Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Wed, 21 Sep 2022 10:15:47 +0200 Subject: [PATCH] Add unplug to dynamically load plugs Signed-off-by: Thomas Citharel --- config/dev.exs | 2 ++ .../app_config_keyword_equals.ex | 20 +++++++++++++++++++ lib/web/endpoint.ex | 9 ++++++--- mix.exs | 1 + mix.lock | 1 + 5 files changed, 30 insertions(+), 3 deletions(-) create mode 100644 lib/service/unplug_predicates/app_config_keyword_equals.ex diff --git a/config/dev.exs b/config/dev.exs index 75d624f5a..77c55e544 100644 --- a/config/dev.exs +++ b/config/dev.exs @@ -96,3 +96,5 @@ config :mobilizon, :anonymous, reports: [ allowed: true ] + +config :unplug, :init_mode, :runtime diff --git a/lib/service/unplug_predicates/app_config_keyword_equals.ex b/lib/service/unplug_predicates/app_config_keyword_equals.ex new file mode 100644 index 000000000..7c9cd286a --- /dev/null +++ b/lib/service/unplug_predicates/app_config_keyword_equals.ex @@ -0,0 +1,20 @@ +defmodule Mobilizon.Service.UnplugPredicates.AppConfigKeywordEquals do + @moduledoc """ + Given an application and a key, execute the plug if the configured value + matches the expected value. + Usage: + ```elixir + plug Unplug, + if: {Unplug.Predicates.AppConfigEquals, {:my_app, :some_config, :enabled}}, + do: MyApp.Plug + ``` + """ + + @behaviour Unplug.Predicate + + @impl true + def call(_conn, {app, key, keyword_key, keyword_default_value, expected_value}) do + app |> Application.get_env(key) |> Keyword.get(keyword_key, keyword_default_value) == + expected_value + end +end diff --git a/lib/web/endpoint.ex b/lib/web/endpoint.ex index f26c15c62..e3ca1ace4 100644 --- a/lib/web/endpoint.ex +++ b/lib/web/endpoint.ex @@ -30,9 +30,12 @@ defmodule Mobilizon.Web.Endpoint do longpoll: false ) - if Keyword.get(endpoint_config(), :has_reverse_proxy, false) == true do - plug(RemoteIp) - end + plug(Unplug, + if: + {Mobilizon.Service.UnplugPredicates.AppConfigKeywordEquals, + {:mobilizon, Mobilizon.Web.Endpoint, :has_reverse_proxy, false, true}}, + do: RemoteIp + ) plug(Mobilizon.Web.Plugs.UploadedMedia) diff --git a/mix.exs b/mix.exs index ce05f4ae4..b50f45c96 100644 --- a/mix.exs +++ b/mix.exs @@ -205,6 +205,7 @@ defmodule Mobilizon.Mixfile do {:tzdata, "~> 1.1"}, {:codepagex, "~> 0.1.6"}, {:vite_phx, "~> 0.2"}, + {:unplug, "~> 1.0.0"}, # Dev and test dependencies {:phoenix_live_reload, "~> 1.2", only: [:dev, :e2e]}, {:ex_machina, "~> 2.3", only: [:dev, :test]}, diff --git a/mix.lock b/mix.lock index a6f328984..31bc66dce 100644 --- a/mix.lock +++ b/mix.lock @@ -140,6 +140,7 @@ "ueberauth_keycloak_strategy": {:hex, :ueberauth_keycloak_strategy, "0.3.0", "262f25ae9a38886e13a954919a873ae6ea9adf73cf8875eec74b945f0b2c7b2b", [:mix], [{:oauth2, "~> 2.0", [hex: :oauth2, repo: "hexpm", optional: false]}, {:ueberauth, "~> 0.7", [hex: :ueberauth, repo: "hexpm", optional: false]}], "hexpm", "d1a0abad08cd5e39722a9899200583b03ac63fee0c264799018ef06eb989db31"}, "ueberauth_twitter": {:hex, :ueberauth_twitter, "0.4.1", "92f88b1ad50322cdda719b439bb7f93b225dc0315723117bc25c782e627c8f33", [:mix], [{:httpoison, "~> 1.0", [hex: :httpoison, repo: "hexpm", optional: false]}, {:oauther, "~> 1.1", [hex: :oauther, repo: "hexpm", optional: false]}, {:ueberauth, "~> 0.7", [hex: :ueberauth, repo: "hexpm", optional: false]}], "hexpm", "83ca8ea3e1a3f976f1adbebfb323b9ebf53af453fbbf57d0486801a303b16065"}, "unicode_util_compat": {:hex, :unicode_util_compat, "0.7.0", "bc84380c9ab48177092f43ac89e4dfa2c6d62b40b8bd132b1059ecc7232f9a78", [:rebar3], [], "hexpm", "25eee6d67df61960cf6a794239566599b09e17e668d3700247bc498638152521"}, + "unplug": {:hex, :unplug, "1.0.0", "8ec2479de0baa9a6283c04a1cc616c5ca6c5b80b8ff1d857481bb2943368dbbc", [:mix], [{:plug, "~> 1.8", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "d171a85758aa412d4e85b809c203e1b1c4c76a4d6ab58e68dc9a8a8acd9b7c3a"}, "unsafe": {:hex, :unsafe, "1.0.1", "a27e1874f72ee49312e0a9ec2e0b27924214a05e3ddac90e91727bc76f8613d8", [:mix], [], "hexpm", "6c7729a2d214806450d29766abc2afaa7a2cbecf415be64f36a6691afebb50e5"}, "vite_phx": {:hex, :vite_phx, "0.3.0", "c57fc4a66eb2252b3a78bab884e204267cd13a90920b7905001cb2fc67d2afec", [:mix], [{:jason, ">= 0.0.0", [hex: :jason, repo: "hexpm", optional: true]}, {:phoenix, ">= 0.0.0", [hex: :phoenix, repo: "hexpm", optional: false]}], "hexpm", "55cae911dc8c43e75f2dab032035d34414f92b87ef030e5dbfcba65b5c248947"}, "web_push_encryption": {:git, "https://github.com/danhper/elixir-web-push-encryption.git", "70f00d06cbd88c9ac382e0ad2539e54448e9d8da", []},