feat: more p2p

This commit is contained in:
Matthew Esposito 2025-04-30 09:48:56 -04:00
parent 362c025842
commit 88c09f6c2e
4 changed files with 16 additions and 9 deletions

View file

@ -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<Value, String> {
// Closure to quickly build errors
let err = |msg: &str, e: String, path: String| -> Result<Value, String> {
@ -420,6 +420,11 @@ pub async fn json(path: String, quarantine: bool) -> Result<Value, String> {
// 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

View file

@ -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());

View file

@ -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<DashMap<String, bool>> = Lazy::new(DashMap::new);
pub static ONLINE: Lazy<AtomicBool> = Lazy::new(AtomicBool::default);
pub static ONLINE: Lazy<AtomicBool> = Lazy::new(|| AtomicBool::new(true));
pub async fn main() -> Result<(), Box<dyn std::error::Error>> {
let endpoint = Endpoint::builder().discovery_n0().bind().await?;

View file

@ -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 <a> 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 <a> 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);