1
0
Fork 0
mirror of https://github.com/benbusby/farside.git synced 2025-04-20 02:48:42 +00:00
farside/lib/farside/router.ex
Ben Busby 71fb89e028
Move instance selection logic out of router
The process of selecting a random (working) instance for a specified
service has been moved out of the router and into lib/farside.ex. Moving
forward, the router itself should have very simple and easy to follow
logic for all paths.
2021-11-10 11:50:19 -07:00

38 lines
759 B
Elixir

defmodule Farside.Router do
@index Application.fetch_env!(:farside, :index)
use Plug.Router
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)
# Redirect to the available instance
conn
|> Plug.Conn.resp(:found, "")
|> Plug.Conn.put_resp_header(
"location",
"#{instance}/#{path}"
)
end
end