feat: add api functions and actix webserver
This commit is contained in:
15
src/handlers/hello.rs
Normal file
15
src/handlers/hello.rs
Normal 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
2
src/handlers/mod.rs
Normal file
@@ -0,0 +1,2 @@
|
||||
pub mod hello;
|
||||
pub mod search;
|
20
src/handlers/search.rs
Normal file
20
src/handlers/search.rs
Normal 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))
|
||||
}
|
Reference in New Issue
Block a user