From f835c0b11fbe0ce069ef95d8938912dafb769276 Mon Sep 17 00:00:00 2001 From: Markus Wagner <71727154+PRTTMPRPHT@users.noreply.github.com> Date: Thu, 12 Dec 2024 12:48:29 +0100 Subject: [PATCH] =?UTF-8?q?feat:=20Change=20update=20server=20to=20a=20cus?= =?UTF-8?q?tom=20mirror=20so=20we=20can=20avoid=20rate=20li=E2=80=A6=20(#1?= =?UTF-8?q?452)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: Change update server to a custom mirror so we can avoid rate limits on the clients * feat: Update update server URL * feat: Add back the github latest.json as a fallback for tauri's auto updater * feat: Add fallback mechanism for loading the list of updates * feat: Add debug logs when querying updates --- desktop/src-tauri/src/updates.rs | 51 +++++++++++++++++++++++-------- desktop/src-tauri/tauri.conf.json | 1 + 2 files changed, 39 insertions(+), 13 deletions(-) diff --git a/desktop/src-tauri/src/updates.rs b/desktop/src-tauri/src/updates.rs index e43441792..51b8c41bc 100644 --- a/desktop/src-tauri/src/updates.rs +++ b/desktop/src-tauri/src/updates.rs @@ -15,7 +15,8 @@ use tokio::fs::File; use ts_rs::TS; const UPDATE_POLL_INTERVAL: std::time::Duration = std::time::Duration::from_secs(60 * 10); -const RELEASES_URL: &str = "https://api.github.com/repos/loft-sh/devpod/releases"; +const RELEASES_URL: &str = "https://update-server.devpod.sh/releases"; +const FALLBACK_RELEASES_URL: &str = "https://api.github.com/repos/loft-sh/devpod/releases"; #[derive(Error, Debug)] pub enum UpdateError { @@ -265,23 +266,47 @@ impl<'a> UpdateHelper<'a> { .clone()) } - pub async fn fetch_releases(&self) -> anyhow::Result { - let per_page = 50; - let page = 1; - + pub async fn fetch_releases_from_url(&self, url: &str) -> anyhow::Result> { let client = Client::builder().user_agent("loft-sh/devpod").build()?; - let request = client - .request(Method::GET, RELEASES_URL) - .query(&[("per_page", per_page), ("page", page)]) - .header("Accept", "application/vnd.github+json") - .header("X-GitHub-Api-Version", "2022-11-28"); - let releases = request + let response = client + .request(Method::GET, url) + .header("Accept", "application/vnd.github+json") + .header("X-GitHub-Api-Version", "2022-11-28") .send() - .await? + .await + .with_context(|| format!("Fetch releases from {}", url))?; + + if !response.status().is_success() { + return Err(anyhow::anyhow!( + "Status code {} from {}", + response.status(), + url + )); + } + + let releases = response .json::>() .await - .with_context(|| format!("Fetch releases from {}", RELEASES_URL))?; + .with_context(|| format!("Parse JSON from {}", url))?; + + Ok(releases) + } + + pub async fn fetch_releases(&self) -> anyhow::Result { + debug!("Querying releases from update server: {}", RELEASES_URL); + let releases = match self.fetch_releases_from_url(RELEASES_URL).await { + Ok(releases) => releases, + Err(_) => { + debug!("Query from main update server failed. Querying from fallback URL: {}", FALLBACK_RELEASES_URL); + match self.fetch_releases_from_url(FALLBACK_RELEASES_URL).await { + Ok(releases) => releases, + Err(e2) => { + return Err(e2).context("No endpoint delivered updates."); + } + } + } + }; let releases = &releases .into_iter() diff --git a/desktop/src-tauri/tauri.conf.json b/desktop/src-tauri/tauri.conf.json index 05ae6328f..c65784ad3 100644 --- a/desktop/src-tauri/tauri.conf.json +++ b/desktop/src-tauri/tauri.conf.json @@ -57,6 +57,7 @@ "installMode": "passive" }, "endpoints": [ + "https://update-server.devpod.sh/latest", "https://github.com/loft-sh/devpod/releases/latest/download/latest.json" ] }