From f2baf7d9ff4449c5a0ab3d71ebaf12b1dd219861 Mon Sep 17 00:00:00 2001 From: Baitinq Date: Fri, 4 Nov 2022 20:50:41 +0100 Subject: Indexer: Actix: Use the same service handler with multiple routes --- indexer/Cargo.toml | 1 + indexer/src/main.rs | 30 +++++++++++++++++++----------- 2 files changed, 20 insertions(+), 11 deletions(-) (limited to 'indexer') diff --git a/indexer/Cargo.toml b/indexer/Cargo.toml index 2c8f905..5aa5184 100644 --- a/indexer/Cargo.toml +++ b/indexer/Cargo.toml @@ -10,6 +10,7 @@ actix-web = "4.2.1" actix-cors = "0.6.3" scraper = "0.12.0" html2text = "0.4.3" +serde = { version = "1.0", features = ["derive", "rc"] } serde_json = "1.0.87" kuchiki = "0.8.1" lib = { path = "../lib" } diff --git a/indexer/src/main.rs b/indexer/src/main.rs index 6e41cfb..952af96 100644 --- a/indexer/src/main.rs +++ b/indexer/src/main.rs @@ -1,10 +1,11 @@ mod indexer_implementation; use actix_cors::Cors; -use actix_web::{get, post, routes, web, App, HttpServer, Responder}; +use actix_web::{post, web, App, HttpRequest, HttpServer, Responder}; use indexer_implementation::IndexerImplementation; use kuchiki::traits::TendrilSink; use lib::lib::*; +use serde::Deserialize; use std::collections::HashSet; use std::sync::Mutex; @@ -43,9 +44,8 @@ async fn serve_http_endpoint(address: &str, port: u16) -> std::io::Result<()> { App::new() .wrap(cors) .app_data(shared_state.clone()) - .service(no_search) - .service(search) .service(add_resource) + .service(web::resource(["/search", "/search/", "/search/{query}"]).to(search)) }) .bind((address, port))? .run() @@ -142,18 +142,26 @@ async fn add_resource( format!("{resource:?}") } -#[routes] -#[get("/search")] -#[get("/search/")] -async fn no_search(_data: web::Data) -> impl Responder { - "[]".to_string() +#[derive(Debug, Deserialize)] +struct OptSearchPath { + query: Option, } -#[get("/search/{term}")] -async fn search(data: web::Data, term: web::Path) -> impl Responder { +async fn search( + _req: HttpRequest, + data: web::Data, + path: web::Path, +) -> impl Responder { let indexer = data.indexer.lock().unwrap(); - let results = indexer.search(&term); + let query = match &path.query { + Some(query) => query, + None => return "[]".to_string(), + }; + + println!("Query: {:?}", query); + + let results = indexer.search(query); //+is lowercase search good (we turn ascii lowercase, what do we do with inserting) serde_json::to_string(&results.unwrap()).unwrap() -- cgit 1.4.1