From 68d4d64d71160a7797a781483a90c511433ed7fb Mon Sep 17 00:00:00 2001 From: Francesco Date: Sun, 21 Jan 2024 18:46:49 +0100 Subject: [PATCH] client --- .DS_Store | Bin 0 -> 6148 bytes Cargo.toml | 6 +-- client/src/main.rs | 14 +++--- client/templates/index.html | 87 ++++++++++-------------------------- client/templates/query.html | 26 +++++++++-- 5 files changed, 56 insertions(+), 77 deletions(-) create mode 100644 .DS_Store diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..4347e6ca8de7c960d2907176dcbb33224c0a1902 GIT binary patch literal 6148 zcmeHKL2uJA6n^eXn`|KU0Maf2lSi*8dOwa=CaZ*ogng%`t%Ig#@-rN7(XhEo0!Wf zO`!ODL>HUpw8rnV+*1RbM12V(I$P~S&HtCB`3Ke7HCMoVyUxxvABTN?tAJJD&nv+D zg9m5y4VD_!)`3J_0f2QBD?^@t7ntK2^bM98Q3Dg23e;3#t{6hoQSO;I-(aax(@B`i zhcG=0b3+lTceL-Ra1y>on_2~|0;>va>BknI|AWo<|5cKGvI@7?L?# y*9OODtp|SxXXCh1qoN=&$Fa)rQM?ORhB}WkK;K}g5hXDDBcNokiB;gQD)0--%Aykh literal 0 HcmV?d00001 diff --git a/Cargo.toml b/Cargo.toml index adcf252..0a74024 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,4 @@ [workspace] -members = [ - "search", - "client" -] \ No newline at end of file +resolver = "2" +members = ["search", "client"] diff --git a/client/src/main.rs b/client/src/main.rs index eb19fdb..4657dde 100644 --- a/client/src/main.rs +++ b/client/src/main.rs @@ -11,6 +11,7 @@ use search::query::QueryProcessor; use serde::{Deserialize, Serialize}; use std::{ env, + fs::read_to_string, sync::{Arc, Mutex}, time::Instant, }; @@ -57,7 +58,6 @@ async fn main() { axum::serve(listener, app).await.unwrap(); } -// utility struct to render templates struct HtmlTemplate(T); impl IntoResponse for HtmlTemplate @@ -77,7 +77,6 @@ where } } -// homepage #[derive(Template)] #[template(path = "index.html")] struct Root { @@ -91,12 +90,9 @@ async fn root(State(state): State>) -> impl IntoResponse { }) } -// query handler - #[derive(Deserialize, Debug)] struct QueryRequest { query: String, - // limit: usize, } #[derive(Template)] @@ -111,6 +107,7 @@ struct Document { id: u32, score: f32, path: String, + content: String, } async fn post_query( @@ -122,7 +119,7 @@ async fn post_query( let mut q = state.query_processor.lock().unwrap(); let start_time = Instant::now(); - let query_result = q.query(&payload.query, 10); + let query_result = q.query(&payload.query, 100); let time_ms = start_time.elapsed().as_millis(); let documents = query_result @@ -131,8 +128,13 @@ async fn post_query( id: r.id, score: r.score, path: r.path.clone(), + content: read_file_content(r.path.clone()), }) .collect(); HtmlTemplate(QueryResponse { time_ms, documents }) } + +fn read_file_content(path: String) -> String { + read_to_string(path).expect("error while reading file") +} diff --git a/client/templates/index.html b/client/templates/index.html index 9251bc6..cff157c 100644 --- a/client/templates/index.html +++ b/client/templates/index.html @@ -4,84 +4,45 @@ - search-rs - - + - + + + + + + - .search-time { - margin: 10px; - } - .search-result { - margin: 10px; - } - + search-rs - + + + +
-
-

index on {{index_path}}

-
- - +
+

Index on {{index_path}}

+
+ +
- +
diff --git a/client/templates/query.html b/client/templates/query.html index f6ba449..102cd76 100644 --- a/client/templates/query.html +++ b/client/templates/query.html @@ -1,4 +1,22 @@ -
Query completed in {{ time_ms }} ms
-{% for doc in documents %} -
{{ loop.index }} - {{ doc.path }}
-{% endfor %} \ No newline at end of file +
+ + +

Search results

+

Query time: {{ time_ms }}ms

+ + + {% for doc in documents %} + +
+
+

+ {{ doc.path }} +

+

+ {{ doc.content }} +

+
+
+ {% endfor %} + +
\ No newline at end of file