Add unplug to dynamically load plugs

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel 2022-09-21 10:15:47 +02:00
parent d2372d5700
commit 6675c62d94
No known key found for this signature in database
GPG Key ID: A061B9DDE0CA0773
5 changed files with 30 additions and 3 deletions

View File

@ -96,3 +96,5 @@ config :mobilizon, :anonymous,
reports: [
allowed: true
]
config :unplug, :init_mode, :runtime

View File

@ -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

View File

@ -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)

View File

@ -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]},

View File

@ -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", []},