From dc669518084c23577ff6c3767c26b8edf1104aaf Mon Sep 17 00:00:00 2001 From: Erin Nova Date: Wed, 26 Jul 2023 09:06:41 -0400 Subject: [PATCH] Make search request --- Cargo.lock | 11 ++++++++ Cargo.toml | 1 + crawler/Cargo.toml | 2 +- frontend/Cargo.toml | 2 ++ frontend/src/main.rs | 65 ++++++++++++++++++++++++++++++-------------- 5 files changed, 60 insertions(+), 21 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f2dcb1c..3c9f169 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -385,6 +385,15 @@ dependencies = [ "winapi", ] +[[package]] +name = "chrono-humanize" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "799627e6b4d27827a814e837b9d8a504832086081806d45b1afa34dc982b023b" +dependencies = [ + "chrono", +] + [[package]] name = "config" version = "0.13.3" @@ -753,9 +762,11 @@ version = "0.1.0" dependencies = [ "axum", "chrono", + "chrono-humanize", "config", "dirs", "ramhorns", + "reqwest", "serde", "tokio", "tracing", diff --git a/Cargo.toml b/Cargo.toml index ff3a328..f71d7f9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,6 +20,7 @@ scraper = "0.17.1" axum = { version = "0.6.19", features = [ "http2", "tracing" ] } tracing-subscriber = "0.3.17" tracing = "0.1.37" +reqwest = { version = "0.11", default-features = false, features = [ "rustls-tls", "gzip", "brotli", "deflate", "json" ] } [profile.release] strip = true diff --git a/crawler/Cargo.toml b/crawler/Cargo.toml index f738a45..3279444 100644 --- a/crawler/Cargo.toml +++ b/crawler/Cargo.toml @@ -16,7 +16,7 @@ env_logger.workspace = true chrono.workspace = true sqlx.workspace = true scraper.workspace = true +reqwest.workspace = true serde = { version = "1.0.175", features = [ "derive" ] } -reqwest = { version = "0.11", default-features = false, features = [ "rustls-tls", "gzip", "brotli", "deflate" ] } cacache = { version = "11.6.0", default-features = false, features = ["tokio-runtime", "mmap"] } bincode = "1.3.3" diff --git a/frontend/Cargo.toml b/frontend/Cargo.toml index 7fd28ad..cb36a41 100644 --- a/frontend/Cargo.toml +++ b/frontend/Cargo.toml @@ -15,5 +15,7 @@ axum.workspace = true url.workspace = true tracing.workspace = true tracing-subscriber.workspace = true +reqwest.workspace = true ramhorns = "0.14.0" serde = "1.0.175" +chrono-humanize = "0.2.3" diff --git a/frontend/src/main.rs b/frontend/src/main.rs index d8b378c..75e3ea3 100644 --- a/frontend/src/main.rs +++ b/frontend/src/main.rs @@ -8,8 +8,11 @@ use axum::{ Json, Router, }; use chrono::{DateTime, NaiveDateTime, Utc}; +use chrono_humanize::HumanTime; use ramhorns::{Content, Template}; use serde::Deserialize; +use std::collections::BTreeMap; +use std::collections::HashMap; use std::fs::File; use std::io::prelude::*; use std::net::SocketAddr; @@ -31,7 +34,7 @@ async fn main() { .route("/search", post(search)) .route("/style.css", get(stylesheet)); - let addr = SocketAddr::from(([127, 0, 0, 1], 3000)); + let addr = SocketAddr::from(([127, 0, 0, 1], 3001)); tracing::debug!("listening on {}", addr); axum::Server::bind(&addr) @@ -72,7 +75,7 @@ struct SearchForm { } #[derive(Content)] -struct SearchResult { +struct RenderSearchResult { url: String, size: i64, title: String, @@ -80,12 +83,21 @@ struct SearchResult { last_updated: String, } +#[derive(Debug, Deserialize)] +struct SearchResult { + url: Url, + size: i64, + title: String, + summary: String, + last_updated: DateTime, +} + #[derive(Content)] struct SearchResults { title: String, description: String, query: String, - results: Vec, + results: Vec, } async fn search(Form(search): Form) -> Html { @@ -93,28 +105,41 @@ async fn search(Form(search): Form) -> Html { let mut contents = String::new(); source.read_to_string(&mut contents).unwrap(); + let mut map = HashMap::new(); + map.insert("language", "eng"); + map.insert("include", &search.query); + map.insert("option", "Fuzzy"); + + let client = reqwest::Client::new(); + let res = client + .get("http://127.0.0.1:3000/api/search") + .json(&map) + .send() + .await + .unwrap() + .json::>() + .await + .unwrap(); + + tracing::info!("{:#?}", res); + + let mut results = Vec::new(); + for (i, r) in res.iter().rev() { + results.push(RenderSearchResult { + url: r.url.as_str().to_string(), + size: r.size, + title: r.title.clone(), + summary: r.summary.clone(), + last_updated: HumanTime::from(r.last_updated).to_string(), + }); + } + let tpl = Template::new(contents).unwrap(); let rendered = tpl.render(&SearchResults { title: "Ferret".to_string(), description: "A small independent search engine".to_string(), query: search.query, - results: vec![ - SearchResult { - url: "https://example.com/".to_string(), - size: 0, - title: "Example Domain".to_string(), - summary: "This domain is for use in illustrative examples in documents. You may use this domain in literature without prior coordination or asking for permission.".to_string(), - last_updated: "one week ago".to_string(), - }, - SearchResult { - url: "https://example.com/".to_string(), - size: 0, - title: "Example Domain".to_string(), - summary: "This domain is for use in illustrative examples in documents. You may use this domain in literature without prior coordination or asking for permission.".to_string(), - last_updated: "one week ago".to_string(), - } - - ], + results, }); Html(rendered) }