mirror of
https://github.com/benbusby/farside.git
synced 2025-04-29 23:20:03 +00:00
Display list of available instances on home page
This introduces a number of new changes: - Services are now inserted into redis with a prefix prepended to the key name. This allows for easier filtering to get only live instances. - The home page now uses an eex template for displaying all live instances for every service, determined by the last update - A "last_updated" field was added - farside.ex was added to contain all functionality related to querying for instances (WIP) - Other improvements
This commit is contained in:
parent
8042dcad0c
commit
56b9c52528
7 changed files with 131 additions and 12 deletions
45
lib/farside.ex
Normal file
45
lib/farside.ex
Normal file
|
@ -0,0 +1,45 @@
|
|||
defmodule Farside do
|
||||
@service_prefix Application.fetch_env!(:farside, :service_prefix)
|
||||
|
||||
def get_services_map do
|
||||
{:ok, redis_keys} = Redix.command(:redix, ["KEYS", "*"])
|
||||
|
||||
# Extract only service related keys
|
||||
service_list =
|
||||
Enum.filter(
|
||||
redis_keys,
|
||||
fn key ->
|
||||
String.starts_with?(key, @service_prefix)
|
||||
end
|
||||
)
|
||||
|
||||
# Match service name to list of available instances
|
||||
Enum.reduce(service_list, %{}, fn service, acc ->
|
||||
{:ok, instance_list} =
|
||||
Redix.command(
|
||||
:redix,
|
||||
["LRANGE", service, "0", "-1"]
|
||||
)
|
||||
|
||||
Map.put(
|
||||
acc,
|
||||
String.replace_prefix(
|
||||
service,
|
||||
@service_prefix,
|
||||
""
|
||||
),
|
||||
instance_list
|
||||
)
|
||||
end)
|
||||
end
|
||||
|
||||
def get_last_updated do
|
||||
{:ok, last_updated} =
|
||||
Redix.command(
|
||||
:redix,
|
||||
["GET", "last_updated"]
|
||||
)
|
||||
|
||||
last_updated
|
||||
end
|
||||
end
|
|
@ -1,5 +1,7 @@
|
|||
defmodule Farside.Router do
|
||||
@index Application.fetch_env!(:farside, :index)
|
||||
@fallback_str Application.fetch_env!(:farside, :fallback_str)
|
||||
@service_prefix Application.fetch_env!(:farside, :service_prefix)
|
||||
|
||||
use Plug.Router
|
||||
|
||||
|
@ -7,7 +9,14 @@ defmodule Farside.Router do
|
|||
plug(:dispatch)
|
||||
|
||||
get "/" do
|
||||
send_resp(conn, 200, "")
|
||||
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
|
||||
|
@ -22,7 +31,12 @@ defmodule Farside.Router do
|
|||
{:ok, instances} =
|
||||
Redix.command(
|
||||
:redix,
|
||||
["LRANGE", service, "0", "-1"]
|
||||
[
|
||||
"LRANGE",
|
||||
"#{@service_prefix}#{service}",
|
||||
"0",
|
||||
"-1"
|
||||
]
|
||||
)
|
||||
|
||||
# Either pick a random available instance,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue