From 2762a928f14fa68788921b95fc747fd2f4945525 Mon Sep 17 00:00:00 2001 From: Aksiome <54895777+aksiome@users.noreply.github.com> Date: Sun, 29 Dec 2024 02:32:30 +0100 Subject: [PATCH] Read always manifest from local disk first --- src/api/download.rs | 2 +- src/api/manifest.rs | 23 ++++++++++------------- src/api/versions.rs | 2 +- 3 files changed, 12 insertions(+), 15 deletions(-) diff --git a/src/api/download.rs b/src/api/download.rs index bab4510..4a7438b 100644 --- a/src/api/download.rs +++ b/src/api/download.rs @@ -142,7 +142,7 @@ async fn fetch_module( Ok(bytes) }, Err(_) => read_from_file(&disk_path).await - .context("Failed to fetch module from all sources"), + .context(format!("Failed to fetch module: {}", module.id)), } } diff --git a/src/api/manifest.rs b/src/api/manifest.rs index 911392b..4eb1301 100644 --- a/src/api/manifest.rs +++ b/src/api/manifest.rs @@ -23,23 +23,20 @@ pub async fn manifest(Path(version): Path) -> impl IntoResponse { #[cached(time = 86400, result = true)] pub async fn fetch_manifest(version: String) -> Result> { + let disk_path = format!("data/{}/manifest.json", version); + if StdPath::new(&disk_path).exists() { + return read_from_json_file(&disk_path).await.map(Some); + } + let versions = fetch_versions().await.context("Failed to fetch versions")?; if let Some(version) = versions.into_iter().find(|entry| entry.version == version) { - let disk_path = format!("data/{}/manifest.json", version.version); - - return match fetch_manifest_from_github(&version).await { - Ok(manifest) => { - if !StdPath::new(&disk_path).exists() { - write_to_json_file(&disk_path, &manifest).await?; - } - Ok(Some(manifest)) - }, - Err(_) => read_from_json_file(&disk_path).await.map(Some), - }; + let manifest = fetch_manifest_from_github(&version).await?; + write_to_json_file(&disk_path, &manifest).await?; + Ok(Some(manifest)) + } else { + Ok(None) } - - Ok(None) } async fn fetch_manifest_from_github(version: &Version) -> Result { diff --git a/src/api/versions.rs b/src/api/versions.rs index c199080..a550e9f 100644 --- a/src/api/versions.rs +++ b/src/api/versions.rs @@ -23,7 +23,7 @@ pub async fn versions() -> impl IntoResponse { } } -#[cached(time = 3600, result = true)] +#[cached(time = 600, result = true)] pub async fn fetch_versions() -> Result> { let disk_path = "data/versions.json"; match fetch_versions_from_github().await {