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
5 changed files with 30 additions and 3 deletions

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