feat: add api functions and actix webserver

This commit is contained in:
2024-02-12 21:45:27 +01:00
parent af7140790c
commit c502f1da49
10 changed files with 87 additions and 38 deletions

15
src/handlers/hello.rs Normal file
View File

@@ -0,0 +1,15 @@
use actix_web::{get, post, HttpResponse, Responder};
#[get("/")]
pub async fn say_hello() -> impl Responder {
HttpResponse::Ok().body("Hello world!")
}
#[post("/echo")]
pub async fn echo(req_body: String) -> impl Responder {
HttpResponse::Ok().body(req_body)
}
pub async fn manual_hello() -> impl Responder {
HttpResponse::Ok().body("Hey there!")
}

2
src/handlers/mod.rs Normal file
View File

@@ -0,0 +1,2 @@
pub mod hello;
pub mod search;

20
src/handlers/search.rs Normal file
View File

@@ -0,0 +1,20 @@
use actix_web::{web, HttpResponse, Responder};
use serde::Deserialize;
use crate::types::app_state::AppStateWithSearchEngine;
#[derive(Deserialize)]
pub struct AddDocumentRequest {
url: String,
content: String,
}
pub async fn add_document_to_index(data: web::Data<AppStateWithSearchEngine>, req: web::Json<AddDocumentRequest>) -> impl Responder {
data.search_engine.lock().unwrap().index(&req.url, &req.content);
HttpResponse::Created().body("Document added to index!")
}
pub async fn get_number_of_documents(data: web::Data<AppStateWithSearchEngine>) -> impl Responder {
let number_of_documents = data.search_engine.lock().unwrap().number_of_documents();
HttpResponse::Ok().body(format!("Number of documents: {}", number_of_documents))
}