Skip to content

Commit

Permalink
started with actix
Browse files Browse the repository at this point in the history
  • Loading branch information
tomfran committed Jan 20, 2024
1 parent a65de62 commit c27320f
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 13 deletions.
1 change: 1 addition & 0 deletions client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ edition = "2021"
[dependencies]
actix-web = "4.4.1"
search = { path = "../search" }
serde = "1.0.195"
75 changes: 62 additions & 13 deletions client/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,75 @@
use actix_web::{get, post, web, App, HttpResponse, HttpServer, Responder};
use actix_web::{post, web, App, HttpServer, Responder, Result};
use search::query::QueryProcessor;
use serde::{Deserialize, Serialize};
use std::{env, sync::Mutex, time::Instant};

#[get("/")]
async fn hello() -> impl Responder {
HttpResponse::Ok().body("Hello world!")
#[derive(Deserialize, Debug)]
struct QueryRequest {
query: String,
limit: usize,
}

#[post("/echo")]
async fn echo(req_body: String) -> impl Responder {
HttpResponse::Ok().body(req_body)
#[derive(Serialize)]
struct QueryResponse {
num_results: u32,
time_ms: u128,
documents: Vec<QueryDocumentResponse>,
}

async fn manual_hello() -> impl Responder {
HttpResponse::Ok().body("Hey there!")
#[derive(Serialize)]
struct QueryDocumentResponse {
id: u32,
score: f32,
path: String,
}

#[post("/query")]
async fn query(
r: web::Json<QueryRequest>,
q: web::Data<Mutex<QueryProcessor>>,
) -> Result<impl Responder> {
println!("query: {:?}", r);

let mut local_q = q.lock().unwrap();

let start_time = Instant::now();
let result = local_q.query(&r.query, r.limit);
let elapsed_time = start_time.elapsed();

let response = QueryResponse {
num_results: result.len() as u32,
time_ms: elapsed_time.as_millis(),
documents: result
.iter()
.map(|e| QueryDocumentResponse {
id: e.id,
score: e.score,
path: e.path.clone(),
})
.collect(),
};

Ok(web::Json(response))
}

#[actix_web::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(|| {
let args: Vec<String> = env::args().collect();
if args.len() < 2 {
println!("Usage: cargo run --bin client <base_path>");
return Ok(());
}

let base_path = &args[1];
let index_path = format!("{}/index/index", base_path);
let tokenizer_path = format!("{}/tokenizer/bert-base-uncased", base_path);

HttpServer::new(move || {
App::new()
.service(hello)
.service(echo)
.route("/hey", web::get().to(manual_hello))
.app_data(web::Data::new(Mutex::new(
QueryProcessor::build_query_processor(&index_path, &tokenizer_path),
)))
.service(query)
})
.bind(("127.0.0.1", 8080))?
.run()
Expand Down

0 comments on commit c27320f

Please sign in to comment.