Skip to content

Commit

Permalink
feat: fallback to default hub if official script not found on private…
Browse files Browse the repository at this point in the history
… hub (#3951)
  • Loading branch information
HugoCasa committed Jun 21, 2024
1 parent 53eeef0 commit 0c66122
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 10 deletions.
84 changes: 75 additions & 9 deletions backend/windmill-common/src/scripts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 _;
Expand Down Expand Up @@ -368,18 +368,51 @@ 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,
)
.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(
Expand All @@ -392,18 +425,51 @@ 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,
)
.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)]
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/lib/components/instanceSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit 0c66122

Please sign in to comment.