feat(error): add new instance buttom

This commit is contained in:
Matthew Esposito 2024-11-19 16:30:37 -05:00
parent cb9a2a3c39
commit d3ba5f3efb
3 changed files with 40 additions and 0 deletions

View File

@ -223,6 +223,7 @@ async fn main() {
.get(|_| resource(include_str!("../static/check_update.js"), "text/javascript", false).boxed()); .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("/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 // 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());
@ -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() 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<Response<Body>, 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()
}

View File

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

View File

@ -7,6 +7,8 @@
<h3><a href="https://www.redditstatus.com/">Reddit Status</a></h3> <h3><a href="https://www.redditstatus.com/">Reddit Status</a></h3>
<br /> <br />
<h3 id="update-status"></h3> <h3 id="update-status"></h3>
<br />
<h3 id="update-status"><a id="random-instance"></a></h3>
<br> <br>
<div id="git_commit" data-value="{{ crate::instance_info::INSTANCE_INFO.git_commit }}"></div> <div id="git_commit" data-value="{{ crate::instance_info::INSTANCE_INFO.git_commit }}"></div>
<script src="/check_update.js"></script> <script src="/check_update.js"></script>