From 137c62ed5fd2f28e3e0fb10345ee04a8638be399 Mon Sep 17 00:00:00 2001 From: neon_arch Date: Sun, 30 Apr 2023 18:16:08 +0300 Subject: [PATCH] replace commandline arguments with config.lua & add support for changing themes & coloschemes --- Cargo.lock | 250 +++++++----------- Cargo.toml | 6 +- .../{ => colorschemes}/catppuccin-mocha.css | 0 .../static/{style.css => themes/simple.css} | 2 +- public/templates/404.html | 2 +- public/templates/about.html | 2 +- public/templates/header.html | 5 +- public/templates/index.html | 2 +- public/templates/search.html | 2 +- public/templates/settings.html | 2 +- src/bin/websurfx.rs | 53 +--- src/config_parser/mod.rs | 1 + src/config_parser/parser.rs | 62 +++++ src/lib.rs | 14 +- .../aggregation_models.rs | 10 +- src/server/routes.rs | 20 +- websurfx/config.lua | 4 + 17 files changed, 209 insertions(+), 228 deletions(-) rename public/static/{ => colorschemes}/catppuccin-mocha.css (100%) rename public/static/{style.css => themes/simple.css} (98%) create mode 100644 src/config_parser/mod.rs create mode 100644 src/config_parser/parser.rs create mode 100644 websurfx/config.lua diff --git a/Cargo.lock b/Cargo.lock index 67397e3..8323e12 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -15,7 +15,7 @@ dependencies = [ "log", "memchr", "pin-project-lite", - "tokio 1.27.0", + "tokio 1.28.0", "tokio-util", ] @@ -75,7 +75,7 @@ dependencies = [ "rand 0.8.5", "sha1", "smallvec 1.10.0", - "tokio 1.27.0", + "tokio 1.28.0", "tokio-util", "tracing", "zstd", @@ -111,7 +111,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "15265b6b8e2347670eb363c47fc8c75208b4a4994b27192f345fcbe707804f3e" dependencies = [ "futures-core", - "tokio 1.27.0", + "tokio 1.28.0", ] [[package]] @@ -128,7 +128,7 @@ dependencies = [ "mio 0.8.6", "num_cpus", "socket2", - "tokio 1.27.0", + "tokio 1.28.0", "tracing", ] @@ -268,55 +268,6 @@ dependencies = [ "alloc-no-stdlib", ] -[[package]] -name = "anstream" -version = "0.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9e579a7752471abc2a8268df8b20005e3eadd975f585398f17efcfd8d4927371" -dependencies = [ - "anstyle", - "anstyle-parse", - "anstyle-query", - "anstyle-wincon", - "colorchoice", - "is-terminal", - "utf8parse", -] - -[[package]] -name = "anstyle" -version = "1.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "41ed9a86bf92ae6580e0a31281f65a1b1d867c0cc68d5346e2ae128dddfa6a7d" - -[[package]] -name = "anstyle-parse" -version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e765fd216e48e067936442276d1d57399e37bce53c264d6fefbe298080cb57ee" -dependencies = [ - "utf8parse", -] - -[[package]] -name = "anstyle-query" -version = "1.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5ca11d4be1bab0c8bc8734a9aa7bf4ee8316d462a08c6ac5052f888fef5b494b" -dependencies = [ - "windows-sys 0.48.0", -] - -[[package]] -name = "anstyle-wincon" -version = "1.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4bcd8291a340dd8ac70e18878bc4501dd7b4ff970cfa21c207d36ece51ea88fd" -dependencies = [ - "anstyle", - "windows-sys 0.48.0", -] - [[package]] name = "askama_escape" version = "0.10.3" @@ -348,7 +299,7 @@ dependencies = [ "cc", "cfg-if 1.0.0", "libc", - "miniz_oxide", + "miniz_oxide 0.6.2", "object", "rustc-demangle", ] @@ -419,6 +370,15 @@ dependencies = [ "alloc-stdlib", ] +[[package]] +name = "bstr" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba3569f383e8f1598449f1a423e72e99569137b47740b1da11ef19af3d5c3223" +dependencies = [ + "memchr", +] + [[package]] name = "bumpalo" version = "3.12.1" @@ -478,48 +438,6 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" -[[package]] -name = "clap" -version = "4.2.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "956ac1f6381d8d82ab4684768f89c0ea3afe66925ceadb4eeb3fc452ffc55d62" -dependencies = [ - "clap_builder", - "clap_derive", - "once_cell", -] - -[[package]] -name = "clap_builder" -version = "4.2.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "84080e799e54cff944f4b4a4b0e71630b0e0443b25b985175c7dddc1a859b749" -dependencies = [ - "anstream", - "anstyle", - "bitflags", - "clap_lex", - "strsim", -] - -[[package]] -name = "clap_derive" -version = "4.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3f9644cd56d6b87dbe899ef8b053e331c0637664e9e21a33dfcdc36093f5c5c4" -dependencies = [ - "heck", - "proc-macro2 1.0.56", - "quote 1.0.26", - "syn 2.0.15", -] - -[[package]] -name = "clap_lex" -version = "0.4.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8a2dd5a6fe8c6e3502f568a6353e5273bbb15193ad9a89e457b9970798efbea1" - [[package]] name = "cloudabi" version = "0.0.3" @@ -529,12 +447,6 @@ dependencies = [ "bitflags", ] -[[package]] -name = "colorchoice" -version = "1.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7" - [[package]] name = "convert_case" version = "0.4.0" @@ -836,12 +748,12 @@ dependencies = [ [[package]] name = "flate2" -version = "1.0.25" +version = "1.0.26" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a8a2db397cb1c8772f31494cb8917e48cd1e64f0fa7efac59fbd741a0a8ce841" +checksum = "3b9429470923de8e8cbd4d2dc513535400b4b3fef0319fb5c4e1f520a7bef743" dependencies = [ "crc32fast", - "miniz_oxide", + "miniz_oxide 0.7.1", ] [[package]] @@ -1049,7 +961,7 @@ dependencies = [ "http 0.2.9", "indexmap", "slab", - "tokio 1.27.0", + "tokio 1.28.0", "tokio-util", "tracing", ] @@ -1075,12 +987,6 @@ version = "0.12.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" -[[package]] -name = "heck" -version = "0.4.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" - [[package]] name = "hermit-abi" version = "0.2.6" @@ -1241,7 +1147,7 @@ dependencies = [ "itoa 1.0.6", "pin-project-lite", "socket2", - "tokio 1.27.0", + "tokio 1.28.0", "tower-service", "tracing", "want 0.3.0", @@ -1269,7 +1175,7 @@ dependencies = [ "bytes 1.4.0", "hyper 0.14.26", "native-tls", - "tokio 1.27.0", + "tokio 1.28.0", "tokio-native-tls", ] @@ -1422,9 +1328,9 @@ checksum = "6a987beff54b60ffa6d51982e1aa1146bc42f19bd26be28b0586f252fccf5317" [[package]] name = "linux-raw-sys" -version = "0.3.3" +version = "0.3.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9b085a4f2cde5781fc4b1717f2e86c62f5cda49de7ba99a7c2eae02b61c9064c" +checksum = "b64f40e5e03e0d54f03845c8197d0291253cdbedfb1cb46b13c2c117554a9f4c" [[package]] name = "local-channel" @@ -1561,6 +1467,15 @@ dependencies = [ "adler", ] +[[package]] +name = "miniz_oxide" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7810e0be55b428ada41041c41f32c9f1a42817901b4ccf45fa3d4b6561e74c7" +dependencies = [ + "adler", +] + [[package]] name = "mio" version = "0.6.23" @@ -1645,6 +1560,15 @@ version = "0.1.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "72ef4a56884ca558e5ddb05a1d1e7e1bfd9a68d9ed024c21704cc98872dae1bb" +[[package]] +name = "num-traits" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "578ede34cf02f8924ab9447f50c28075b4d3e5b269972345e7e0372b38c6cdcd" +dependencies = [ + "autocfg 1.1.0", +] + [[package]] name = "num_cpus" version = "1.15.0" @@ -1672,9 +1596,9 @@ checksum = "b7e5500299e16ebb147ae15a00a942af264cf3688f47923b8fc2cd5858f23ad3" [[package]] name = "openssl" -version = "0.10.51" +version = "0.10.52" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "97ea2d98598bf9ada7ea6ee8a30fb74f9156b63bbe495d64ec2b87c269d2dda3" +checksum = "01b8574602df80f7b85fdfc5392fa884a4e3b3f4f35402c070ab34c3d3f78d56" dependencies = [ "bitflags", "cfg-if 1.0.0", @@ -1704,9 +1628,9 @@ checksum = "ff011a302c396a5197692431fc1948019154afc178baf7d8e37367442a4601cf" [[package]] name = "openssl-sys" -version = "0.9.86" +version = "0.9.87" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "992bac49bdbab4423199c654a5515bd2a6c6a23bf03f2dd3bdb7e5ae6259bc69" +checksum = "8e17f59264b2809d77ae94f0e1ebabc434773f370d6ca667bd223ea10e06cc7e" dependencies = [ "cc", "libc", @@ -1783,9 +1707,9 @@ checksum = "478c572c3d73181ff3c2539045f6eb99e5491218eae919370993b890cdbdd98e" [[package]] name = "pest" -version = "2.5.7" +version = "2.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7b1403e8401ad5dedea73c626b99758535b342502f8d1e361f4a2dd952749122" +checksum = "e68e84bfb01f0507134eac1e9b410a12ba379d064eab48c50ba4ce329a527b70" dependencies = [ "thiserror", "ucd-trie", @@ -1793,9 +1717,9 @@ dependencies = [ [[package]] name = "pest_derive" -version = "2.5.7" +version = "2.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "be99c4c1d2fc2769b1d00239431d711d08f6efedcecb8b6e30707160aee99c15" +checksum = "6b79d4c71c865a25a4322296122e3924d30bc8ee0834c8bfc8b95f7f054afbfb" dependencies = [ "pest", "pest_generator", @@ -1803,9 +1727,9 @@ dependencies = [ [[package]] name = "pest_generator" -version = "2.5.7" +version = "2.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e56094789873daa36164de2e822b3888c6ae4b4f9da555a1103587658c805b1e" +checksum = "6c435bf1076437b851ebc8edc3a18442796b30f1728ffea6262d59bbe28b077e" dependencies = [ "pest", "pest_meta", @@ -1816,9 +1740,9 @@ dependencies = [ [[package]] name = "pest_meta" -version = "2.5.7" +version = "2.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6733073c7cff3d8459fda0e42f13a047870242aed8b509fe98000928975f359e" +checksum = "745a452f8eb71e39ffd8ee32b3c5f51d03845f99786fa9b68db6ff509c505411" dependencies = [ "once_cell", "pest", @@ -2310,9 +2234,9 @@ dependencies = [ [[package]] name = "reqwest" -version = "0.11.16" +version = "0.11.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "27b71749df584b7f4cac2c426c127a7c785a5106cc98f7a8feb044115f0fa254" +checksum = "13293b639a097af28fc8a90f22add145a9c954e49d77da06263d58cf44d5fb91" dependencies = [ "base64 0.21.0", "bytes 1.4.0", @@ -2335,7 +2259,7 @@ dependencies = [ "serde", "serde_json", "serde_urlencoded 0.7.1", - "tokio 1.27.0", + "tokio 1.28.0", "tokio-native-tls", "tower-service", "url 2.3.1", @@ -2345,6 +2269,30 @@ dependencies = [ "winreg 0.10.1", ] +[[package]] +name = "rlua" +version = "0.19.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95b38117a836316ef62c02f6751e6d28e2eb53a1c35f0329427a9fb9c1c7b6a0" +dependencies = [ + "bitflags", + "bstr", + "libc", + "num-traits", + "rlua-lua54-sys", +] + +[[package]] +name = "rlua-lua54-sys" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23ae48797c3e76fb2c205fda8f30e28416a15b9fc1d649cc7cea9ff1fb9cf028" +dependencies = [ + "cc", + "libc", + "pkg-config", +] + [[package]] name = "rustc-demangle" version = "0.1.23" @@ -2371,9 +2319,9 @@ dependencies = [ [[package]] name = "rustix" -version = "0.37.13" +version = "0.37.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f79bef90eb6d984c72722595b5b1348ab39275a5e5123faca6863bf07d75a4e0" +checksum = "bc809f704c03a812ac71f22456c857be34185cac691a4316f27ab0f633bb9009" dependencies = [ "bitflags", "errno", @@ -2719,12 +2667,6 @@ version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b1884d1bc09741d466d9b14e6d37ac89d6909cbcac41dd9ae982d4d063bbedfc" -[[package]] -name = "strsim" -version = "0.10.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" - [[package]] name = "syn" version = "0.15.44" @@ -2897,9 +2839,9 @@ dependencies = [ [[package]] name = "tokio" -version = "1.27.0" +version = "1.28.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d0de47a4eecbe11f498978a9b29d792f0d2692d1dd003650c24c76510e3bc001" +checksum = "c3c786bf8134e5a3a166db9b29ab8f48134739014a3eca7bc6bfa95d673b136f" dependencies = [ "autocfg 1.1.0", "bytes 1.4.0", @@ -2911,7 +2853,7 @@ dependencies = [ "signal-hook-registry", "socket2", "tokio-macros", - "windows-sys 0.45.0", + "windows-sys 0.48.0", ] [[package]] @@ -2958,9 +2900,9 @@ dependencies = [ [[package]] name = "tokio-macros" -version = "2.0.0" +version = "2.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "61a573bdc87985e9d6ddeed1b3d864e8a302c847e40d647746df2f1de209d1ce" +checksum = "630bdcf245f78637c13ec01ffae6187cca34625e8c63150d424b59e55af2675e" dependencies = [ "proc-macro2 1.0.56", "quote 1.0.26", @@ -2974,7 +2916,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bbae76ab933c85776efabc971569dd6119c580d8f5d448769dec1764bf796ef2" dependencies = [ "native-tls", - "tokio 1.27.0", + "tokio 1.28.0", ] [[package]] @@ -3051,15 +2993,15 @@ dependencies = [ [[package]] name = "tokio-util" -version = "0.7.7" +version = "0.7.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5427d89453009325de0d8f342c9490009f76e999cb7672d77e46267448f7e6b2" +checksum = "806fe8c2c87eccc8b3267cbae29ed3ab2d0bd37fca70ab622e46aaa9375ddb7d" dependencies = [ "bytes 1.4.0", "futures-core", "futures-sink", "pin-project-lite", - "tokio 1.27.0", + "tokio 1.28.0", "tracing", ] @@ -3193,12 +3135,6 @@ version = "0.7.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "09cc8ee72d2a9becf2f2febe0205bbed8fc6615b7cb429ad062dc7b7ddd036a9" -[[package]] -name = "utf8parse" -version = "0.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a" - [[package]] name = "uuid" version = "0.7.4" @@ -3351,16 +3287,16 @@ version = "0.1.0" dependencies = [ "actix-files", "actix-web", - "clap", "env_logger", "fake-useragent", "handlebars", "log", - "reqwest 0.11.16", + "reqwest 0.11.17", + "rlua", "scraper", "serde", "serde_json", - "tokio 1.27.0", + "tokio 1.28.0", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 95955b7..3f38026 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,12 +9,12 @@ edition = "2021" reqwest = {version="*",features=["json"]} tokio = {version="*",features=["full"]} serde = {version="*",features=["derive"]} -handlebars = { version = "4.2.1", features = ["dir_source"] } +handlebars = { version = "4.3.6", features = ["dir_source"] } scraper = {version="*"} -actix-web = {version="4"} +actix-web = {version="4.3.1"} actix-files = {version="0.6.2"} serde_json = {version="*"} fake-useragent = {version="*"} -clap = {version="*", features = ["derive"] } env_logger = "0.10.0" log = "0.4.17" +rlua = {version="*"} diff --git a/public/static/catppuccin-mocha.css b/public/static/colorschemes/catppuccin-mocha.css similarity index 100% rename from public/static/catppuccin-mocha.css rename to public/static/colorschemes/catppuccin-mocha.css diff --git a/public/static/style.css b/public/static/themes/simple.css similarity index 98% rename from public/static/style.css rename to public/static/themes/simple.css index 9b3e9c0..b5e80e9 100644 --- a/public/static/style.css +++ b/public/static/themes/simple.css @@ -1,4 +1,4 @@ -@import url('./catppuccin-mocha.css'); +/* @import url('./catppuccin-mocha.css'); */ * { padding: 0; diff --git a/public/templates/404.html b/public/templates/404.html index 57a9898..a8a9ac7 100644 --- a/public/templates/404.html +++ b/public/templates/404.html @@ -1,4 +1,4 @@ -{{>header}} +{{>header this}}
Image of broken robot.
diff --git a/public/templates/about.html b/public/templates/about.html index 78c998a..56c2165 100644 --- a/public/templates/about.html +++ b/public/templates/about.html @@ -1,4 +1,4 @@ -{{>header}} +{{>header this}}

Websurfx

- + Websurfx - + + diff --git a/public/templates/index.html b/public/templates/index.html index a35fd78..64021e9 100644 --- a/public/templates/index.html +++ b/public/templates/index.html @@ -1,4 +1,4 @@ -{{>header}} +{{>header this}}
Websurfx meta-search engine logo {{>search_bar}} diff --git a/public/templates/search.html b/public/templates/search.html index 981fc40..c40e917 100644 --- a/public/templates/search.html +++ b/public/templates/search.html @@ -1,4 +1,4 @@ -{{>header}} +{{>header this.style}}
{{>search_bar}}
diff --git a/public/templates/settings.html b/public/templates/settings.html index c549bcc..4215d08 100644 --- a/public/templates/settings.html +++ b/public/templates/settings.html @@ -1,4 +1,4 @@ -{{>header}} +{{>header this}}

Page is under construction

diff --git a/src/bin/websurfx.rs b/src/bin/websurfx.rs index a79876f..fa21486 100644 --- a/src/bin/websurfx.rs +++ b/src/bin/websurfx.rs @@ -3,50 +3,10 @@ //! This module contains the main function which handles the logging of the application to the //! stdout and handles the command line arguments provided and launches the `websurfx` server. -use std::{ops::RangeInclusive, net::TcpListener}; +use std::net::TcpListener; -use clap::{command, Parser}; use env_logger::Env; -use websurfx::run; - -/// A commandline arguments struct. -#[derive(Parser, Debug, Default)] -#[clap(author = "neon_arch", version, about = "Websurfx server application")] -#[command(propagate_version = true)] -struct CliArgs { - #[clap(default_value_t = 8080, short, long,value_parser = is_port_in_range)] - /// provide port number in range [1024-65536] to launch the server on. - port: u16, -} - -const PORT_RANGE: RangeInclusive = 1024..=65535; - -/// A function to check whether port is valid u32 number or is in range -/// between [1024-65536] otherwise display an appropriate error message. -/// -/// # Arguments -/// -/// * `s` - Takes a commandline argument port as a string. -/// -/// # Error -/// -/// Check whether the provided argument to `--port` commandline option is a valid -/// u16 argument and returns it as a u16 value otherwise returns an error with an -/// appropriate error message. -fn is_port_in_range(s: &str) -> Result { - let port: usize = s - .parse() - .map_err(|_| format!("`{s}` is not a valid port number"))?; - if PORT_RANGE.contains(&port) { - Ok(port as u16) - } else { - Err(format!( - "port not found in range {}-{}", - PORT_RANGE.start(), - PORT_RANGE.end() - )) - } -} +use websurfx::{config_parser::parser::Config, run}; /// The function that launches the main server and registers all the routes of the website. /// @@ -56,14 +16,15 @@ fn is_port_in_range(s: &str) -> Result { /// available for being used for other applications. #[actix_web::main] async fn main() -> std::io::Result<()> { - let args = CliArgs::parse(); + // Initialize the parsed config file. + let config = Config::parse().unwrap(); // Initializing logging middleware with level set to default or info. env_logger::Builder::from_env(Env::default().default_filter_or("info")).init(); - log::info!("started server on port {}", args.port); + log::info!("started server on port {}", config.port); - let listener = TcpListener::bind(("127.0.0.1", args.port))?; + let listener = TcpListener::bind((config.binding_ip_addr.clone(), config.port))?; - run(listener)?.await + run(listener, config)?.await } diff --git a/src/config_parser/mod.rs b/src/config_parser/mod.rs new file mode 100644 index 0000000..67c567f --- /dev/null +++ b/src/config_parser/mod.rs @@ -0,0 +1 @@ +pub mod parser; diff --git a/src/config_parser/parser.rs b/src/config_parser/parser.rs new file mode 100644 index 0000000..6e9ab5b --- /dev/null +++ b/src/config_parser/parser.rs @@ -0,0 +1,62 @@ +//! This module provides the functionality to parse the lua config and convert the config options +//! into rust readable form. + +use rlua::Lua; +use serde::Serialize; +use std::fs; + +#[derive(Serialize, Clone)] +pub struct Style { + pub theme: String, + pub colorscheme: String, +} + +impl Style { + pub fn new(theme: String, colorscheme: String) -> Self { + Style { theme, colorscheme } + } +} + +/// A named struct which stores the parsed config file options. +/// +/// # Fields +// +/// * `port` - It stores the parsed port number option on which the server should launch. +/// * `binding_ip_addr` - It stores the parsed ip address option on which the server should launch +#[derive(Clone)] +pub struct Config { + pub port: u16, + pub binding_ip_addr: String, + pub style: Style, +} + +impl Config { + /// A function which parses the config.lua file and puts all the parsed options in the newly + /// contructed Config struct and returns it. + /// + /// # Error + /// + /// Returns a lua parse error if parsing of the config.lua file fails or has a syntax error + /// or io error if the config.lua file doesn't exists otherwise it returns a newly contructed + /// Config struct with all the parsed config options from the parsed config file. + pub fn parse() -> Result> { + let lua = Lua::new(); + + lua.context(|context| { + let globals = context.globals(); + + context + .load(&fs::read_to_string("./websurfx/config.lua")?) + .exec()?; + + Ok(Config { + port: globals.get::<_, u16>("port")?, + binding_ip_addr: globals.get::<_, String>("binding_ip_addr")?, + style: Style::new( + globals.get::<_, String>("theme")?, + globals.get::<_, String>("colorscheme")?, + ), + }) + }) + } +} diff --git a/src/lib.rs b/src/lib.rs index 696a07e..25d093e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,21 +1,22 @@ //! This main library module provides the functionality to provide and handle the Tcp server //! and register all the routes for the `websurfx` meta search engine website. +pub mod config_parser; pub mod engines; -pub mod server; pub mod search_results_handler; +pub mod server; use std::net::TcpListener; use crate::server::routes; use actix_files as fs; -use actix_web::{middleware::Logger, web, App, HttpServer, dev::Server}; +use actix_web::{dev::Server, middleware::Logger, web, App, HttpServer}; +use config_parser::parser::Config; use handlebars::Handlebars; - /// Runs the web server on the provided TCP listener and returns a `Server` instance. -/// +/// /// # Arguments /// /// * `listener` - A `TcpListener` instance representing the address and port to listen on. @@ -25,7 +26,7 @@ use handlebars::Handlebars; /// Returns a `Result` containing a `Server` instance on success, or an `std::io::Error` on failure. /// /// # Example -/// +/// /// ```rust /// use std::net::TcpListener; /// use websurfx::run; @@ -33,7 +34,7 @@ use handlebars::Handlebars; /// let listener = TcpListener::bind("127.0.0.1:8080").expect("Failed to bind address"); /// let server = run(listener).expect("Failed to start server"); /// ``` -pub fn run(listener: TcpListener) -> std::io::Result { +pub fn run(listener: TcpListener, config: Config) -> std::io::Result { let mut handlebars: Handlebars = Handlebars::new(); handlebars @@ -45,6 +46,7 @@ pub fn run(listener: TcpListener) -> std::io::Result { let server = HttpServer::new(move || { App::new() .app_data(handlebars_ref.clone()) + .app_data(web::Data::new(config.clone())) .wrap(Logger::default()) // added logging middleware for logging. // Serve images and static files (css and js files). .service(fs::Files::new("/static", "./public/static").show_files_listing()) diff --git a/src/search_results_handler/aggregation_models.rs b/src/search_results_handler/aggregation_models.rs index 2bf3055..bf6845f 100644 --- a/src/search_results_handler/aggregation_models.rs +++ b/src/search_results_handler/aggregation_models.rs @@ -3,6 +3,8 @@ use serde::Serialize; +use crate::config_parser::parser::Style; + /// A named struct to store and serialize the individual search result from all the scraped /// and aggregated search results from the upstream search engines. /// @@ -117,11 +119,12 @@ impl RawSearchResult { /// * `results` - Stores the individual serializable `SearchResult` struct into a vector of /// `SearchResult` structs. /// * `page_query` - Stores the current pages search query `q` provided in the search url. -#[derive(Debug, Serialize)] +#[derive(Serialize)] #[serde(rename_all = "camelCase")] pub struct SearchResults { pub results: Vec, pub page_query: String, + pub style: Style, } impl SearchResults { @@ -137,6 +140,11 @@ impl SearchResults { SearchResults { results, page_query, + style: Style::new("".to_string(), "".to_string()), } } + + pub fn add_style(&mut self, style: Style) { + self.style = style; + } } diff --git a/src/server/routes.rs b/src/server/routes.rs index 5a0914b..221bbbf 100644 --- a/src/server/routes.rs +++ b/src/server/routes.rs @@ -4,7 +4,7 @@ use std::fs::read_to_string; -use crate::search_results_handler::aggregator::aggregate; +use crate::{config_parser::parser::Config, search_results_handler::aggregator::aggregate}; use actix_web::{get, web, HttpRequest, HttpResponse}; use handlebars::Handlebars; use serde::Deserialize; @@ -27,8 +27,9 @@ struct SearchParams { #[get("/")] pub async fn index( hbs: web::Data>, + config: web::Data, ) -> Result> { - let page_content: String = hbs.render("index", &"").unwrap(); + let page_content: String = hbs.render("index", &config.style).unwrap(); Ok(HttpResponse::Ok().body(page_content)) } @@ -36,8 +37,9 @@ pub async fn index( /// website essentially the 404 error page. pub async fn not_found( hbs: web::Data>, + config: web::Data, ) -> Result> { - let page_content: String = hbs.render("404", &"")?; + let page_content: String = hbs.render("404", &config.style)?; Ok(HttpResponse::Ok() .content_type("text/html; charset=utf-8") @@ -52,7 +54,7 @@ pub async fn not_found( /// ```bash /// curl "http://127.0.0.1:8080/search?q=sweden&page=1" /// ``` -/// +/// /// Or /// /// ```bash @@ -62,6 +64,7 @@ pub async fn not_found( pub async fn search( hbs: web::Data>, req: HttpRequest, + config: web::Data, ) -> Result> { let params = web::Query::::from_query(req.query_string())?; match ¶ms.q { @@ -71,8 +74,9 @@ pub async fn search( .insert_header(("location", "/")) .finish()) } else { - let results_json: crate::search_results_handler::aggregation_models::SearchResults = + let mut results_json: crate::search_results_handler::aggregation_models::SearchResults = aggregate(query, params.page).await?; + results_json.add_style(config.style.clone()); let page_content: String = hbs.render("search", &results_json)?; Ok(HttpResponse::Ok().body(page_content)) } @@ -96,8 +100,9 @@ pub async fn robots_data(_req: HttpRequest) -> Result>, + config: web::Data, ) -> Result> { - let page_content: String = hbs.render("about", &"")?; + let page_content: String = hbs.render("about", &config.style)?; Ok(HttpResponse::Ok().body(page_content)) } @@ -105,8 +110,9 @@ pub async fn about( #[get("/settings")] pub async fn settings( hbs: web::Data>, + config: web::Data, ) -> Result> { - let page_content: String = hbs.render("settings", &"")?; + let page_content: String = hbs.render("settings", &config.style)?; Ok(HttpResponse::Ok().body(page_content)) } diff --git a/websurfx/config.lua b/websurfx/config.lua new file mode 100644 index 0000000..cc478b8 --- /dev/null +++ b/websurfx/config.lua @@ -0,0 +1,4 @@ +port = "8080" +binding_ip_addr = "127.0.0.1" +colorscheme = "catppuccin-mocha" +theme = "simple"