Skip to content

Fix #395: cache is error-aware #396

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 19 additions & 25 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

use cached::proc_macro::cached;
use clap::{Arg, ArgAction, Command};
use std::str::FromStr;

use futures_lite::FutureExt;
use hyper::Uri;
use hyper::{header::HeaderValue, Body, Request, Response};
Expand Down Expand Up @@ -83,9 +81,9 @@ async fn resource(body: &str, content_type: &str, cache: bool) -> Result<Respons
.unwrap_or_default();

if cache {
if let Ok(val) = HeaderValue::from_str("public, max-age=1209600, s-maxage=86400") {
res.headers_mut().insert("Cache-Control", val);
}
res
.headers_mut()
.insert("Cache-Control", HeaderValue::from_static("public, max-age=1209600, s-maxage=86400"));
}

Ok(res)
Expand Down Expand Up @@ -260,8 +258,8 @@ async fn main() {
.get(|_| resource(include_str!("../static/check_update.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("/instances.json").get(|_| async move { proxy_instances().await }.boxed());
app.at("/commits.atom").get(|_| async move { Ok(proxy_commit_info().await.unwrap()) }.boxed()); // TODO: see below
app.at("/instances.json").get(|_| async move { Ok(proxy_instances().await.unwrap()) }.boxed()); // TODO: In the process of migrating error handling. (I recommend thiserror crate for dynamic errors.) No proper error handling yes, so functionality unimpacted.

// Proxy media through Redlib
app.at("/vid/:id/:size").get(|r| proxy(r, "https://v.redd.it/{id}/DASH_{size}").boxed());
Expand Down Expand Up @@ -421,40 +419,36 @@ async fn main() {
}
}

pub async fn proxy_commit_info() -> Result<Response<Body>, String> {
pub async fn proxy_commit_info() -> Result<Response<Body>, hyper::Error> {
Ok(
Response::builder()
.status(200)
.header("content-type", "application/atom+xml")
.body(Body::from(fetch_commit_info().await))
.body(Body::from(fetch_commit_info().await?))
.unwrap_or_default(),
)
}

#[cached(time = 600)]
async fn fetch_commit_info() -> String {
let uri = Uri::from_str("https://github.com/redlib-org/redlib/commits/main.atom").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()
#[cached(time = 600, result = true, result_fallback = true)]
async fn fetch_commit_info() -> Result<String, hyper::Error> {
let uri = Uri::from_static("https://github.com/redlib-org/redlib/commits/main.atom");
let resp: Body = CLIENT.get(uri).await?.into_body(); // Could fail if there is no internet
Ok(hyper::body::to_bytes(resp).await?.iter().copied().map(|x| x as char).collect())
}

pub async fn proxy_instances() -> Result<Response<Body>, String> {
pub async fn proxy_instances() -> Result<Response<Body>, hyper::Error> {
Ok(
Response::builder()
.status(200)
.header("content-type", "application/json")
.body(Body::from(fetch_instances().await))
.body(Body::from(fetch_instances().await?)) // Could fail if no internet
.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()
#[cached(time = 600, result = true, result_fallback = true)]
async fn fetch_instances() -> Result<String, hyper::Error> {
let uri = Uri::from_static("https://raw.githubusercontent.com/redlib-org/redlib-instances/refs/heads/main/instances.json");
let resp: Body = CLIENT.get(uri).await?.into_body(); // Could fail if no internet
Ok(hyper::body::to_bytes(resp).await?.iter().copied().map(|x| x as char).collect())
}