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 --- Cargo.lock | 154 +++++++++++++++++++++++++++++++++++++++++++++++++++- indexer/Cargo.toml | 1 + indexer/src/main.rs | 30 ++++++---- 3 files changed, 171 insertions(+), 14 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4f92603..343b3b3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -34,6 +34,29 @@ dependencies = [ "smallvec", ] +[[package]] +name = "actix-files" +version = "0.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d832782fac6ca7369a70c9ee9a20554623c5e51c76e190ad151780ebea1cf689" +dependencies = [ + "actix-http", + "actix-service", + "actix-utils", + "actix-web", + "askama_escape", + "bitflags", + "bytes 1.2.1", + "derive_more", + "futures-core", + "http-range", + "log", + "mime", + "mime_guess", + "percent-encoding", + "pin-project-lite 0.2.9", +] + [[package]] name = "actix-http" version = "3.2.2" @@ -44,7 +67,7 @@ dependencies = [ "actix-rt", "actix-service", "actix-utils", - "ahash", + "ahash 0.7.6", "base64", "bitflags", "brotli", @@ -158,7 +181,7 @@ dependencies = [ "actix-service", "actix-utils", "actix-web-codegen", - "ahash", + "ahash 0.7.6", "bytes 1.2.1", "bytestring", "cfg-if 1.0.0", @@ -213,6 +236,18 @@ dependencies = [ "version_check", ] +[[package]] +name = "ahash" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "464b3811b747f8f7ebc8849c9c728c39f6ac98a055edad93baf9eb330e3f8f9d" +dependencies = [ + "cfg-if 1.0.0", + "getrandom 0.2.7", + "once_cell", + "version_check", +] + [[package]] name = "aho-corasick" version = "0.7.19" @@ -243,6 +278,18 @@ version = "0.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d301b3b94cb4b2f23d7917810addbbaff90738e0ca2be692bd027e70d7e0330c" +[[package]] +name = "arc-swap" +version = "1.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "983cd8b9d4b02a6dc6ffa557262eb5858a27a0038ffffe21a0f133eaa819a164" + +[[package]] +name = "askama_escape" +version = "0.10.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "619743e34b5ba4e9703bba34deac3427c72507c7159f5fd030aea8cac0cfe341" + [[package]] name = "async-channel" version = "1.7.1" @@ -254,6 +301,17 @@ dependencies = [ "futures-core", ] +[[package]] +name = "async-trait" +version = "0.1.58" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e805d94e6b5001b651426cf4cd446b1ab5f319d27bab5c644f61de0a804360c" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "autocfg" version = "1.1.0" @@ -317,6 +375,18 @@ dependencies = [ "alloc-stdlib", ] +[[package]] +name = "bstr" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba3569f383e8f1598449f1a423e72e99569137b47740b1da11ef19af3d5c3223" +dependencies = [ + "lazy_static", + "memchr", + "regex-automata", + "serde", +] + [[package]] name = "bumpalo" version = "3.11.1" @@ -498,6 +568,28 @@ dependencies = [ "syn", ] +[[package]] +name = "csv" +version = "1.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "22813a6dc45b335f9bade10bf7271dc477e81113e89eb251a0bc2a8a81c536e1" +dependencies = [ + "bstr", + "csv-core", + "itoa 0.4.8", + "ryu", + "serde", +] + +[[package]] +name = "csv-core" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b2466559f260f48ad25fe6317b3c8dac77b5bdb5763ac7d9d6103530663bc90" +dependencies = [ + "memchr", +] + [[package]] name = "derive_more" version = "0.99.17" @@ -519,6 +611,7 @@ checksum = "adfbc57365a37acbd2ebf2b64d7e69bb766e2fea813521ed536f5d0520dcf86c" dependencies = [ "block-buffer", "crypto-common", + "subtle", ] [[package]] @@ -778,8 +871,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4eb1a864a501629691edf6c15a593b7a51eebaa1e8468e9ddc623de7c9b58ec6" dependencies = [ "cfg-if 1.0.0", + "js-sys", "libc", "wasi 0.11.0+wasi-snapshot-preview1", + "wasm-bindgen", ] [[package]] @@ -1004,6 +1099,15 @@ dependencies = [ "libc", ] +[[package]] +name = "hmac" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c49c37c09c17a53d937dfbb742eb3a961d65a994e6bcdcf37e7399d0cc8ab5e" +dependencies = [ + "digest", +] + [[package]] name = "html2text" version = "0.4.3" @@ -1066,6 +1170,12 @@ dependencies = [ "http", ] +[[package]] +name = "http-range" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "21dec9db110f5f872ed9699c3ecf50cf16f423502706ba5c72462e28d3157573" + [[package]] name = "httparse" version = "1.8.0" @@ -1150,6 +1260,7 @@ dependencies = [ "kuchiki", "lib", "scraper", + "serde", "serde_json", ] @@ -1965,6 +2076,12 @@ dependencies = [ "regex-syntax", ] +[[package]] +name = "regex-automata" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c230d73fb8d8c1b9c0b3135c5142a8acee3a0558fb8db5cf1cb65f8d7862132" + [[package]] name = "regex-syntax" version = "0.6.27" @@ -2156,6 +2273,19 @@ dependencies = [ "syn", ] +[[package]] +name = "serde_html_form" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3312ac3bf56e70cb7082a85db89d127940607b6acf39bd537cc06c8212124927" +dependencies = [ + "form_urlencoded", + "indexmap", + "itoa 1.0.4", + "ryu", + "serde", +] + [[package]] name = "serde_json" version = "1.0.87" @@ -2327,6 +2457,12 @@ dependencies = [ "syn", ] +[[package]] +name = "subtle" +version = "2.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6bdef32e8150c2a081110b42772ffe7d7c9032b606bc226c8260fd97e0976601" + [[package]] name = "syn" version = "1.0.102" @@ -2442,7 +2578,7 @@ dependencies = [ "pin-project-lite 0.1.12", "signal-hook-registry", "slab", - "tokio-macros", + "tokio-macros 0.2.6", "winapi 0.3.9", ] @@ -2461,6 +2597,7 @@ dependencies = [ "pin-project-lite 0.2.9", "signal-hook-registry", "socket2 0.4.7", + "tokio-macros 1.8.0", "winapi 0.3.9", ] @@ -2475,6 +2612,17 @@ dependencies = [ "syn", ] +[[package]] +name = "tokio-macros" +version = "1.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9724f9a975fb987ef7a3cd9be0350edcbe130698af5b8f7a631e23d42d052484" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "tokio-stream" version = "0.1.11" 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