feat: refactor code and add new search functionality
This commit is contained in:
parent
2acbdb9f37
commit
242f7138f2
@ -1,15 +1,6 @@
|
|||||||
use actix_web::{get, post, HttpResponse, Responder};
|
use actix_web::{get, HttpResponse, Responder};
|
||||||
|
|
||||||
#[get("/")]
|
#[get("/")]
|
||||||
pub async fn say_hello() -> impl Responder {
|
pub async fn say_hello() -> impl Responder {
|
||||||
HttpResponse::Ok().body("Hello world!")
|
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!")
|
|
||||||
}
|
}
|
@ -24,11 +24,12 @@ async fn main() -> std::io::Result<()> {
|
|||||||
App::new()
|
App::new()
|
||||||
// Inject the search engine into the application state
|
// Inject the search engine into the application state
|
||||||
.app_data(app_state.clone())
|
.app_data(app_state.clone())
|
||||||
|
|
||||||
// enable logger
|
// enable logger
|
||||||
.wrap(Logger::default())
|
.wrap(Logger::default())
|
||||||
|
|
||||||
.service(hello::say_hello)
|
.service(hello::say_hello)
|
||||||
.service(hello::echo)
|
|
||||||
.route("/hey", web::get().to(hello::manual_hello))
|
|
||||||
// Search Routes
|
// Search Routes
|
||||||
.route(
|
.route(
|
||||||
"/search/index/document",
|
"/search/index/document",
|
||||||
|
@ -34,4 +34,51 @@ mod tests {
|
|||||||
let resp = test::call_service(&app, req).await;
|
let resp = test::call_service(&app, req).await;
|
||||||
assert!(resp.status() == 201);
|
assert!(resp.status() == 201);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[actix_web::test]
|
||||||
|
async fn test_get_number_of_documents() {
|
||||||
|
let mut search_engine = SearchEngine::new(1.5, 0.75);
|
||||||
|
search_engine.index("https://example.com", "This is an example document");
|
||||||
|
|
||||||
|
let app_state = web::Data::new(AppStateWithSearchEngine {
|
||||||
|
search_engine: Mutex::new(search_engine.clone()),
|
||||||
|
});
|
||||||
|
|
||||||
|
let app = test::init_service(App::new()
|
||||||
|
.app_data(app_state.clone())
|
||||||
|
.route(
|
||||||
|
"/search/index/number_of_documents",
|
||||||
|
web::get().to(search::get_number_of_documents),
|
||||||
|
)
|
||||||
|
).await;
|
||||||
|
|
||||||
|
let req = test::TestRequest::get()
|
||||||
|
.uri("/search/index/number_of_documents")
|
||||||
|
.to_request();
|
||||||
|
|
||||||
|
let resp = test::call_service(&app, req).await;
|
||||||
|
assert!(resp.status() == 200);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[actix_web::test]
|
||||||
|
async fn test_search() {
|
||||||
|
let mut search_engine = SearchEngine::new(1.5, 0.75);
|
||||||
|
search_engine.index("https://example.com", "This is an example document");
|
||||||
|
|
||||||
|
let app_state = web::Data::new(AppStateWithSearchEngine {
|
||||||
|
search_engine: Mutex::new(search_engine.clone()),
|
||||||
|
});
|
||||||
|
|
||||||
|
let app = test::init_service(App::new()
|
||||||
|
.app_data(app_state.clone())
|
||||||
|
.route("/search", web::get().to(search::search))
|
||||||
|
).await;
|
||||||
|
|
||||||
|
let req = test::TestRequest::get()
|
||||||
|
.uri("/search?query=example")
|
||||||
|
.to_request();
|
||||||
|
|
||||||
|
let resp = test::call_service(&app, req).await;
|
||||||
|
assert!(resp.status() == 200);
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user