functional webserver :DDDD
This commit is contained in:
@@ -8,7 +8,7 @@ axum = "0.8.9"
|
|||||||
curl = "0.4.51"
|
curl = "0.4.51"
|
||||||
futures = "0.3.34"
|
futures = "0.3.34"
|
||||||
ironhtml = "1.0.0"
|
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"
|
scraper = "0.29.0"
|
||||||
serde = { version = "1.0.229", features = ["derive"] }
|
serde = { version = "1.0.229", features = ["derive"] }
|
||||||
tokio = { version = "1.54.1", features = ["full"] }
|
tokio = { version = "1.54.1", features = ["full"] }
|
||||||
|
|||||||
@@ -15,8 +15,6 @@ pub fn get_engine_vec(
|
|||||||
for pair in engine_table.pairs::<String, mlua::Value>() {
|
for pair in engine_table.pairs::<String, mlua::Value>() {
|
||||||
let (name, functions) = pair.expect("Invalid Search Engine Pair");
|
let (name, functions) = pair.expect("Invalid Search Engine Pair");
|
||||||
|
|
||||||
println!("{}", name);
|
|
||||||
|
|
||||||
let mut grabber_function: Option<mlua::Function> = None;
|
let mut grabber_function: Option<mlua::Function> = None;
|
||||||
let mut builder_function: Option<mlua::Function> = lua_instance
|
let mut builder_function: Option<mlua::Function> = lua_instance
|
||||||
.globals()
|
.globals()
|
||||||
|
|||||||
@@ -1,30 +1,37 @@
|
|||||||
use crate::backend::scraper;
|
use crate::backend::{self, scraper};
|
||||||
use mlua::{Table, UserData, UserDataMethods};
|
use mlua::{Table, UserData, UserDataMethods};
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
pub struct LuaHtml {
|
pub struct LuaHtml {
|
||||||
pub document: scraper::Html,
|
pub document: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl UserData for LuaHtml {
|
impl UserData for LuaHtml {
|
||||||
fn add_methods<M: UserDataMethods<Self>>(methods: &mut M) {
|
fn add_methods<M: UserDataMethods<Self>>(methods: &mut M) {
|
||||||
// Allow documents to be implicitly converted to strings
|
// Allow documents to be implicitly converted to strings
|
||||||
methods.add_meta_method(mlua::MetaMethod::ToString, |_, this, ()| {
|
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
|
// Return table of all snippets (as LuaHtml objects) matching a certain css selector
|
||||||
methods.add_method("filter", |lua, this, filter: String| {
|
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 selector = scraper::Selector::parse(&filter).map_err(mlua::Error::external)?;
|
||||||
|
|
||||||
let output = lua.create_table()?;
|
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 html = element.html();
|
||||||
|
|
||||||
let document = scraper::Html::parse_fragment(&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)
|
Ok(output)
|
||||||
@@ -33,6 +40,7 @@ impl UserData for LuaHtml {
|
|||||||
// Return a table of tables matching the input schema, with all found results
|
// 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
|
// requires source attribute that matches one of the given fields, ie title
|
||||||
methods.add_method("scrape", |lua, this, config: Table| {
|
methods.add_method("scrape", |lua, this, config: Table| {
|
||||||
|
let document = backend::get_sanitised_html_graph(&this.document);
|
||||||
let mut selectors = Vec::new();
|
let mut selectors = Vec::new();
|
||||||
|
|
||||||
// Determine which configured selector provides the result containers.
|
// Determine which configured selector provides the result containers.
|
||||||
@@ -68,7 +76,7 @@ impl UserData for LuaHtml {
|
|||||||
})?;
|
})?;
|
||||||
|
|
||||||
// Collect source elements.
|
// 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();
|
let mut results = Vec::new();
|
||||||
|
|
||||||
@@ -77,7 +85,7 @@ impl UserData for LuaHtml {
|
|||||||
let mut result = HashMap::new();
|
let mut result = HashMap::new();
|
||||||
|
|
||||||
for (name, selector, attribute) in &selectors {
|
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) {
|
let extracted = match (value, attribute) {
|
||||||
(Some(element), Some(attribute)) => {
|
(Some(element), Some(attribute)) => {
|
||||||
@@ -113,8 +121,6 @@ impl UserData for LuaHtml {
|
|||||||
output.set(index + 1, result_table)?;
|
output.set(index + 1, result_table)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
// println!("output {:#?}", &output);
|
|
||||||
|
|
||||||
Ok(output)
|
Ok(output)
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -48,10 +48,7 @@ fn create_lua_function_table(lua: &Lua) -> mlua::Result<Table> {
|
|||||||
|
|
||||||
idx_table.set(
|
idx_table.set(
|
||||||
"decodeHtml",
|
"decodeHtml",
|
||||||
lua.create_function(|_, result: String| {
|
lua.create_function(|_, result: String| Ok(LuaHtml { document: result }))?,
|
||||||
let document = backend::get_sanitised_html_graph(&result);
|
|
||||||
Ok(LuaHtml { document })
|
|
||||||
})?,
|
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
Ok(idx_table)
|
Ok(idx_table)
|
||||||
|
|||||||
15
src/main.rs
15
src/main.rs
@@ -30,18 +30,5 @@ async fn main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// // Long lived lua instance
|
webserver::main_server(config_location).await;
|
||||||
// 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;
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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 serde;
|
||||||
|
|
||||||
|
use crate::runner;
|
||||||
|
|
||||||
#[derive(serde::Deserialize)]
|
#[derive(serde::Deserialize)]
|
||||||
struct Params {
|
struct Params {
|
||||||
value: String,
|
query: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn main_server() {
|
pub async fn main_server(config_location: Option<String>) {
|
||||||
// build our application with a single route
|
// 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
|
// run our app with hyper, listening globally on port 3000
|
||||||
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
|
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
|
||||||
axum::serve(listener, app).await.unwrap();
|
axum::serve(listener, app).await.unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn handler(Query(params): Query<Params>) -> String {
|
async fn handler(
|
||||||
format!("Value is: {}", params.value)
|
State(config_location): State<Option<String>>,
|
||||||
|
Query(params): Query<Params>,
|
||||||
|
) -> Html<String> {
|
||||||
|
// 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!("<li>{:#?}</li>", result))
|
||||||
|
.collect::<Vec<String>>()
|
||||||
|
.join("");
|
||||||
|
|
||||||
|
Html(format!("<h1>Engine Results</h1><ul>{}</ul>", html))
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user