Skip to content

Commit

Permalink
Read always manifest from local disk first
Browse files Browse the repository at this point in the history
  • Loading branch information
aksiome committed Dec 29, 2024
1 parent b9af0b3 commit 2762a92
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/api/download.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
}
}

Expand Down
23 changes: 10 additions & 13 deletions src/api/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,20 @@ pub async fn manifest(Path(version): Path<String>) -> impl IntoResponse {

#[cached(time = 86400, result = true)]
pub async fn fetch_manifest(version: String) -> Result<Option<ManifestKind>> {
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<ManifestKind> {
Expand Down
2 changes: 1 addition & 1 deletion src/api/versions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Vec<Version>> {
let disk_path = "data/versions.json";
match fetch_versions_from_github().await {
Expand Down

0 comments on commit 2762a92

Please sign in to comment.