mirror of
https://github.com/benbusby/farside.git
synced 2025-08-17 11:00:23 -04:00

Query params ("/watch?v=dQw4w9WgXcQ" for instance) would previously be lost in Farside redirects. This now includes them if they were included in the original request.
48 lines
943 B
Elixir
48 lines
943 B
Elixir
defmodule Farside.Router do
|
|
@index Application.fetch_env!(:farside, :index)
|
|
|
|
use Plug.Router
|
|
|
|
plug(Farside.Throttle)
|
|
plug(:match)
|
|
plug(:dispatch)
|
|
|
|
get "/" do
|
|
resp =
|
|
EEx.eval_file(
|
|
@index,
|
|
last_updated: Farside.get_last_updated(),
|
|
services: Farside.get_services_map()
|
|
)
|
|
|
|
send_resp(conn, 200, resp)
|
|
end
|
|
|
|
get "/ping" do
|
|
# Useful for app healthcheck
|
|
{:ok, resp} = Redix.command(:redix, ["PING"])
|
|
send_resp(conn, 200, resp)
|
|
end
|
|
|
|
get "/:service/*glob" do
|
|
path = Enum.join(glob, "/")
|
|
instance = Farside.pick_instance(service)
|
|
|
|
params =
|
|
cond do
|
|
String.length(conn.query_string) > 0 ->
|
|
"?#{conn.query_string}"
|
|
|
|
true ->
|
|
""
|
|
end
|
|
|
|
# Redirect to the available instance
|
|
conn
|
|
|> Plug.Conn.resp(:found, "")
|
|
|> Plug.Conn.put_resp_header(
|
|
"location",
|
|
"#{instance}/#{path}#{params}"
|
|
)
|
|
end
|
|
end
|