|
| 1 | +defmodule PlugDynamic do |
| 2 | + @moduledoc """ |
| 3 | + Moves any plugs configuration from compile time to runtime. |
| 4 | +
|
| 5 | + ### Usage Example (plain) |
| 6 | +
|
| 7 | + defmodule Acme.Endpoint do |
| 8 | + use Plug.Builder |
| 9 | +
|
| 10 | + plug( |
| 11 | + PlugDynamic, |
| 12 | + plug: Plug.IpWhitelist.IpWhitelistEnforcer, |
| 13 | + options: {__MODULE__, :ip_whitelist_options, 0}, |
| 14 | + reevaluate: :first_usage |
| 15 | + ) |
| 16 | +
|
| 17 | + def ip_whitelist_options, |
| 18 | + do: Application.fetch_env!(:acme, Plug.IpWhitelist.IpWhitelistEnforcer) |
| 19 | + end |
| 20 | +
|
| 21 | + ### Usage Example (macro) |
| 22 | +
|
| 23 | + defmodule Acme.Endpoint do |
| 24 | + use Plug.Builder |
| 25 | + use PlugDynamic |
| 26 | +
|
| 27 | + dynamic_plug Plug.IpWhitelist.IpWhitelistEnforcer, [reevaluate: :first_usage] do |
| 28 | + Application.fetch_env!(:acme, Plug.IpWhitelist.IpWhitelistEnforcer) |
| 29 | + end |
| 30 | + end |
| 31 | +
|
| 32 | + ### Options |
| 33 | +
|
| 34 | + * `options` - anonymous function or `{Module, :function, [args]}` tuple to fetch the configuration |
| 35 | + * `reevaluate` - default: `:first_usage` - one of the following values |
| 36 | + - `:first_usage` - Evaluate Options when it is used for the first time. The resulting value will be cached in ets. |
| 37 | + - `:always` - Evaluate Options for every request. (Attention: This can cause a severe performance impact.) |
| 38 | +
|
| 39 | + """ |
| 40 | + |
| 41 | + @behaviour Plug |
| 42 | + |
| 43 | + alias Plug.Conn |
| 44 | + alias PlugDynamic.{Builder, Storage} |
| 45 | + |
| 46 | + require Logger |
| 47 | + |
| 48 | + @type options_fetcher :: (() -> any) | {atom, atom, [any]} | mfa |
| 49 | + @type reevaluate :: :first_usage | :always |
| 50 | + @type plug :: atom |
| 51 | + |
| 52 | + @typep options_fetcher_normalized :: (() -> any) |
| 53 | + |
| 54 | + @enforce_keys [:options_fetcher, :reevaluate, :plug, :reference] |
| 55 | + defstruct @enforce_keys |
| 56 | + |
| 57 | + defmacro __using__ do |
| 58 | + quote do |
| 59 | + import unquote(Builder), only: [dynamic_plug: 1, dynamic_plug: 2, dynamic_plug: 3] |
| 60 | + end |
| 61 | + end |
| 62 | + |
| 63 | + @impl Plug |
| 64 | + @doc false |
| 65 | + def init(options) do |
| 66 | + plug = Keyword.fetch!(options, :plug) |
| 67 | + |
| 68 | + %__MODULE__{ |
| 69 | + plug: plug, |
| 70 | + options_fetcher: options |> Keyword.get(:options, {__MODULE__, :empty_opts, 0}), |
| 71 | + reevaluate: options |> Keyword.get(:reevaluate, :first_usage), |
| 72 | + reference: :"#{plug}.#{inspect(make_ref())}" |
| 73 | + } |
| 74 | + end |
| 75 | + |
| 76 | + @impl Plug |
| 77 | + @doc false |
| 78 | + def call(%Conn{} = conn, %{reevaluate: :always, plug: plug, options_fetcher: options_fetcher}), |
| 79 | + do: plug.call(conn, plug.init(normalize_options_fetcher(options_fetcher).())) |
| 80 | + |
| 81 | + def call(%Conn{} = conn, %{ |
| 82 | + reevaluate: :first_usage, |
| 83 | + plug: plug, |
| 84 | + options_fetcher: options_fetcher, |
| 85 | + reference: reference |
| 86 | + }) do |
| 87 | + options = fetch_or_create_options(plug, reference, options_fetcher) |
| 88 | + plug.call(conn, options) |
| 89 | + end |
| 90 | + |
| 91 | + @spec fetch_or_create_options( |
| 92 | + plug :: atom, |
| 93 | + reference :: atom, |
| 94 | + options_fetcher :: options_fetcher |
| 95 | + ) :: any |
| 96 | + defp fetch_or_create_options(plug, reference, options_fetcher) do |
| 97 | + reference |
| 98 | + |> Storage.fetch() |
| 99 | + |> case do |
| 100 | + {:ok, options} -> |
| 101 | + options |
| 102 | + |
| 103 | + :error -> |
| 104 | + options = plug.init(normalize_options_fetcher(options_fetcher).()) |
| 105 | + |
| 106 | + Logger.debug(fn -> |
| 107 | + "Options for Plug `#{inspect(plug)}` (#{inspect(reference, pretty: true)}) not found, storing" |
| 108 | + end) |
| 109 | + |
| 110 | + Storage.store(reference, options) |
| 111 | + |
| 112 | + options |
| 113 | + end |
| 114 | + end |
| 115 | + |
| 116 | + @spec normalize_options_fetcher(fun :: options_fetcher) :: options_fetcher_normalized |
| 117 | + defp normalize_options_fetcher(fun) when is_function(fun, 0), do: fun |
| 118 | + |
| 119 | + defp normalize_options_fetcher(fun) when is_function(fun), |
| 120 | + do: raise("Option fetching function must have 0 arity") |
| 121 | + |
| 122 | + defp normalize_options_fetcher({module, function, arguments}) |
| 123 | + when is_atom(module) and is_atom(function) and is_list(arguments), |
| 124 | + do: fn -> apply(module, function, arguments) end |
| 125 | + |
| 126 | + if function_exported?(Function, :capture, 2) do |
| 127 | + defp normalize_options_fetcher({module, function, 0}) |
| 128 | + when is_atom(module) and is_atom(function), |
| 129 | + do: Function.capture(module, function, 0) |
| 130 | + else |
| 131 | + defp normalize_options_fetcher({module, function, 0}) |
| 132 | + when is_atom(module) and is_atom(function), |
| 133 | + do: fn -> apply(module, function, []) end |
| 134 | + end |
| 135 | + |
| 136 | + defp normalize_options_fetcher({module, function, arity}) |
| 137 | + when is_atom(module) and is_atom(function) and is_integer(0) and arity > 0, |
| 138 | + do: raise("Option fetching function must have 0 arity") |
| 139 | + |
| 140 | + @doc false |
| 141 | + @spec empty_opts :: [] |
| 142 | + def empty_opts, do: [] |
| 143 | +end |
0 commit comments