Skip to content

Commit

Permalink
For user requests redirect even if link does not exists
Browse files Browse the repository at this point in the history
  • Loading branch information
JElgar committed Oct 5, 2024
1 parent e46ffd8 commit 0798390
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 24 deletions.
10 changes: 10 additions & 0 deletions src/crawler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,13 @@ pub fn is_crawler(user_agent: &str) -> bool {
.iter()
.any(|crawler| crawler.pattern.is_match(user_agent))
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_is_crawler() {
assert_eq!(is_crawler("abc"), false);
}
}
8 changes: 8 additions & 0 deletions src/dao/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ pub async fn get_link(
.fetch_optional(pool)
.await;

if matches!(result, Ok(None)) {
tracing::info!("No link found by id link_id={link_id}");
}

result.map_err(|err| {
tracing::error!("Error whilst fetching link link_id={link_id} err={err}");
Error::UnknownError(Box::new(err))
Expand All @@ -123,6 +127,10 @@ pub async fn get_link_by_link_path(
.fetch_optional(pool)
.await;

if matches!(result, Ok(None)) {
tracing::info!("No link found by url path link_path={link_path}");
}

result.map_err(|err| {
tracing::error!("Error whilst creating link err={err}");
Error::UnknownError(Box::new(err))
Expand Down
50 changes: 26 additions & 24 deletions src/link_router/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,31 +77,33 @@ async fn link_handler(
) -> impl IntoResponse {
tracing::info!("Handling link link_path={link_path} request_actor={request_actor:?}");

match dao::get_link_by_link_path(&app_context.pool, &link_path).await {
Ok(Some(link)) => match request_actor {
RequestActor::Crawler => {
let response = CrawlerResponseTemplate {
og_title: link.title,
og_description: link.description,
og_url: link.link_path,
og_image_url: link.image_url,
og_type: "website".to_string(),
};
Html(response.render().unwrap()).into_response()
match request_actor {
RequestActor::Crawler => {
match dao::get_link_by_link_path(&app_context.pool, &link_path).await {
Ok(Some(link)) => {
let response = CrawlerResponseTemplate {
og_title: link.title,
og_description: link.description,
og_url: link.link_path,
og_image_url: link.image_url,
og_type: "website".to_string(),
};
Html(response.render().unwrap()).into_response()
}
Ok(None) => Response::builder()
.status(StatusCode::NOT_FOUND)
.body("Link not found".to_string()),
Err(_) => Response::builder()
.status(StatusCode::INTERNAL_SERVER_ERROR)
.body("Unknown error".to_string()),
}
RequestActor::User(platform) => Redirect::temporary(match platform {
Platform::Android => &fallback_data.android_fallback,
Platform::Ios => &fallback_data.ios_fallback,
Platform::Web | Platform::Unknown => &fallback_data.web_fallback,
})
.into_response(),
},
Ok(None) => Response::builder()
.status(StatusCode::NOT_FOUND)
.body("Link not found".to_string()),
Err(_) => Response::builder()
.status(StatusCode::INTERNAL_SERVER_ERROR)
.body("Unknown error".to_string()),
}
RequestActor::User(platform) => Redirect::temporary(match platform {
Platform::Android => &fallback_data.android_fallback,
Platform::Ios => &fallback_data.ios_fallback,
Platform::Web | Platform::Unknown => &fallback_data.web_fallback,
})
.into_response(),
}
}

Expand Down

0 comments on commit 0798390

Please sign in to comment.