diff --git a/src/client.rs b/src/client.rs index 6bb2440..f893323 100644 --- a/src/client.rs +++ b/src/client.rs @@ -358,7 +358,7 @@ fn request(method: &'static Method, path: String, redirect: bool, quarantine: bo } // Make a request to a Reddit API and parse the JSON response -#[cached(size = 100, time = 30, result = true)] +// #[cached(size = 100, time = 30, result = true)] pub async fn json(path: String, quarantine: bool) -> Result { // Closure to quickly build errors let err = |msg: &str, e: String, path: String| -> Result { @@ -420,6 +420,11 @@ pub async fn json(path: String, quarantine: bool) -> Result { // Parse the response from Reddit as JSON match serde_json::from_reader(body.reader()) { Ok(value) => { + + if !ONLINE.load(Ordering::SeqCst) { + return Err("Reddit is having issues, check if there's an outage".to_string()); + } + let json: Value = value; // If user is suspended diff --git a/src/main.rs b/src/main.rs index c2195e9..6216de6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -282,7 +282,7 @@ async fn main() { app.at("/copy.js").get(|_| resource(include_str!("../static/copy.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()); + app.at("/instances.json").get(|_| async move { map_json().await }.boxed()); // Proxy media through Redlib app.at("/vid/:id/:size").get(|r| proxy(r, "https://v.redd.it/{id}/DASH_{size}").boxed()); diff --git a/src/p2p.rs b/src/p2p.rs index 42c205b..ab94726 100644 --- a/src/p2p.rs +++ b/src/p2p.rs @@ -1,6 +1,5 @@ use std::{ - str::FromStr, - sync::atomic::{AtomicBool, Ordering}, + collections::HashMap, str::FromStr, sync::atomic::{AtomicBool, Ordering} }; use bytes::Bytes; @@ -21,7 +20,7 @@ use tokio::{task, time::sleep}; use crate::{config, p2p_mon::{Message, MessageLog}}; pub static DASHMAP: Lazy> = Lazy::new(DashMap::new); -pub static ONLINE: Lazy = Lazy::new(AtomicBool::default); +pub static ONLINE: Lazy = Lazy::new(|| AtomicBool::new(true)); pub async fn main() -> Result<(), Box> { let endpoint = Endpoint::builder().discovery_n0().bind().await?; diff --git a/static/check_update.js b/static/check_update.js index 4cd745b..df1b9a2 100644 --- a/static/check_update.js +++ b/static/check_update.js @@ -41,10 +41,13 @@ 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; + // will look like {"http://localhost:8082":true,"http://localhost:8080":false,"http://localhost:8081":true} + // filter by true values, then select randomly + const availableInstances = Object.keys(data).filter(key => data[key]); + const randomInstance = availableInstances[Math.floor(Math.random() * availableInstances.length)]; + // Set the href of the tag to the instance URL with path included, if it exists + const pathname = window.location.pathname || ''; + document.getElementById('random-instance').href = randomInstance + pathname; document.getElementById('random-instance').innerText = "Visit Random Instance"; } catch (error) { console.error('Error fetching instances:', error);