0
0
mirror of https://github.com/neon-mmd/websurfx.git synced 2024-11-23 14:38:21 -05:00
websurfx/src/bin/websurfx.rs

55 lines
1.7 KiB
Rust
Raw Normal View History

2023-04-27 10:53:28 -04:00
//! Main module of the application
//!
//! 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.
✨ Compression and encryption for the cached search results (#443) * attempt1 * rough draft * add features and their optional dependancies * add encryption and compression error variants * add a sample implementation to cache trait * Update src/cache/cacher.rs Co-authored-by: neon_arch <mustafadhuleb53@gmail.com> * adjust comment so feature flag would apply? * adjust feature flag so it applies? * formatting * Update src/cache/cacher.rs update documentation Co-authored-by: neon_arch <mustafadhuleb53@gmail.com> * [features]Add base64 and chacha20 dependencies for compress-cache-results and encrypt-cache-results * move encryption key and cipher logic to separate sub module * added cacha20 and cec-results feature * added cacha20 and cec-results feature * added compression and encryption helper functions to trait implementations * added compression and encryption implementation for inMemoryCache * base64 is only requried when redis-cache feature is enabled * add error case for base64 and encryption/compression implementation to redisCache * Refactor cacher to remove regex dependency * fmt cache error and cacher * Update Cargo.toml disabling the unneeded default-features Co-authored-by: neon_arch <mustafadhuleb53@gmail.com> * fix unused import warning for mimalloc * remove deprecated method * add doc comments for encryption module * fix known bugs and use cfg-if module * make cfg-if an optional dependency * use feature-flag instead of maco lint * add comment to explain type complexity * bump app version * Update src/cache/encryption.rs Co-authored-by: neon_arch <mustafadhuleb53@gmail.com> * fixed type complexity and add docs for types --------- Co-authored-by: Spencer Najib <spencernajib2@gmail.com> Co-authored-by: alamin655 <mdalamin655@outlook.com> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com> Co-authored-by: neon_arch <mustafadhuleb53@gmail.com> Co-authored-by: Spencerjibz <=spencernajib2@gmail.com> Co-authored-by: spencer <spencer@DESKTOP-SIF13AR>
2024-01-11 06:10:35 -05:00
#[cfg(not(feature = "dhat-heap"))]
use mimalloc::MiMalloc;
✨ Compression and encryption for the cached search results (#443) * attempt1 * rough draft * add features and their optional dependancies * add encryption and compression error variants * add a sample implementation to cache trait * Update src/cache/cacher.rs Co-authored-by: neon_arch <mustafadhuleb53@gmail.com> * adjust comment so feature flag would apply? * adjust feature flag so it applies? * formatting * Update src/cache/cacher.rs update documentation Co-authored-by: neon_arch <mustafadhuleb53@gmail.com> * [features]Add base64 and chacha20 dependencies for compress-cache-results and encrypt-cache-results * move encryption key and cipher logic to separate sub module * added cacha20 and cec-results feature * added cacha20 and cec-results feature * added compression and encryption helper functions to trait implementations * added compression and encryption implementation for inMemoryCache * base64 is only requried when redis-cache feature is enabled * add error case for base64 and encryption/compression implementation to redisCache * Refactor cacher to remove regex dependency * fmt cache error and cacher * Update Cargo.toml disabling the unneeded default-features Co-authored-by: neon_arch <mustafadhuleb53@gmail.com> * fix unused import warning for mimalloc * remove deprecated method * add doc comments for encryption module * fix known bugs and use cfg-if module * make cfg-if an optional dependency * use feature-flag instead of maco lint * add comment to explain type complexity * bump app version * Update src/cache/encryption.rs Co-authored-by: neon_arch <mustafadhuleb53@gmail.com> * fixed type complexity and add docs for types --------- Co-authored-by: Spencer Najib <spencernajib2@gmail.com> Co-authored-by: alamin655 <mdalamin655@outlook.com> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com> Co-authored-by: neon_arch <mustafadhuleb53@gmail.com> Co-authored-by: Spencerjibz <=spencernajib2@gmail.com> Co-authored-by: spencer <spencer@DESKTOP-SIF13AR>
2024-01-11 06:10:35 -05:00
:zap: perf: several optimizations for improving the performance of the engine (#540) * :recycle: refactor: initialize & store the config & cache structs as a constant (#486) - initializes & stores the config & cache structs as a static constant. - Pass the config & cache structs as a static reference to all the functions handling their respective route. * :zap: perf: replace hashmaps with vectors for fetching & aggregating results (#486) - replace hashmaps with vectors for fetching, collecting & aggregating results as it tends to be contigous & cache efficient data structure. - refactor & redesign algorithms for fetching & aggregating results centered around vectors in aggregate function. * :heavy_plus_sign: build: add the future crate (#486) * :zap: perf: use `futureunordered` for collecting results fetched from the tokio spawn tasks (#486) - using the `futureunordered` instead of vector for collecting results reduces the time it takes to fetch the results as the results do not need to come in specific order so any result that gets fetched first gets collected in the `futureunordered` type. Co-authored-by: Spencerjibz <spencernajib2@gmail.com> * :zap: perf: initialize new async connections parallely using tokio spawn tasks (#486) * :zap: perf: initialize redis pipeline struct once with the default size of 3 (#486) * :zap: perf: reduce branch predictions by reducing conditional code branches (#486) * :white_check_mark: test(unit): provide unit test for the `get_safesearch_level` function (#486) * :zap: perf: reduce clones & use index based loop to improve search results filtering performance (#486) * 🚨 fix(clippy): make clippy/format checks happy (#486) * 🚨 fix(build): make the cargo build check happy (#486) * :zap: perf: reduce the amount of clones, to_owneds & to_strings (#486) * :zap: perf: use async crates & methods & make functions async (#486) * :bookmark: chore(release): bump the app version (#486) --------- Co-authored-by: Spencerjibz <spencernajib2@gmail.com>
2024-03-11 05:01:30 -04:00
use std::{net::TcpListener, sync::OnceLock};
use websurfx::{cache::cacher::create_cache, config::parser::Config, run};
2023-04-24 06:12:44 -04:00
/// A dhat heap memory profiler
#[cfg(feature = "dhat-heap")]
#[global_allocator]
static ALLOC: dhat::Alloc = dhat::Alloc;
#[cfg(not(feature = "dhat-heap"))]
#[global_allocator]
static GLOBAL: MiMalloc = MiMalloc;
:zap: perf: several optimizations for improving the performance of the engine (#540) * :recycle: refactor: initialize & store the config & cache structs as a constant (#486) - initializes & stores the config & cache structs as a static constant. - Pass the config & cache structs as a static reference to all the functions handling their respective route. * :zap: perf: replace hashmaps with vectors for fetching & aggregating results (#486) - replace hashmaps with vectors for fetching, collecting & aggregating results as it tends to be contigous & cache efficient data structure. - refactor & redesign algorithms for fetching & aggregating results centered around vectors in aggregate function. * :heavy_plus_sign: build: add the future crate (#486) * :zap: perf: use `futureunordered` for collecting results fetched from the tokio spawn tasks (#486) - using the `futureunordered` instead of vector for collecting results reduces the time it takes to fetch the results as the results do not need to come in specific order so any result that gets fetched first gets collected in the `futureunordered` type. Co-authored-by: Spencerjibz <spencernajib2@gmail.com> * :zap: perf: initialize new async connections parallely using tokio spawn tasks (#486) * :zap: perf: initialize redis pipeline struct once with the default size of 3 (#486) * :zap: perf: reduce branch predictions by reducing conditional code branches (#486) * :white_check_mark: test(unit): provide unit test for the `get_safesearch_level` function (#486) * :zap: perf: reduce clones & use index based loop to improve search results filtering performance (#486) * 🚨 fix(clippy): make clippy/format checks happy (#486) * 🚨 fix(build): make the cargo build check happy (#486) * :zap: perf: reduce the amount of clones, to_owneds & to_strings (#486) * :zap: perf: use async crates & methods & make functions async (#486) * :bookmark: chore(release): bump the app version (#486) --------- Co-authored-by: Spencerjibz <spencernajib2@gmail.com>
2024-03-11 05:01:30 -04:00
/// A static constant for holding the parsed config.
static CONFIG: OnceLock<Config> = OnceLock::new();
2023-04-27 10:53:28 -04:00
/// The function that launches the main server and registers all the routes of the website.
///
/// # Error
///
/// Returns an error if the port is being used by something else on the system and is not
/// available for being used for other applications.
2023-04-22 07:35:07 -04:00
#[actix_web::main]
async fn main() -> std::io::Result<()> {
// A dhat heap profiler initialization.
#[cfg(feature = "dhat-heap")]
let _profiler = dhat::Profiler::new_heap();
:zap: perf: several optimizations for improving the performance of the engine (#540) * :recycle: refactor: initialize & store the config & cache structs as a constant (#486) - initializes & stores the config & cache structs as a static constant. - Pass the config & cache structs as a static reference to all the functions handling their respective route. * :zap: perf: replace hashmaps with vectors for fetching & aggregating results (#486) - replace hashmaps with vectors for fetching, collecting & aggregating results as it tends to be contigous & cache efficient data structure. - refactor & redesign algorithms for fetching & aggregating results centered around vectors in aggregate function. * :heavy_plus_sign: build: add the future crate (#486) * :zap: perf: use `futureunordered` for collecting results fetched from the tokio spawn tasks (#486) - using the `futureunordered` instead of vector for collecting results reduces the time it takes to fetch the results as the results do not need to come in specific order so any result that gets fetched first gets collected in the `futureunordered` type. Co-authored-by: Spencerjibz <spencernajib2@gmail.com> * :zap: perf: initialize new async connections parallely using tokio spawn tasks (#486) * :zap: perf: initialize redis pipeline struct once with the default size of 3 (#486) * :zap: perf: reduce branch predictions by reducing conditional code branches (#486) * :white_check_mark: test(unit): provide unit test for the `get_safesearch_level` function (#486) * :zap: perf: reduce clones & use index based loop to improve search results filtering performance (#486) * 🚨 fix(clippy): make clippy/format checks happy (#486) * 🚨 fix(build): make the cargo build check happy (#486) * :zap: perf: reduce the amount of clones, to_owneds & to_strings (#486) * :zap: perf: use async crates & methods & make functions async (#486) * :bookmark: chore(release): bump the app version (#486) --------- Co-authored-by: Spencerjibz <spencernajib2@gmail.com>
2024-03-11 05:01:30 -04:00
// Initialize the parsed config globally.
let config = CONFIG.get_or_init(|| Config::parse(false).unwrap());
:zap: perf: several optimizations for improving the performance of the engine (#540) * :recycle: refactor: initialize & store the config & cache structs as a constant (#486) - initializes & stores the config & cache structs as a static constant. - Pass the config & cache structs as a static reference to all the functions handling their respective route. * :zap: perf: replace hashmaps with vectors for fetching & aggregating results (#486) - replace hashmaps with vectors for fetching, collecting & aggregating results as it tends to be contigous & cache efficient data structure. - refactor & redesign algorithms for fetching & aggregating results centered around vectors in aggregate function. * :heavy_plus_sign: build: add the future crate (#486) * :zap: perf: use `futureunordered` for collecting results fetched from the tokio spawn tasks (#486) - using the `futureunordered` instead of vector for collecting results reduces the time it takes to fetch the results as the results do not need to come in specific order so any result that gets fetched first gets collected in the `futureunordered` type. Co-authored-by: Spencerjibz <spencernajib2@gmail.com> * :zap: perf: initialize new async connections parallely using tokio spawn tasks (#486) * :zap: perf: initialize redis pipeline struct once with the default size of 3 (#486) * :zap: perf: reduce branch predictions by reducing conditional code branches (#486) * :white_check_mark: test(unit): provide unit test for the `get_safesearch_level` function (#486) * :zap: perf: reduce clones & use index based loop to improve search results filtering performance (#486) * 🚨 fix(clippy): make clippy/format checks happy (#486) * 🚨 fix(build): make the cargo build check happy (#486) * :zap: perf: reduce the amount of clones, to_owneds & to_strings (#486) * :zap: perf: use async crates & methods & make functions async (#486) * :bookmark: chore(release): bump the app version (#486) --------- Co-authored-by: Spencerjibz <spencernajib2@gmail.com>
2024-03-11 05:01:30 -04:00
let cache = create_cache(config).await;
2023-04-24 06:12:44 -04:00
log::info!(
"started server on port {} and IP {}",
config.port,
config.binding_ip
);
log::info!(
"Open http://{}:{}/ in your browser",
config.binding_ip,
config.port,
);
2023-04-24 06:12:44 -04:00
:zap: perf: several optimizations for improving the performance of the engine (#540) * :recycle: refactor: initialize & store the config & cache structs as a constant (#486) - initializes & stores the config & cache structs as a static constant. - Pass the config & cache structs as a static reference to all the functions handling their respective route. * :zap: perf: replace hashmaps with vectors for fetching & aggregating results (#486) - replace hashmaps with vectors for fetching, collecting & aggregating results as it tends to be contigous & cache efficient data structure. - refactor & redesign algorithms for fetching & aggregating results centered around vectors in aggregate function. * :heavy_plus_sign: build: add the future crate (#486) * :zap: perf: use `futureunordered` for collecting results fetched from the tokio spawn tasks (#486) - using the `futureunordered` instead of vector for collecting results reduces the time it takes to fetch the results as the results do not need to come in specific order so any result that gets fetched first gets collected in the `futureunordered` type. Co-authored-by: Spencerjibz <spencernajib2@gmail.com> * :zap: perf: initialize new async connections parallely using tokio spawn tasks (#486) * :zap: perf: initialize redis pipeline struct once with the default size of 3 (#486) * :zap: perf: reduce branch predictions by reducing conditional code branches (#486) * :white_check_mark: test(unit): provide unit test for the `get_safesearch_level` function (#486) * :zap: perf: reduce clones & use index based loop to improve search results filtering performance (#486) * 🚨 fix(clippy): make clippy/format checks happy (#486) * 🚨 fix(build): make the cargo build check happy (#486) * :zap: perf: reduce the amount of clones, to_owneds & to_strings (#486) * :zap: perf: use async crates & methods & make functions async (#486) * :bookmark: chore(release): bump the app version (#486) --------- Co-authored-by: Spencerjibz <spencernajib2@gmail.com>
2024-03-11 05:01:30 -04:00
let listener = TcpListener::bind((config.binding_ip.as_str(), config.port))?;
2023-04-22 07:35:07 -04:00
run(listener, config, cache)?.await
2023-04-22 07:35:07 -04:00
}