Skip to content

Commit

Permalink
Remove reverse proxy plug and use redirect
Browse files Browse the repository at this point in the history
  • Loading branch information
zacksiri committed Nov 1, 2024
1 parent a2cd92e commit ce88aa8
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 16 deletions.
4 changes: 0 additions & 4 deletions config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@ config :uplink, Oban,
components: 1
]

config :reverse_proxy_plug,
:http_client,
ReverseProxyPlug.HTTPClient.Adapters.HTTPoison

config :formation, Formation.Lxd, timeout: 180

config :logger,
Expand Down
16 changes: 6 additions & 10 deletions lib/uplink/packages/distribution.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ defmodule Uplink.Packages.Distribution do

plug :validate

plug :serve_or_proxy
plug :serve_or_redirect

defp validate(conn, _opts) do
case Firewall.allowed?(conn) do
Expand All @@ -32,7 +32,7 @@ defmodule Uplink.Packages.Distribution do
end
end

defp serve_or_proxy(conn, _opts) do
defp serve_or_redirect(conn, _opts) do
%{"glob" => params} = conn.params

[channel, org, package] = Enum.take(params, 3)
Expand All @@ -42,7 +42,7 @@ defmodule Uplink.Packages.Distribution do
|> Packages.get_latest_deployment(channel)
|> case do
%Deployment{archive: archive} ->
serve(conn, archive)
respond(conn, archive)

nil ->
conn
Expand All @@ -51,7 +51,7 @@ defmodule Uplink.Packages.Distribution do
end
end

defp serve(conn, %Archive{node: archive_node}) do
defp respond(conn, %Archive{node: archive_node}) do
if Atom.to_string(Node.self()) == archive_node do
static_options =
Plug.Static.init(
Expand All @@ -66,15 +66,11 @@ defmodule Uplink.Packages.Distribution do
internal_router_config = Application.get_env(:uplink, Uplink.Internal)
port = Keyword.get(internal_router_config, :port, 4080)

upstream =
location =
["#{conn.scheme}://", "#{node_host_name}:#{port}", conn.request_path]
|> Path.join()

reverse_proxy_options = ReverseProxyPlug.init(upstream: upstream)

conn
|> Map.put(:path_info, [])
|> ReverseProxyPlug.call(reverse_proxy_options)
Uplink.Web.redirect(conn, location)
end
end
end
12 changes: 12 additions & 0 deletions lib/uplink/web.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@ defmodule Uplink.Web do
|> send_resp(status, Jason.encode!(%{data: response}))
end

def redirect(conn, location) do
html = Plug.HTML.html_escape(location)

body =
"<html><body>You are being <a href=\"#{html}\">redirected</a>.</body></html>"

conn
|> put_resp_header("location", location)
|> put_resp_content_type("text/html")
|> send_resp(conn.status || 302, body)
end

def translate_errors(%Ecto.Changeset{} = changeset) do
Ecto.Changeset.traverse_errors(changeset, fn {msg, opts} ->
Regex.replace(~r"%{(\w+)}", msg, fn _, key ->
Expand Down
1 change: 0 additions & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ defmodule Uplink.MixProject do
{:formation, "~> 0.15"},
{:lexdee, "~> 2.4.4"},
{:plug_cowboy, "~> 2.0"},
{:reverse_proxy_plug, "~> 2.1"},
{:mint_web_socket, "~> 1.0.2"},
{:broadway, "~> 1.0"},
{:prometheus_parser, "~> 0.1.10"},
Expand Down
1 change: 0 additions & 1 deletion mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@
"prometheus_parser": {:hex, :prometheus_parser, "0.1.10", "d1657b308506261b17f111429b38c427d7e4699b9b77601113ccec658c8cb7f9", [:mix], [{:nimble_parsec, "~> 1.0", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "3db190840fc9634a8b1a478cbaebd6ef6118d8e4970c71c80a8d11cd24613640"},
"ranch": {:hex, :ranch, "1.8.0", "8c7a100a139fd57f17327b6413e4167ac559fbc04ca7448e9be9057311597a1d", [:make, :rebar3], [], "hexpm", "49fbcfd3682fab1f5d109351b61257676da1a2fdbe295904176d5e521a2ddfe5"},
"req": {:hex, :req, "0.5.6", "8fe1eead4a085510fe3d51ad854ca8f20a622aae46e97b302f499dfb84f726ac", [:mix], [{:brotli, "~> 0.3.1", [hex: :brotli, repo: "hexpm", optional: true]}, {:ezstd, "~> 1.0", [hex: :ezstd, repo: "hexpm", optional: true]}, {:finch, "~> 0.17", [hex: :finch, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}, {:mime, "~> 2.0.6 or ~> 2.1", [hex: :mime, repo: "hexpm", optional: false]}, {:nimble_csv, "~> 1.0", [hex: :nimble_csv, repo: "hexpm", optional: true]}, {:plug, "~> 1.0", [hex: :plug, repo: "hexpm", optional: true]}], "hexpm", "cfaa8e720945d46654853de39d368f40362c2641c4b2153c886418914b372185"},
"reverse_proxy_plug": {:hex, :reverse_proxy_plug, "2.3.0", "6830ae83b383576899826d09ac00328e559455de650c9f047a413f1b64de450d", [:mix], [{:cowboy, "~> 2.4", [hex: :cowboy, repo: "hexpm", optional: false]}, {: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", "eb0007fb8fc6a4a1627c62270e2713e99bea31883daac77c85d60dd10dcdfdc3"},
"shards": {:hex, :shards, "1.1.0", "ed3032e63ae99f0eaa6d012b8b9f9cead48b9a810b3f91aeac266cfc4118eff6", [:make, :rebar3], [], "hexpm", "1d188e565a54a458a7a601c2fd1e74f5cfeba755c5a534239266d28b7ff124c7"},
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.7", "354c321cf377240c7b8716899e182ce4890c5938111a1296add3ec74cf1715df", [:make, :mix, :rebar3], [], "hexpm", "fe4c190e8f37401d30167c8c405eda19469f34577987c76dde613e838bbc67f8"},
"telemetry": {:hex, :telemetry, "1.3.0", "fedebbae410d715cf8e7062c96a1ef32ec22e764197f70cda73d82778d61e7a2", [:rebar3], [], "hexpm", "7015fc8919dbe63764f4b4b87a95b7c0996bd539e0d499be6ec9d7f3875b79e6"},
Expand Down

0 comments on commit ce88aa8

Please sign in to comment.