mirror of
https://github.com/redlib-org/redlib.git
synced 2025-06-06 06:40:23 +00:00
feat: more p2p
This commit is contained in:
parent
362c025842
commit
88c09f6c2e
4 changed files with 16 additions and 9 deletions
|
@ -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
|
// 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> {
|
pub async fn json(path: String, quarantine: bool) -> Result<Value, String> {
|
||||||
// Closure to quickly build errors
|
// Closure to quickly build errors
|
||||||
let err = |msg: &str, e: String, path: String| -> Result<Value, String> {
|
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
|
// Parse the response from Reddit as JSON
|
||||||
match serde_json::from_reader(body.reader()) {
|
match serde_json::from_reader(body.reader()) {
|
||||||
Ok(value) => {
|
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;
|
let json: Value = value;
|
||||||
|
|
||||||
// If user is suspended
|
// If user is suspended
|
||||||
|
|
|
@ -282,7 +282,7 @@ async fn main() {
|
||||||
app.at("/copy.js").get(|_| resource(include_str!("../static/copy.js"), "text/javascript", false).boxed());
|
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("/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
|
// Proxy media through Redlib
|
||||||
app.at("/vid/:id/:size").get(|r| proxy(r, "https://v.redd.it/{id}/DASH_{size}").boxed());
|
app.at("/vid/:id/:size").get(|r| proxy(r, "https://v.redd.it/{id}/DASH_{size}").boxed());
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
use std::{
|
use std::{
|
||||||
str::FromStr,
|
collections::HashMap, str::FromStr, sync::atomic::{AtomicBool, Ordering}
|
||||||
sync::atomic::{AtomicBool, Ordering},
|
|
||||||
};
|
};
|
||||||
|
|
||||||
use bytes::Bytes;
|
use bytes::Bytes;
|
||||||
|
@ -21,7 +20,7 @@ use tokio::{task, time::sleep};
|
||||||
use crate::{config, p2p_mon::{Message, MessageLog}};
|
use crate::{config, p2p_mon::{Message, MessageLog}};
|
||||||
|
|
||||||
pub static DASHMAP: Lazy<DashMap<String, bool>> = Lazy::new(DashMap::new);
|
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>> {
|
pub async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
let endpoint = Endpoint::builder().discovery_n0().bind().await?;
|
let endpoint = Endpoint::builder().discovery_n0().bind().await?;
|
||||||
|
|
|
@ -41,10 +41,13 @@ async function checkOtherInstances() {
|
||||||
try {
|
try {
|
||||||
const response = await fetch('/instances.json');
|
const response = await fetch('/instances.json');
|
||||||
const data = await response.json();
|
const data = await response.json();
|
||||||
const randomInstance = data.instances[Math.floor(Math.random() * data.instances.length)];
|
// will look like {"http://localhost:8082":true,"http://localhost:8080":false,"http://localhost:8081":true}
|
||||||
const instanceUrl = randomInstance.url;
|
// filter by true values, then select randomly
|
||||||
// Set the href of the <a> tag to the instance URL with path included
|
const availableInstances = Object.keys(data).filter(key => data[key]);
|
||||||
document.getElementById('random-instance').href = instanceUrl + window.location.pathname;
|
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";
|
document.getElementById('random-instance').innerText = "Visit Random Instance";
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error fetching instances:', error);
|
console.error('Error fetching instances:', error);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue