mirror of
https://github.com/iv-org/instances-api.git
synced 2024-10-01 04:35:38 -04:00
Support 'sort_by' in JSON response
This commit is contained in:
parent
83d5942fb2
commit
51c67b3da6
@ -95,8 +95,37 @@ get "/" do |env|
|
|||||||
sort_by = env.params.query["sort_by"]?
|
sort_by = env.params.query["sort_by"]?
|
||||||
sort_by ||= "users"
|
sort_by ||= "users"
|
||||||
|
|
||||||
|
instances = sort_instances(INSTANCES, sort_by)
|
||||||
|
|
||||||
|
rendered "index"
|
||||||
|
end
|
||||||
|
|
||||||
|
get "/instances.json" do |env|
|
||||||
|
env.response.content_type = "application/json; charset=utf-8"
|
||||||
|
sort_by = env.params.query["sort_by"]?
|
||||||
|
sort_by ||= "users"
|
||||||
|
|
||||||
|
instances = sort_instances(INSTANCES, sort_by)
|
||||||
|
|
||||||
|
if env.params.query["pretty"]?.try &.== "1"
|
||||||
|
instances.to_pretty_json
|
||||||
|
else
|
||||||
|
instances.to_json
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
error 404 do |env|
|
||||||
|
env.redirect "/"
|
||||||
|
halt env, status_code: 302, response: ""
|
||||||
|
end
|
||||||
|
|
||||||
|
static_headers do |response, filepath, filestat|
|
||||||
|
response.headers.add("Cache-Control", "max-age=86400")
|
||||||
|
end
|
||||||
|
|
||||||
|
def sort_instances(instances, sort_by)
|
||||||
sort_proc = ->(instance : Tuple(String, Instance)) { instance[0] }
|
sort_proc = ->(instance : Tuple(String, Instance)) { instance[0] }
|
||||||
instances = INSTANCES.dup.to_a
|
instances = instances.to_a
|
||||||
|
|
||||||
case sort_by
|
case sort_by
|
||||||
when .starts_with? "name"
|
when .starts_with? "name"
|
||||||
@ -116,26 +145,7 @@ get "/" do |env|
|
|||||||
end
|
end
|
||||||
|
|
||||||
instances.reverse! if sort_by.ends_with?("-reverse")
|
instances.reverse! if sort_by.ends_with?("-reverse")
|
||||||
rendered "index"
|
instances
|
||||||
end
|
|
||||||
|
|
||||||
get "/instances.json" do |env|
|
|
||||||
env.response.content_type = "application/json; charset=utf-8"
|
|
||||||
|
|
||||||
if env.params.query["pretty"]?.try &.== "1"
|
|
||||||
INSTANCES.to_pretty_json
|
|
||||||
else
|
|
||||||
INSTANCES.to_json
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
error 404 do |env|
|
|
||||||
env.redirect "/"
|
|
||||||
halt env, status_code: 302, response: ""
|
|
||||||
end
|
|
||||||
|
|
||||||
static_headers do |response, filepath, filestat|
|
|
||||||
response.headers.add("Cache-Control", "max-age=86400")
|
|
||||||
end
|
end
|
||||||
|
|
||||||
gzip true
|
gzip true
|
||||||
|
@ -52,7 +52,7 @@
|
|||||||
<div class="header">
|
<div class="header">
|
||||||
<p>
|
<p>
|
||||||
<span>Add your instance <a href="https://github.com/omarroth/invidious/wiki/Invidious-Instances">here</a>.</span>
|
<span>Add your instance <a href="https://github.com/omarroth/invidious/wiki/Invidious-Instances">here</a>.</span>
|
||||||
<span class="right"><a href="/instances.json?pretty=1">JSON</a></span>
|
<span class="right"><a href="/instances.json?pretty=1<% if sort_by != "users" %>&sort_by=<%= sort_by %><% end %>">JSON</a></span>
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user