feat: fallback to default hub if official script not found on private hub (#3951)

This commit is contained in:
HugoCasa
2024-06-22 01:29:08 +02:00
committed by GitHub
parent 98d3358e1e
commit e92d8655c4
2 changed files with 77 additions and 10 deletions
+75 -9
View File
@@ -14,7 +14,7 @@ use std::{
use crate::{
error::{to_anyhow, Error},
utils::http_get_from_hub,
DB, HUB_BASE_URL,
DB, DEFAULT_HUB_BASE_URL, HUB_BASE_URL,
};
use anyhow::Context;
use serde::de::Error as _;
@@ -368,9 +368,11 @@ pub async fn get_hub_script_by_path(
.strip_prefix("hub/")
.ok_or_else(|| Error::BadRequest("Impossible to remove prefix hex".to_string()))?;
let content = http_get_from_hub(
let hub_base_url = HUB_BASE_URL.read().await.clone();
let result = http_get_from_hub(
http_client,
&format!("{}/raw/{}.ts", *HUB_BASE_URL.read().await, path),
&format!("{}/raw/{}.ts", hub_base_url, path),
true,
None,
db,
@@ -378,8 +380,39 @@ pub async fn get_hub_script_by_path(
.await?
.text()
.await
.map_err(to_anyhow)?;
Ok(content)
.map_err(to_anyhow);
match result {
Ok(result) => Ok(result),
Err(e) => {
if hub_base_url != DEFAULT_HUB_BASE_URL
&& path
.split("/")
.next()
.is_some_and(|x| x.parse::<i32>().is_ok_and(|x| x < 10_000_000))
{
tracing::info!(
"Not found on private hub, fallback to default hub for {}",
path
);
let content = http_get_from_hub(
http_client,
&format!("{}/raw/{}.ts", DEFAULT_HUB_BASE_URL, path),
true,
None,
db,
)
.await?
.text()
.await
.map_err(to_anyhow)?;
Ok(content)
} else {
Err(e)?
}
}
}
}
pub async fn get_full_hub_script_by_path(
@@ -392,9 +425,11 @@ pub async fn get_full_hub_script_by_path(
.strip_prefix("hub/")
.ok_or_else(|| Error::BadRequest("Impossible to remove prefix hex".to_string()))?;
let value = http_get_from_hub(
let hub_base_url = HUB_BASE_URL.read().await.clone();
let result = http_get_from_hub(
http_client,
&format!("{}/raw2/{}", *HUB_BASE_URL.read().await, path),
&format!("{}/raw2/{}", hub_base_url, path),
true,
None,
db,
@@ -402,8 +437,39 @@ pub async fn get_full_hub_script_by_path(
.await?
.json::<HubScript>()
.await
.context("Decoding hub response to script")?;
Ok(value)
.context("Decoding hub response to script");
match result {
Ok(result) => Ok(result),
Err(e) => {
if hub_base_url != DEFAULT_HUB_BASE_URL
&& path
.split("/")
.next()
.is_some_and(|x| x.parse::<i32>().is_ok_and(|x| x < 10_000_000))
{
tracing::info!(
"Not found on private hub, fallback to default hub for {}",
path
);
let value = http_get_from_hub(
http_client,
&format!("{}/raw2/{}", DEFAULT_HUB_BASE_URL, path),
true,
None,
db,
)
.await?
.json::<HubScript>()
.await
.context("Decoding hub response to script")?;
Ok(value)
} else {
Err(e)?
}
}
}
}
#[derive(Deserialize, Serialize)]
@@ -131,7 +131,8 @@ export const settings: Record<string, Setting[]> = {
},
{
label: 'Private hub base url',
description: 'Base url of your private hub instance',
description: 'Base url of your private hub instance, without trailing slash',
placeholder: 'https://hub.company.com',
key: 'hub_base_url',
fieldType: 'text',
storage: 'setting',