mirror of
https://gitea.invidious.io/iv-org/instances-api.git
synced 2024-08-15 00:53:15 +00:00
Support 'sort_by' in JSON response
This commit is contained in:
parent
83d5942fb2
commit
51c67b3da6
2 changed files with 32 additions and 22 deletions
|
@ -95,8 +95,37 @@ get "/" do |env|
|
|||
sort_by = env.params.query["sort_by"]?
|
||||
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] }
|
||||
instances = INSTANCES.dup.to_a
|
||||
instances = instances.to_a
|
||||
|
||||
case sort_by
|
||||
when .starts_with? "name"
|
||||
|
@ -116,26 +145,7 @@ get "/" do |env|
|
|||
end
|
||||
|
||||
instances.reverse! if sort_by.ends_with?("-reverse")
|
||||
rendered "index"
|
||||
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")
|
||||
instances
|
||||
end
|
||||
|
||||
gzip true
|
||||
|
|
|
@ -52,7 +52,7 @@
|
|||
<div class="header">
|
||||
<p>
|
||||
<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>
|
||||
</div>
|
||||
|
||||
|
|
Loading…
Reference in a new issue