diff --git a/src/main.rs b/src/main.rs index 4923921..7342597 100644 --- a/src/main.rs +++ b/src/main.rs @@ -223,6 +223,7 @@ async fn main() { .get(|_| resource(include_str!("../static/check_update.js"), "text/javascript", false).boxed()); app.at("/commits.atom").get(|_| async move { proxy_commit_info().await }.boxed()); + app.at("/instances.json").get(|_| async move { proxy_instances().await }.boxed()); // Proxy media through Redlib app.at("/vid/:id/:size").get(|r| proxy(r, "https://v.redd.it/{id}/DASH_{size}").boxed()); @@ -399,3 +400,22 @@ async fn fetch_commit_info() -> String { hyper::body::to_bytes(resp).await.expect("Failed to read body").iter().copied().map(|x| x as char).collect() } + +pub async fn proxy_instances() -> Result, String> { + Ok( + Response::builder() + .status(200) + .header("content-type", "application/json") + .body(Body::from(fetch_instances().await)) + .unwrap_or_default(), + ) +} + +#[cached(time = 600)] +async fn fetch_instances() -> String { + let uri = Uri::from_str("https://raw.githubusercontent.com/redlib-org/redlib-instances/refs/heads/main/instances.json").expect("Invalid URI"); + + let resp: Body = CLIENT.get(uri).await.expect("Failed to request GitHub").into_body(); + + hyper::body::to_bytes(resp).await.expect("Failed to read body").iter().copied().map(|x| x as char).collect() +} diff --git a/static/check_update.js b/static/check_update.js index b68c508..b747203 100644 --- a/static/check_update.js +++ b/static/check_update.js @@ -37,4 +37,22 @@ async function checkInstanceUpdateStatus() { } } +async function checkOtherInstances() { + try { + const response = await fetch('/instances.json'); + const data = await response.json(); + const randomInstance = data.instances[Math.floor(Math.random() * data.instances.length)]; + const instanceUrl = randomInstance.url; + // Set the href of the tag to the instance URL with path included + document.getElementById('random-instance').href = instanceUrl + window.location.pathname; + document.getElementById('random-instance').innerText = "Visit Random Instance"; + } catch (error) { + console.error('Error fetching instances:', error); + document.getElementById('update-status').innerText = '⚠️ Error checking update status.'; + } +} + +// Set the target URL when the page loads +window.addEventListener('load', checkOtherInstances); + checkInstanceUpdateStatus(); diff --git a/templates/error.html b/templates/error.html index 8f2f44d..2a56f68 100644 --- a/templates/error.html +++ b/templates/error.html @@ -7,6 +7,8 @@

Reddit Status


+
+