Skip to content

Commit

Permalink
Upgrade and alter for new reverse_proxy_plug
Browse files Browse the repository at this point in the history
Signed-off-by: Edmondfrank <[email protected]>
  • Loading branch information
EdmondFrank committed Nov 13, 2024
1 parent 16aa40f commit e7b6281
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 15 deletions.
32 changes: 19 additions & 13 deletions lib/compass_admin_web/controllers/debug_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ defmodule CompassAdminWeb.DebugController do
params =
ReverseProxyPlug.init(
upstream: upstream,
client_options: [proxy: nil, timeout: 1_000, recv_timeout: 2_000],
response_mode: :buffer,
preserve_host_header: true
preserve_host_header: false
)
{:ok, body, conn} = Plug.Conn.read_body(conn)

Expand Down Expand Up @@ -100,24 +101,29 @@ defmodule CompassAdminWeb.DebugController do

def handle_redirect(resp, _), do: resp

def handle_redirect_resp({:ok, %HTTPoison.AsyncResponse{} = resp}) do
{:ok, translate_response(resp)}
end
def handle_redirect_resp({:ok, %HTTPoison.AsyncResponse{} = _resp}) do
{:ok,
Stream.unfold(nil, fn _ ->
receive do
%HTTPoison.AsyncStatus{code: code} ->
{{:status, code}, nil}

def handle_redirect_resp(resp), do: resp
%HTTPoison.AsyncHeaders{headers: headers} ->
{{:headers, headers}, nil}

defp translate_response(%mod{} = response) do
data = Map.from_struct(response)
%HTTPoison.AsyncChunk{chunk: chunk} ->
{{:chunk, chunk}, nil}

translated_resp =
mod
|> translate_mod()
|> struct(data)
%HTTPoison.Error{reason: reason} ->
{{:error, %HTTPClient.Error{reason: reason}}, nil}

translated_resp
%HTTPoison.AsyncEnd{} ->
nil
end
end)}
end

defp translate_mod(HTTPoison.AsyncResponse), do: HTTPClient.AsyncResponse
def handle_redirect_resp(resp), do: resp

defp get_location(headers) do
{_h, location} =
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ defmodule CompassAdmin.MixProject do
{:jason, "~> 1.2"},
{:sitemapper, "~> 0.6"},
{:plug_cowboy, "~> 2.5"},
{:reverse_proxy_plug, "~> 2.4"},
{:reverse_proxy_plug, "~> 3.0"},
{:httpoison, "~> 2.2"},
{:petal_components, "~> 0.18.0"},
{:ex_indexea, "~> 0.1.0"},
Expand Down
2 changes: 1 addition & 1 deletion mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
"recon": {:hex, :recon, "2.5.3", "739107b9050ea683c30e96de050bc59248fd27ec147696f79a8797ff9fa17153", [:mix, :rebar3], [], "hexpm", "6c6683f46fd4a1dfd98404b9f78dcabc7fcd8826613a89dcb984727a8c3099d7"},
"redix": {:hex, :redix, "1.4.1", "8303e13bad38ca80c15bdf79ea9cbd6eb879554c9cbb815b35df1602d7b1549d", [:mix], [{:castore, "~> 0.1.0 or ~> 1.0", [hex: :castore, repo: "hexpm", optional: true]}, {:nimble_options, "~> 0.5.0 or ~> 1.0", [hex: :nimble_options, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4.0 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "676b5ce37d7b1d46931d506e3208786bd8334a1625ecb591d87d790b23ffbd1f"},
"redlock": {:hex, :redlock, "1.0.21", "7c6b0eaa8470fb6fea24d565fd116ac98a6a0e309fda5366607bceef6733cdbe", [:mix], [{:ex_hash_ring, "~> 3.0", [hex: :ex_hash_ring, repo: "hexpm", optional: false]}, {:fastglobal, "~> 1.0.0", [hex: :fastglobal, repo: "hexpm", optional: false]}, {:poolboy, "~> 1.5", [hex: :poolboy, repo: "hexpm", optional: false]}, {:redix, "~> 1.3", [hex: :redix, repo: "hexpm", optional: false]}], "hexpm", "7e72e5b13148a3a0f5565513992543c01bdf20d082385cb91a11ea3b3e8c9cdd"},
"reverse_proxy_plug": {:hex, :reverse_proxy_plug, "2.4.1", "851547dae91f563b12863f117c564f8a4bd252b2c1aa7f19ffee897f15a1a7bd", [:mix], [{:httpoison, "~> 1.2 or ~> 2.0", [hex: :httpoison, repo: "hexpm", optional: true]}, {:plug, "~> 1.6", [hex: :plug, repo: "hexpm", optional: false]}, {:tesla, "~> 1.4", [hex: :tesla, repo: "hexpm", optional: true]}], "hexpm", "9a2dff0250968a62a34330c6a88fcf9fbbba110966d84340f250b5156f0d59f9"},
"reverse_proxy_plug": {:hex, :reverse_proxy_plug, "3.0.2", "38fde2f59bca8b219ef4f1ec0c0849a67c6d9705160e426a2354f35399db5c7b", [:mix], [{:finch, "~> 0.18", [hex: :finch, repo: "hexpm", optional: true]}, {:httpoison, "~> 1.2 or ~> 2.0", [hex: :httpoison, repo: "hexpm", optional: true]}, {:plug, "~> 1.6", [hex: :plug, repo: "hexpm", optional: false]}, {:req, "~> 0.3.0 or ~> 0.4.0 or ~> 0.5.0", [hex: :req, repo: "hexpm", optional: true]}, {:tesla, "~> 1.4", [hex: :tesla, repo: "hexpm", optional: true]}], "hexpm", "31ae5e068f7f504fba1b5c17c31c87966c720809ac15140c6c181440fbd24eda"},
"riak": {:git, "https://github.com/EdmondFrank/riak-elixir-client", "8f75b0265b2afd44348393ceb135bfce31a8d5d4", []},
"riak_pb": {:hex, :riak_pb, "2.3.2", "48ffbf66dbb3f136ab9a7134bac4e496754baa5ef58c4f50a61326736d996390", [:make, :mix, :rebar3], [{:hamcrest, "~> 0.4.1", [hex: :basho_hamcrest, repo: "hexpm", optional: false]}], "hexpm", "7c0644bcba8a423dd9c1ab85b6e7ff4df0712d8a350f9698647cff1996f0348c"},
"riakc": {:git, "https://github.com/kivra/riak-erlang-client", "675098b50052508ab7be09198b023a2648c061ee", []},
Expand Down

0 comments on commit e7b6281

Please sign in to comment.