From 8e56425c3b26d84d28a07c20cdfea5d97c88756c Mon Sep 17 00:00:00 2001 From: xRealNeon Date: Tue, 11 Jan 2022 14:00:46 +0100 Subject: [PATCH] Changed CORS from string to boolean --- src/instances.cr | 9 ++++++--- src/instances/views/index.ecr | 2 +- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/instances.cr b/src/instances.cr index 25fcee4..a3478b7 100644 --- a/src/instances.cr +++ b/src/instances.cr @@ -24,7 +24,7 @@ macro rendered(filename) render "src/instances/views/#{{{filename}}}.ecr" end -alias Instance = NamedTuple(flag: String?, region: String?, stats: JSON::Any?, cors: String?, api: Bool?, type: String, uri: String, monitor: JSON::Any?) +alias Instance = NamedTuple(flag: String?, region: String?, stats: JSON::Any?, cors: Bool?, api: Bool?, type: String, uri: String, monitor: JSON::Any?) INSTANCES = {} of String => Instance @@ -77,14 +77,17 @@ spawn do begin req = client.get("/api/v1/stats") stats = JSON.parse(req.body) - cors = req.headers["Access-Control-Allow-Origin"] api = false + cors = false req = client.get("/api/v1/trending") if req.status_code == 200 begin JSON.parse(req.body) api = true + if req.headers["Access-Control-Allow-Origin"] == "*" + cors = true + end rescue puts "Cant parse API json" end @@ -153,7 +156,7 @@ 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]? || "-" }, + "cors" => ->(name : String, instance : Instance) { instance[:cors] == nil ? 2 : instance[:cors] ? 0 : 1 }, "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] }, diff --git a/src/instances/views/index.ecr b/src/instances/views/index.ecr index 6b645f4..1dc3ee7 100644 --- a/src/instances/views/index.ecr +++ b/src/instances/views/index.ecr @@ -119,7 +119,7 @@ <%= instance[:stats]?.try &.["software"]?.try &.["version"] || "-" %> <%= instance[:type] %> <%= instance[:stats]?.try &.["usage"]?.try &.["users"]["total"] || "-" %> - <%= instance[:cors] || "-" %> + <%= instance[:cors] == nil ? "-" : instance[:cors] ? "✔" : "❌" %> <%= instance[:api] == nil ? "-" : instance[:api] ? "✔" : "❌" %> <%= instance[:stats]?.try &.["openRegistrations"]?.try { |bool| bool.as_bool ? "✔" : "❌" } || "-" %> <%= instance[:flag]? ? "#{instance[:flag]} #{instance[:region]}" : "-" %>