Skip to content

Commit

Permalink
this is one of the cow business
Browse files Browse the repository at this point in the history
  • Loading branch information
amitu committed Jan 30, 2025
1 parent 161cc98 commit 8d2368f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 20 deletions.
34 changes: 18 additions & 16 deletions fastn-core/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,27 +139,29 @@ impl RequestConfig {
// Sanitize the mountpoint request.
// Get the package and sanitized path
let package1;
let new_path1;

// TODO: The shitty code written by me ever
let (path_with_package_name, document, path_params, extra_data) =
if fastn_core::file::is_static(path)? {
(path.to_string(), None, vec![], Default::default())
(path, None, vec![], Default::default())
} else {
let (path_with_package_name, sanitized_package, sanitized_path) =
match self.config.get_mountpoint_sanitized_path(path) {
Some((new_path, package, remaining_path, _)) => {
// Update the sitemap of the package, if it does not contain the sitemap information
new_path1 = new_path;
if package.name != self.config.package.name {
package1 = self
.config
.update_sitemap(package, preview_session_id)
.await?;
(new_path, &package1, remaining_path)
(new_path1.as_ref(), &package1, remaining_path)
} else {
(new_path, package, remaining_path)
(new_path1.as_ref(), package, remaining_path)
}
}
None => (path.to_string(), &self.config.package, path.to_string()),
None => (path, &self.config.package, path.to_string()),
};

// Getting `document` with dynamic parameters, if exists
Expand All @@ -179,7 +181,7 @@ impl RequestConfig {
(path_with_package_name, document, path_params, extra_data)
};

let path = path_with_package_name.as_str();
let path = path_with_package_name;

tracing::info!("resolved path: {path}");
tracing::info!(
Expand Down Expand Up @@ -588,14 +590,14 @@ impl Config {
// Output
// -/<todos-package-name>/add-todo/, <todos-package-name>, /add-todo/
// #[tracing::instrument(skip_all)]
pub fn get_mountpoint_sanitized_path(
&self,
path: &str,
pub fn get_mountpoint_sanitized_path<'a>(
&'a self,
path: &'a str,
) -> Option<(
std::borrow::Cow<'a, str>,
&'a fastn_core::Package,
String,
&fastn_core::Package,
String,
Option<&fastn_core::package::app::App>,
Option<&'a fastn_core::package::app::App>,
)> {
// Problem for recursive dependency is that only current package contains dependency,
// dependent package does not contain dependency
Expand All @@ -606,7 +608,7 @@ impl Config {
if path.starts_with(dash_path.as_str()) {
let path_without_package_name = path.trim_start_matches(dash_path.as_str());
return Some((
path.to_string(),
std::borrow::Cow::from(path),
&self.package,
path_without_package_name.to_string(),
None,
Expand All @@ -626,15 +628,15 @@ impl Config {
let package_name = dep.name.trim_matches('/');
let sanitized_path = path.trim_start_matches(mp.trim_start_matches('/'));
return Some((
format!("-/{package_name}/{sanitized_path}"),
std::borrow::Cow::from(format!("-/{package_name}/{sanitized_path}")),
dep,
sanitized_path.to_string(),
Some(app),
));
} else if path.starts_with(dash_path.as_str()) {
let path_without_package_name = path.trim_start_matches(dash_path.as_str());
return Some((
path.to_string(),
std::borrow::Cow::from(path),
dep,
path_without_package_name.to_string(),
Some(app),
Expand Down Expand Up @@ -790,9 +792,9 @@ impl Config {
let sanitized_id = self
.get_mountpoint_sanitized_path(id)
.map(|(x, _, _, _)| x)
.unwrap_or_else(|| id.to_string());
.unwrap_or_else(|| std::borrow::Cow::Borrowed(id));

let id = sanitized_id.as_str();
let id = sanitized_id.as_ref();
let id = if let Some(id) = id.strip_prefix("-/") {
id
} else {
Expand Down
10 changes: 6 additions & 4 deletions fastn-core/src/config/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,19 @@ pub fn get_clean_url(
));
}

let cow_1 = std::borrow::Cow::from(url);

let url = if url.starts_with("/-/") || url.starts_with("-/") {
url.to_string()
cow_1
} else {
config
.get_mountpoint_sanitized_path(url)
.map(|(u, _, _, _)| u)
.unwrap_or_else(|| url.to_string()) // TODO: Error possibly, in that return 404 from proxy
.unwrap_or_else(|| cow_1) // TODO: Error possibly, in that return 404 from proxy
};

// This is for current package
if let Some(remaining_url) = trim_package_name(url.as_str(), config.package.name.as_str()) {
if let Some(remaining_url) = trim_package_name(url.as_ref(), config.package.name.as_str()) {
if config.package.endpoints.is_empty() {
return Err(fastn_core::Error::GenericError(format!(
"package does not contain the endpoints: {:?}",
Expand Down Expand Up @@ -116,7 +118,7 @@ pub fn get_clean_url(
// Handle logic for apps
for app in config.package.apps.iter() {
if let Some(ep) = &app.end_point {
if let Some(remaining_url) = trim_package_name(url.as_str(), app.package.name.as_str())
if let Some(remaining_url) = trim_package_name(url.as_ref(), app.package.name.as_str())
{
let mut app_conf = app.config.clone();
if let Some(user_id) = &app.user_id {
Expand Down

0 comments on commit 8d2368f

Please sign in to comment.