Skip to content

Commit

Permalink
client
Browse files Browse the repository at this point in the history
  • Loading branch information
tomfran committed Jan 22, 2024
1 parent 4a428f3 commit 68d4d64
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 77 deletions.
Binary file added .DS_Store
Binary file not shown.
6 changes: 2 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
[workspace]

members = [
"search",
"client"
]
resolver = "2"
members = ["search", "client"]
14 changes: 8 additions & 6 deletions client/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use search::query::QueryProcessor;
use serde::{Deserialize, Serialize};
use std::{
env,
fs::read_to_string,
sync::{Arc, Mutex},
time::Instant,
};
Expand Down Expand Up @@ -57,7 +58,6 @@ async fn main() {
axum::serve(listener, app).await.unwrap();
}

// utility struct to render templates
struct HtmlTemplate<T>(T);

impl<T> IntoResponse for HtmlTemplate<T>
Expand All @@ -77,7 +77,6 @@ where
}
}

// homepage
#[derive(Template)]
#[template(path = "index.html")]
struct Root {
Expand All @@ -91,12 +90,9 @@ async fn root(State(state): State<Arc<AppState>>) -> impl IntoResponse {
})
}

// query handler

#[derive(Deserialize, Debug)]
struct QueryRequest {
query: String,
// limit: usize,
}

#[derive(Template)]
Expand All @@ -111,6 +107,7 @@ struct Document {
id: u32,
score: f32,
path: String,
content: String,
}

async fn post_query(
Expand All @@ -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
Expand All @@ -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")
}
87 changes: 24 additions & 63 deletions client/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,84 +4,45 @@
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>search-rs</title>

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css">

<!-- htmx -->
<script src="https://unpkg.com/[email protected]"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/js/all.min.js"></script>
<script src="https://unpkg.com/htmx.org/dist/ext/json-enc.js"></script>

<!-- css -->
<script src="https://cdn.tailwindcss.com"></script>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link
href="https://fonts.googleapis.com/css2?family=Literata:ital,opsz,wght@0,7..72,200;0,7..72,300;0,7..72,400;0,7..72,500;0,7..72,600;0,7..72,700;0,7..72,800;0,7..72,900;1,7..72,200;1,7..72,300;1,7..72,400;1,7..72,500;1,7..72,600;1,7..72,700;1,7..72,800;1,7..72,900&display=swap"
rel="stylesheet">

<style>
body {
display: flex;
justify-content: center;
/* align-items: center; */
/* Center vertically */
/* height: 100vh; */
width: 60vw;
margin: auto;
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background-color: #1a1a1a;
color: #fff;
}

.search-container {
text-align: center;
margin-top: 100px;
}

.search-title {
font-size: 60px;
padding-bottom: 10px;
}

.search-bar-container {
position: relative;
display: inline-block;
}

.search-bar {
padding: 15px;
border: none;
border-radius: 10px;
outline: none;
font-size: 18px;
width: 400px;
background-color: #333;
color: #fff;
padding-left: 40px;
}

.search-icon {
position: absolute;
left: 15px;
top: 50%;
transform: translateY(-50%);
color: #ccc;
font-family: 'Literata', serif;
}
</style>

.search-time {
margin: 10px;
}

.search-result {
margin: 10px;
}
</style>
<title>search-rs</title>
</head>

<body>
<body class="bg-neutral-900 text-white">

<!-- Main Content -->
<div class="container mx-auto mt-16 max-w-3xl">

<div class="search-container">
<h1 class="search-title">index on {{index_path}}</h1>
<div class="search-bar-container">
<i class="fas fa-search search-icon"></i>
<input type="text" class="search-bar" name="query" autofocus hx-post="/query" hx-ext='json-enc'
<!-- Search Heading -->
<div class="mb-6">
<h1 class="text-3xl font-bold my-3">Index on {{index_path}}</h1>
<input type="text" class="w-full mt-10 p-4 rounded-md bg-neutral-800 text-white"
placeholder="Enter your search query..." autofocus name="query" hx-post="/query" hx-ext='json-enc'
hx-target=".search-results" hx-trigger="keyup[keyCode==13]">
</div>


<div class="search-results" hx-target=".search-results">
<!-- Search results will appear here -->

</div>
</div>

Expand Down
26 changes: 22 additions & 4 deletions client/templates/query.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,22 @@
<div class="search-time">Query completed in {{ time_ms }} ms<div>
{% for doc in documents %}
<div class="search-result">{{ loop.index }} - {{ doc.path }}</div>
{% endfor %}
<div>

<!-- Title -->
<h1 class="text-xl"> Search results</h1>
<h1 class=" font-light text-sm mt-1 mb-6">Query time: {{ time_ms }}ms</h1>

<!-- Documents -->
{% for doc in documents %}

<div>
<div class="bg-neutral-800 p-4 rounded-md shadow-md mb-6">
<h2 class="text-xl font-semibold mb-2">
{{ doc.path }}
</h2>
<p class="text-neutral-300">
{{ doc.content }}
</p>
</div>
</div>
{% endfor %}

</div>

0 comments on commit 68d4d64

Please sign in to comment.