diff --git a/Cargo.toml b/Cargo.toml index 38c80d4..d54d191 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,7 +8,7 @@ axum = "0.8.9" curl = "0.4.51" futures = "0.3.34" ironhtml = "1.0.0" -mlua = { version = "0.12.2", features = ["lua54", "async", "vendored"] } +mlua = { version = "0.12.2", features = ["lua54", "send", "async", "vendored"] } scraper = "0.29.0" serde = { version = "1.0.229", features = ["derive"] } tokio = { version = "1.54.1", features = ["full"] } diff --git a/src/lua/engine_decoder.rs b/src/lua/engine_decoder.rs index 074acb6..e715bee 100644 --- a/src/lua/engine_decoder.rs +++ b/src/lua/engine_decoder.rs @@ -15,8 +15,6 @@ pub fn get_engine_vec( for pair in engine_table.pairs::() { let (name, functions) = pair.expect("Invalid Search Engine Pair"); - println!("{}", name); - let mut grabber_function: Option = None; let mut builder_function: Option = lua_instance .globals() diff --git a/src/lua/luahtml.rs b/src/lua/luahtml.rs index 8ab5f01..06bc9c5 100644 --- a/src/lua/luahtml.rs +++ b/src/lua/luahtml.rs @@ -1,30 +1,37 @@ -use crate::backend::scraper; +use crate::backend::{self, scraper}; use mlua::{Table, UserData, UserDataMethods}; use std::collections::HashMap; pub struct LuaHtml { - pub document: scraper::Html, + pub document: String, } impl UserData for LuaHtml { fn add_methods>(methods: &mut M) { // Allow documents to be implicitly converted to strings methods.add_meta_method(mlua::MetaMethod::ToString, |_, this, ()| { - Ok(this.document.root_element().html()) + let document = backend::get_sanitised_html_graph(&this.document); + Ok(document.root_element().html()) }); // Return table of all snippets (as LuaHtml objects) matching a certain css selector methods.add_method("filter", |lua, this, filter: String| { + let document = backend::get_sanitised_html_graph(&this.document); let selector = scraper::Selector::parse(&filter).map_err(mlua::Error::external)?; let output = lua.create_table()?; - for (index, element) in this.document.select(&selector).enumerate() { + for (index, element) in document.select(&selector).enumerate() { let html = element.html(); let document = scraper::Html::parse_fragment(&html); - output.set(index + 1, lua.create_userdata(LuaHtml { document })?)?; + output.set( + index + 1, + lua.create_userdata(LuaHtml { + document: document.html(), + })?, + )?; } Ok(output) @@ -33,6 +40,7 @@ impl UserData for LuaHtml { // Return a table of tables matching the input schema, with all found results // requires source attribute that matches one of the given fields, ie title methods.add_method("scrape", |lua, this, config: Table| { + let document = backend::get_sanitised_html_graph(&this.document); let mut selectors = Vec::new(); // Determine which configured selector provides the result containers. @@ -68,7 +76,7 @@ impl UserData for LuaHtml { })?; // Collect source elements. - let source_elements: Vec<_> = this.document.select(source_selector).collect(); + let source_elements: Vec<_> = document.select(source_selector).collect(); let mut results = Vec::new(); @@ -77,7 +85,7 @@ impl UserData for LuaHtml { let mut result = HashMap::new(); for (name, selector, attribute) in &selectors { - let value = this.document.select(selector).nth(index); + let value = document.select(selector).nth(index); let extracted = match (value, attribute) { (Some(element), Some(attribute)) => { @@ -113,8 +121,6 @@ impl UserData for LuaHtml { output.set(index + 1, result_table)?; } - // println!("output {:#?}", &output); - Ok(output) }); } diff --git a/src/lua/mod.rs b/src/lua/mod.rs index bee0783..ef6bc5c 100644 --- a/src/lua/mod.rs +++ b/src/lua/mod.rs @@ -48,10 +48,7 @@ fn create_lua_function_table(lua: &Lua) -> mlua::Result { idx_table.set( "decodeHtml", - lua.create_function(|_, result: String| { - let document = backend::get_sanitised_html_graph(&result); - Ok(LuaHtml { document }) - })?, + lua.create_function(|_, result: String| Ok(LuaHtml { document: result }))?, )?; Ok(idx_table) diff --git a/src/main.rs b/src/main.rs index e361cb1..36e9f61 100644 --- a/src/main.rs +++ b/src/main.rs @@ -30,18 +30,5 @@ async fn main() { } } - // // Long lived lua instance - // let mut lua_instance = lua::create_instance(); - // - // let engine_table: mlua::Table = - // lua::intialise_lua(&mut lua_instance, config_location).expect("Failed to initialise lua"); - // - // let engine_vec = engine_decoder::get_engine_vec(&mut lua_instance, engine_table) - // .expect("could not get engines"); - // - // let results = runner::run_engines(engine_vec, "test".to_string()).await; - // - // println!("{:#?}", results) - // - webserver::main_server().await; + webserver::main_server(config_location).await; } diff --git a/src/webserver/mod.rs b/src/webserver/mod.rs index be29f1d..0e374db 100644 --- a/src/webserver/mod.rs +++ b/src/webserver/mod.rs @@ -1,20 +1,51 @@ -use axum::{Router, extract::Query, routing::get}; +use crate::engine_decoder; +use crate::lua; +use axum::{ + Router, + extract::{Query, State}, + response::Html, + routing::get, +}; use serde; +use crate::runner; + #[derive(serde::Deserialize)] struct Params { - value: String, + query: String, } -pub async fn main_server() { +pub async fn main_server(config_location: Option) { // build our application with a single route - let app = Router::new().route("/", get(handler)); + let app = Router::new() + .route("/", get(handler)) + .with_state(config_location); // run our app with hyper, listening globally on port 3000 let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap(); axum::serve(listener, app).await.unwrap(); } -async fn handler(Query(params): Query) -> String { - format!("Value is: {}", params.value) +async fn handler( + State(config_location): State>, + Query(params): Query, +) -> Html { + // Long lived lua instance + let mut lua_instance = lua::create_instance(); + + let engine_table: mlua::Table = + lua::intialise_lua(&mut lua_instance, config_location).expect("Failed to initialise lua"); + + let engine_vec = engine_decoder::get_engine_vec(&mut lua_instance, engine_table) + .expect("could not get engines"); + + let results = runner::run_engines(engine_vec, params.query.to_string()).await; + + let html = results + .iter() + .map(|result| format!("
  • {:#?}
  • ", result)) + .collect::>() + .join(""); + + Html(format!("

    Engine Results

      {}
    ", html)) }