akoutmos
Unplug: Conditionally execute any plug at run-time
On more than one occasion I’ve needed the ability to conditionally execute plugs depending on the incoming request. I have also needed the ability to change this conditional behavior at run-time depending on some configuration. For example, as not to pollute my log system, I want to only run Plug.Telemetry if the request route is not something that Prometheus or Kubernetes hit frequently:
plug Unplug,
if: {Unplug.Predicates.RequestPathNotIn, ["/metrics", "/healthcheck"]},
do: Plug.Telemetry
Another example would be to add some sort of custom authentication around an arbitrary plug (like in this issue Configuring basic auth in Distillery · Issue #30 · prometheus-erl/prometheus-plugs · GitHub). Using Unplug, you can write you own Unplug predicate and only execute the plug if your request meets your particular criteria.
This removes the burden from the library maintainer from having to have some sort of configuration that meets your particular needs, and also still preserves Plug’s init/1 compile-time init behavior (Plug — Plug v1.20.2).
Hex: unplug | Hex
Github: GitHub - akoutmos/unplug: Unplug allows you to conditionally execute your Elixir plugs at run-time · GitHub
Popular in Announcing
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #hex









