Replug is a sister library for Unplug thats aims to provide the functionality that is out of scope for the Unplug library. Specifically, while Unplug allows you to conditionally execute Plugs, Replug allows you to configure arbitrary Plugs at run-time even if the Plugs don't directly support run-time configuration.
If available in Hex, the package can be installed
by adding replug
to your list of dependencies in mix.exs
:
def deps do
[
{:replug, "~> 0.1.0"}
]
end
Replug can be used anywhere you would typically use the plug
macro. For example, let's say you want
to configure some non-configurable fields in Corsica, you could do the following:
# ---- router.ex ----
plug Replug,
plug: {Corsica, expose_headers: ~w(X-Foo)},
opts: {MyAppWeb.PlugConfigs, :corsica}
# ---- plug_configs.ex ----
defmodule MyAppWeb.PlugConfigs do
def corsica do
[
max_age: System.get_env("CORSICA_MAX_AGE"),
origins: System.get_env("VALID_ORIGINS")
]
end
end
This will wrap the call to Corsica's Plug and manually invoke Corsica's init/1
function and it's call/2
function
while providing run-time configuration.