Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add link to conditional plug invocation #244

Merged
merged 1 commit into from
Sep 13, 2024
Merged

Conversation

krwenholz
Copy link
Contributor

Change description

Minor docs update! I think Plug added this functionality late last year.

What problem does this solve?

Unplug is awesome, but I needed to conditionally check a few headers in ways that weren't obvious with that library. It's also nice to add one less library.

Example usage

  plug :metrics

  defp metrics(%Plug.Conn{} = conn, _opts) do
    bearer_string = "Bearer #{Application.get_env(:my_app, MyApp.PromEx)[:bearer_token]}"

    if conn.request_path == "/metrics" &&
         Plug.Conn.get_req_header(conn, "authorization")
         |> Enum.any?(fn i ->
           i == bearer_string
         end) do
      PromEx.Plug.call(conn, %{metrics_path: "/metrics", prom_ex_module: MyApp.PromEx})
    else
      conn
    end
  end

Checklist

  • I have added unit tests to cover my changes.
  • I have added documentation to cover my changes.
  • My changes have passed unit tests and have been tested E2E in an example project.

@coveralls
Copy link

Coverage Status

coverage: 76.931% (-0.2%) from 77.131%
when pulling 3f20f31 on krwenholz:patch-1
into 6f97943 on akoutmos:master.

@akoutmos akoutmos merged commit 69de8fd into akoutmos:master Sep 13, 2024
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants