Skip to content

Dynamically configure your Elixir Plugs at run-time

License

Notifications You must be signed in to change notification settings

akoutmos/replug

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Replug

Hex.pm Build Status Coverage Status

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.

Installation

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

Usage

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.

About

Dynamically configure your Elixir Plugs at run-time

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages