Skip to content

Commit

Permalink
feat: add support for forwarded %Plug.Conn{}
Browse files Browse the repository at this point in the history
Forwarded `%Plug.Conn{}` means the `%Plug.Conn{}` processed by
`Plug.forward/4` or something like that.
  • Loading branch information
c4710n committed Dec 24, 2023
1 parent baaa63d commit e1ac5b8
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions lib/plug_heartbeat.ex
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,18 @@ defmodule PlugHeartbeat do
do: Keyword.merge([path: @default_path, json: false], opts)

def call(%Plug.Conn{} = conn, opts) do
if conn.request_path == opts[:path] and conn.method in ~w(GET HEAD) do
conn |> halt |> send_beat(opts[:json])
expected_path_info = String.split(opts[:path], "/", trim: true)

if conn.path_info == expected_path_info and conn.method in ~w(GET HEAD) do
conn |> halt |> response(opts[:json])
else
conn
end
end

defp send_beat(conn, false = _json),
defp response(conn, false = _json),
do: send_resp(conn, 200, "OK")

defp send_beat(conn, true = _json),
defp response(conn, true = _json),
do: conn |> put_resp_content_type("application/json") |> send_resp(200, "{}")
end

0 comments on commit e1ac5b8

Please sign in to comment.