Skip to content

Commit

Permalink
Implement the capability to output the answer as clear text if the re…
Browse files Browse the repository at this point in the history
…quest originates from cURL

Implement the capability to output the answer as clear text if the request originates from cURL.

* Check if the request originates from cURL by inspecting the `User-Agent` header.
* Format the crawled services data as plain text for cURL requests.
* Output the answer as clear text if the request originates from cURL.
* Continue to render the HTML template for non-cURL requests.

---

For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/cofob/fastside?shareId=XXXX-XXXX-XXXX-XXXX).
  • Loading branch information
cofob committed Dec 31, 2024
1 parent 96d1ac6 commit a9419e2
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions fastside/src/routes/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ pub struct IndexTemplate<'a> {

#[get("/")]
async fn index(
req: web::HttpRequest,
crawler: web::Data<Crawler>,
loaded_data: web::Data<RwLock<LoadedData>>,
) -> actix_web::Result<impl Responder> {
Expand All @@ -46,6 +47,30 @@ async fn index(
return Err(RedirectError::from(SearchError::CrawlerNotFetchedYet))?;
};
let loaded_data_guard = loaded_data.read().await;

let user_agent = req
.headers()
.get("User-Agent")
.and_then(|ua| ua.to_str().ok())
.unwrap_or("");

if user_agent.contains("curl") {
let mut plain_text_output = String::new();
for (service_name, service) in &crawled_services.services {
plain_text_output.push_str(&format!("Service: {}\n", service_name));
for instance in &service.instances {
plain_text_output.push_str(&format!(
" Instance: {}\n Status: {:?}\n Tags: {:?}\n",
instance.url, instance.status, instance.tags
));
}
plain_text_output.push('\n');
}
return Ok(actix_web::HttpResponse::Ok()
.content_type("text/plain; charset=utf-8")
.body(plain_text_output));
}

let template = IndexTemplate {
services: &loaded_data_guard.services,
crawled_services: &crawled_services.services,
Expand Down

0 comments on commit a9419e2

Please sign in to comment.