Skip to content

Commit d04e16e

Browse files
committed
fix(main): use result_fallback for fetch_commit_info and fetch_instances
1 parent 07d8afa commit d04e16e

1 file changed

Lines changed: 15 additions & 18 deletions

File tree

src/main.rs

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -230,10 +230,7 @@ async fn main() {
230230
.get(|_| resource(include_str!("../static/manifest.json"), "application/json", false).boxed());
231231
app.at("/robots.txt").get(|_| {
232232
resource(
233-
if match config::get_setting("REDLIB_ROBOTS_DISABLE_INDEXING") {
234-
Some(val) => val == "on",
235-
None => false,
236-
} {
233+
if redlib::utils::disable_indexing() {
237234
"User-agent: *\nDisallow: /"
238235
} else {
239236
"User-agent: *\nDisallow: /u/\nDisallow: /user/"
@@ -425,49 +422,49 @@ async fn main() {
425422
}
426423

427424
pub async fn proxy_commit_info() -> Result<Response<Body>, String> {
425+
let body = fetch_commit_info().await.unwrap_or_default();
428426
Ok(
429427
Response::builder()
430428
.status(200)
431429
.header("content-type", "application/atom+xml")
432-
.body(full(fetch_commit_info().await))
430+
.body(full(body))
433431
.unwrap_or_default(),
434432
)
435433
}
436434

437-
#[cached(time = 600)]
438-
async fn fetch_commit_info() -> String {
439-
// wreq uses http v1.x: pass URL as &str and call .send().await, then .bytes().await
435+
#[cached(time = 600, result = true, result_fallback = true)]
436+
async fn fetch_commit_info() -> Result<String, String> {
440437
let bytes = MEDIA_CLIENT
441438
.get("https://github.com/redlib-org/redlib/commits/main.atom")
442439
.send()
443440
.await
444-
.expect("Failed to request GitHub")
441+
.map_err(|e| e.to_string())?
445442
.bytes()
446443
.await
447-
.expect("Failed to read body");
448-
String::from_utf8_lossy(&bytes).into_owned()
444+
.map_err(|e| e.to_string())?;
445+
Ok(String::from_utf8_lossy(&bytes).into_owned())
449446
}
450447

451448
pub async fn proxy_instances() -> Result<Response<Body>, String> {
449+
let body = fetch_instances().await.unwrap_or_default();
452450
Ok(
453451
Response::builder()
454452
.status(200)
455453
.header("content-type", "application/json")
456-
.body(full(fetch_instances().await))
454+
.body(full(body))
457455
.unwrap_or_default(),
458456
)
459457
}
460458

461-
#[cached(time = 600)]
462-
async fn fetch_instances() -> String {
463-
// wreq uses http v1.x: pass URL as &str and call .send().await, then .bytes().await
459+
#[cached(time = 600, result = true, result_fallback = true)]
460+
async fn fetch_instances() -> Result<String, String> {
464461
let bytes = MEDIA_CLIENT
465462
.get("https://raw.githubusercontent.com/redlib-org/redlib-instances/refs/heads/main/instances.json")
466463
.send()
467464
.await
468-
.expect("Failed to request GitHub")
465+
.map_err(|e| e.to_string())?
469466
.bytes()
470467
.await
471-
.expect("Failed to read body");
472-
String::from_utf8_lossy(&bytes).into_owned()
468+
.map_err(|e| e.to_string())?;
469+
Ok(String::from_utf8_lossy(&bytes).into_owned())
473470
}

0 commit comments

Comments
 (0)