Add CORS and API field

This commit is contained in:
xRealNeon 2022-01-09 18:58:48 +01:00
parent e46bf0d710
commit 7e743abee0
2 changed files with 23 additions and 3 deletions

View File

@ -24,7 +24,7 @@ macro rendered(filename)
render "src/instances/views/#{{{filename}}}.ecr"
end
alias Instance = NamedTuple(flag: String?, region: String?, stats: JSON::Any?, type: String, uri: String, monitor: JSON::Any?)
alias Instance = NamedTuple(flag: String?, region: String?, stats: JSON::Any?, cors: String?, api: Bool?, type: String, uri: String, monitor: JSON::Any?)
INSTANCES = {} of String => Instance
@ -75,14 +75,28 @@ spawn do
client.connect_timeout = 5.seconds
client.read_timeout = 5.seconds
begin
stats = JSON.parse(client.get("/api/v1/stats").body)
req = client.get("/api/v1/stats")
stats = JSON.parse(req.body)
cors = req.headers["Access-Control-Allow-Origin"]
api = false
req = client.get("/api/v1/trending")
if req.status_code == 200
begin
JSON.parse(req.body)
api = true
rescue
puts "Cant parse API json"
end
end
rescue ex
stats = nil
end
end
monitor = monitors.try &.select { |monitor| monitor["name"].try &.as_s == host }[0]?
instances[host] = {flag: flag, region: region, stats: stats, type: type, uri: uri.to_s, monitor: monitor || instances[host]?.try &.[:monitor]?}
instances[host] = {flag: flag, region: region, stats: stats, cors: cors, api: api, type: type, uri: uri.to_s, monitor: monitor || instances[host]?.try &.[:monitor]?}
end
INSTANCES.clear
@ -139,6 +153,8 @@ SORT_PROCS = {
"name" => ->(name : String, instance : Instance) { name },
"signup" => ->(name : String, instance : Instance) { instance[:stats]?.try &.["openRegistrations"]?.try { |bool| bool.as_bool ? 0 : 1 } || 2 },
"type" => ->(name : String, instance : Instance) { instance[:type] },
"cors" => ->(name : String, instance : Instance) { instance[:cors]? || "-" },
"api" => ->(name : String, instance : Instance) { instance[:api] == nil ? 2 : instance[:api] ? 0 : 1 },
"users" => ->(name : String, instance : Instance) { -(instance[:stats]?.try &.["usage"]?.try &.["users"]["total"].as_i || 0) },
"version" => ->(name : String, instance : Instance) { instance[:stats]?.try &.["software"]?.try &.["version"].as_s.try &.split("-", 2)[0].split(".").map { |a| -a.to_i } || [0, 0, 0] },
}

View File

@ -101,6 +101,8 @@
<th><a href="/?sort_by=<%= sort_by == "version" ? "version-reverse" : "version" %>">version</a></th>
<th><a href="/?sort_by=<%= sort_by == "type" ? "type-reverse" : "type" %>">type</a></th>
<th><a href="/?sort_by=<%= sort_by == "users" ? "users-reverse" : "users" %>">users</a></th>
<th><a href="/?sort_by=<%= sort_by == "cors" ? "cors-reverse" : "cors" %>">cors</a></th>
<th><a href="/?sort_by=<%= sort_by == "api" ? "api-reverse" : "api" %>">api</a></th>
<th><a href="/?sort_by=<%= sort_by == "signup" ? "signup-reverse" : "signup" %>">signup</a></th>
<th><a href="/?sort_by=<%= sort_by == "location" ? "location-reverse" : "location" %>">location</a></th>
<th><a href="/?sort_by=<%= sort_by == "health" ? "health-reverse" : "health" %>">
@ -117,6 +119,8 @@
<td><%= instance[:stats]?.try &.["software"]?.try &.["version"] || "-" %></td>
<td><%= instance[:type] %></td>
<td><%= instance[:stats]?.try &.["usage"]?.try &.["users"]["total"] || "-" %></td>
<td><%= instance[:cors] || "-" %></td>
<td><%= instance[:api] == nil ? "-" : instance[:api] ? "✔" : "❌" %></td>
<td><%= instance[:stats]?.try &.["openRegistrations"]?.try { |bool| bool.as_bool ? "✔" : "❌" } || "-" %></td>
<td><%= instance[:flag]? ? "#{instance[:flag]} #{instance[:region]}" : "-" %></td>
<td><%= instance[:monitor]?.try &.["30dRatio"]["ratio"] || "-" %></td>