mirror of
https://github.com/iv-org/instances-api.git
synced 2025-03-13 09:46:34 -04:00
implement random.invidious.io
automatically redirects requests to a random, healthy instance. the user may specify which region the instance should be from by setting the iv-region query parameter and the minimum 30 day uptime in percent with iv-health query parameter. if no instance matching these parameters can be found, a 404 error is returned instead. note that this behaviour is only enabled for the magic hostname 'random.invidious.io' in order to not send traffic to instances unless explicitly requested.
This commit is contained in:
parent
5301e08bd3
commit
a70d0c6147
@ -142,8 +142,26 @@ get "/instances.json" do |env|
|
||||
end
|
||||
|
||||
error 404 do |env|
|
||||
env.redirect "/"
|
||||
halt env, status_code: 302, response: ""
|
||||
if env.request.headers["host"]? != "random.invidious.io"
|
||||
next env.redirect "/"
|
||||
end
|
||||
|
||||
type = env.params.query["iv-type"]? || "https"
|
||||
region = env.params.query["iv-region"]?
|
||||
health = (env.params.query["iv-health"]?.try &.to_f || 95.0).clamp(0,100)
|
||||
api_call = env.request.path.starts_with?("/api/")
|
||||
|
||||
instances = INSTANCES.values.select {|v|
|
||||
v[:type] == type &&
|
||||
(!api_call || v[:api]) &&
|
||||
(!region || v[:region] == region) &&
|
||||
(v[:monitor]?.try &.["30dRatio"]["ratio"].as_s.to_f || 0.0) >= health
|
||||
}
|
||||
if instances.empty?
|
||||
next "no suitable instance found (region = #{region || "any"}, health > #{health}%)"
|
||||
end
|
||||
instance = instances.sample
|
||||
next env.redirect "#{instance[:uri]}#{env.request.resource}"
|
||||
end
|
||||
|
||||
static_headers do |response, filepath, filestat|
|
||||
|
Loading…
x
Reference in New Issue
Block a user