Spaces:
Running
Running
neon_arch
commited on
Commit
·
d8bd2fe
1
Parent(s):
5b4e7c7
✨ feat: add new config option to manage threads and improve logging
Browse files- src/bin/websurfx.rs +10 -8
- src/config/parser.rs +36 -3
- src/lib.rs +3 -0
- websurfx/config.lua +1 -0
src/bin/websurfx.rs
CHANGED
|
@@ -4,7 +4,6 @@
|
|
| 4 |
//! stdout and handles the command line arguments provided and launches the `websurfx` server.
|
| 5 |
|
| 6 |
use std::net::TcpListener;
|
| 7 |
-
|
| 8 |
use websurfx::{config::parser::Config, run};
|
| 9 |
|
| 10 |
/// The function that launches the main server and registers all the routes of the website.
|
|
@@ -18,13 +17,16 @@ async fn main() -> std::io::Result<()> {
|
|
| 18 |
// Initialize the parsed config file.
|
| 19 |
let config = Config::parse().unwrap();
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
let listener = TcpListener::bind((config.binding_ip.clone(), config.port))?;
|
| 30 |
|
|
|
|
| 4 |
//! stdout and handles the command line arguments provided and launches the `websurfx` server.
|
| 5 |
|
| 6 |
use std::net::TcpListener;
|
|
|
|
| 7 |
use websurfx::{config::parser::Config, run};
|
| 8 |
|
| 9 |
/// The function that launches the main server and registers all the routes of the website.
|
|
|
|
| 17 |
// Initialize the parsed config file.
|
| 18 |
let config = Config::parse().unwrap();
|
| 19 |
|
| 20 |
+
log::info!(
|
| 21 |
+
"started server on port {} and IP {}",
|
| 22 |
+
config.port,
|
| 23 |
+
config.binding_ip
|
| 24 |
+
);
|
| 25 |
+
log::info!(
|
| 26 |
+
"Open http://{}:{}/ in your browser",
|
| 27 |
+
config.port,
|
| 28 |
+
config.binding_ip
|
| 29 |
+
);
|
| 30 |
|
| 31 |
let listener = TcpListener::bind((config.binding_ip.clone(), config.port))?;
|
| 32 |
|
src/config/parser.rs
CHANGED
|
@@ -2,8 +2,9 @@
|
|
| 2 |
//! into rust readable form.
|
| 3 |
|
| 4 |
use super::parser_models::Style;
|
|
|
|
| 5 |
use rlua::Lua;
|
| 6 |
-
use std::{collections::HashMap, format, fs, path::Path};
|
| 7 |
|
| 8 |
// ------- Constants --------
|
| 9 |
static COMMON_DIRECTORY_NAME: &str = "websurfx";
|
|
@@ -23,6 +24,7 @@ static CONFIG_FILE_NAME: &str = "config.lua";
|
|
| 23 |
/// * `debug` - It stores the option to whether enable or disable debug mode.
|
| 24 |
/// * `upstream_search_engines` - It stores all the engine names that were enabled by the user.
|
| 25 |
/// * `request_timeout` - It stores the time (secs) which controls the server request timeout.
|
|
|
|
| 26 |
#[derive(Clone)]
|
| 27 |
pub struct Config {
|
| 28 |
pub port: u16,
|
|
@@ -34,6 +36,7 @@ pub struct Config {
|
|
| 34 |
pub debug: bool,
|
| 35 |
pub upstream_search_engines: Vec<String>,
|
| 36 |
pub request_timeout: u8,
|
|
|
|
| 37 |
}
|
| 38 |
|
| 39 |
/// Configuration options for the aggregator.
|
|
@@ -64,6 +67,35 @@ impl Config {
|
|
| 64 |
.load(&fs::read_to_string(Config::config_path()?)?)
|
| 65 |
.exec()?;
|
| 66 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
Ok(Config {
|
| 68 |
port: globals.get::<_, u16>("port")?,
|
| 69 |
binding_ip: globals.get::<_, String>("binding_ip")?,
|
|
@@ -75,14 +107,15 @@ impl Config {
|
|
| 75 |
aggregator: AggregatorConfig {
|
| 76 |
random_delay: globals.get::<_, bool>("production_use")?,
|
| 77 |
},
|
| 78 |
-
logging
|
| 79 |
-
debug
|
| 80 |
upstream_search_engines: globals
|
| 81 |
.get::<_, HashMap<String, bool>>("upstream_search_engines")?
|
| 82 |
.into_iter()
|
| 83 |
.filter_map(|(key, value)| value.then_some(key))
|
| 84 |
.collect(),
|
| 85 |
request_timeout: globals.get::<_, u8>("request_timeout")?,
|
|
|
|
| 86 |
})
|
| 87 |
})
|
| 88 |
}
|
|
|
|
| 2 |
//! into rust readable form.
|
| 3 |
|
| 4 |
use super::parser_models::Style;
|
| 5 |
+
use log::LevelFilter;
|
| 6 |
use rlua::Lua;
|
| 7 |
+
use std::{collections::HashMap, format, fs, io::Write, path::Path, thread::available_parallelism};
|
| 8 |
|
| 9 |
// ------- Constants --------
|
| 10 |
static COMMON_DIRECTORY_NAME: &str = "websurfx";
|
|
|
|
| 24 |
/// * `debug` - It stores the option to whether enable or disable debug mode.
|
| 25 |
/// * `upstream_search_engines` - It stores all the engine names that were enabled by the user.
|
| 26 |
/// * `request_timeout` - It stores the time (secs) which controls the server request timeout.
|
| 27 |
+
/// * `threads` - It stores the number of threads which controls the app will use to run.
|
| 28 |
#[derive(Clone)]
|
| 29 |
pub struct Config {
|
| 30 |
pub port: u16,
|
|
|
|
| 36 |
pub debug: bool,
|
| 37 |
pub upstream_search_engines: Vec<String>,
|
| 38 |
pub request_timeout: u8,
|
| 39 |
+
pub threads: u8,
|
| 40 |
}
|
| 41 |
|
| 42 |
/// Configuration options for the aggregator.
|
|
|
|
| 67 |
.load(&fs::read_to_string(Config::config_path()?)?)
|
| 68 |
.exec()?;
|
| 69 |
|
| 70 |
+
let parsed_threads: u8 = globals.get::<_, u8>("threads")?;
|
| 71 |
+
|
| 72 |
+
let debug: bool = globals.get::<_, bool>("debug")?;
|
| 73 |
+
let logging:bool= globals.get::<_, bool>("logging")?;
|
| 74 |
+
|
| 75 |
+
// Initializing logging middleware with level set to default or info.
|
| 76 |
+
let mut log_level: LevelFilter = LevelFilter::Off;
|
| 77 |
+
if logging && debug == false {
|
| 78 |
+
log_level = LevelFilter::Info;
|
| 79 |
+
} else if debug {
|
| 80 |
+
log_level = LevelFilter::Trace;
|
| 81 |
+
};
|
| 82 |
+
env_logger::Builder::new().filter(None, log_level).init();
|
| 83 |
+
|
| 84 |
+
let threads: u8 = if parsed_threads == 0 {
|
| 85 |
+
let total_num_of_threads:usize = available_parallelism()?.get() /2;
|
| 86 |
+
if debug || logging {
|
| 87 |
+
log::error!("Config Error: The value of `threads` option should be a non zero positive integer");
|
| 88 |
+
log::info!("Falling back to using {} threads", total_num_of_threads)
|
| 89 |
+
} else {
|
| 90 |
+
std::io::stdout()
|
| 91 |
+
.lock()
|
| 92 |
+
.write_all(&format!("Config Error: The value of `threads` option should be a non zero positive integer\nFalling back to using {} threads\n", total_num_of_threads).into_bytes())?;
|
| 93 |
+
};
|
| 94 |
+
total_num_of_threads as u8
|
| 95 |
+
} else {
|
| 96 |
+
parsed_threads
|
| 97 |
+
};
|
| 98 |
+
|
| 99 |
Ok(Config {
|
| 100 |
port: globals.get::<_, u16>("port")?,
|
| 101 |
binding_ip: globals.get::<_, String>("binding_ip")?,
|
|
|
|
| 107 |
aggregator: AggregatorConfig {
|
| 108 |
random_delay: globals.get::<_, bool>("production_use")?,
|
| 109 |
},
|
| 110 |
+
logging,
|
| 111 |
+
debug,
|
| 112 |
upstream_search_engines: globals
|
| 113 |
.get::<_, HashMap<String, bool>>("upstream_search_engines")?
|
| 114 |
.into_iter()
|
| 115 |
.filter_map(|(key, value)| value.then_some(key))
|
| 116 |
.collect(),
|
| 117 |
request_timeout: globals.get::<_, u8>("request_timeout")?,
|
| 118 |
+
threads,
|
| 119 |
})
|
| 120 |
})
|
| 121 |
}
|
src/lib.rs
CHANGED
|
@@ -49,6 +49,8 @@ pub fn run(listener: TcpListener, config: Config) -> std::io::Result<Server> {
|
|
| 49 |
|
| 50 |
let handlebars_ref: web::Data<Handlebars> = web::Data::new(handlebars);
|
| 51 |
|
|
|
|
|
|
|
| 52 |
let server = HttpServer::new(move || {
|
| 53 |
App::new()
|
| 54 |
.app_data(handlebars_ref.clone())
|
|
@@ -70,6 +72,7 @@ pub fn run(listener: TcpListener, config: Config) -> std::io::Result<Server> {
|
|
| 70 |
.service(routes::settings) // settings page
|
| 71 |
.default_service(web::route().to(routes::not_found)) // error page
|
| 72 |
})
|
|
|
|
| 73 |
// Start server on 127.0.0.1 with the user provided port number. for example 127.0.0.1:8080.
|
| 74 |
.listen(listener)?
|
| 75 |
.run();
|
|
|
|
| 49 |
|
| 50 |
let handlebars_ref: web::Data<Handlebars> = web::Data::new(handlebars);
|
| 51 |
|
| 52 |
+
let cloned_config_threads_opt: u8 = config.threads;
|
| 53 |
+
|
| 54 |
let server = HttpServer::new(move || {
|
| 55 |
App::new()
|
| 56 |
.app_data(handlebars_ref.clone())
|
|
|
|
| 72 |
.service(routes::settings) // settings page
|
| 73 |
.default_service(web::route().to(routes::not_found)) // error page
|
| 74 |
})
|
| 75 |
+
.workers(cloned_config_threads_opt as usize)
|
| 76 |
// Start server on 127.0.0.1 with the user provided port number. for example 127.0.0.1:8080.
|
| 77 |
.listen(listener)?
|
| 78 |
.run();
|
websurfx/config.lua
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
-- ### General ###
|
| 2 |
logging = true -- an option to enable or disable logs.
|
| 3 |
debug = false -- an option to enable or disable debug mode.
|
|
|
|
| 4 |
|
| 5 |
-- ### Server ###
|
| 6 |
port = "8080" -- port on which server should be launched
|
|
|
|
| 1 |
-- ### General ###
|
| 2 |
logging = true -- an option to enable or disable logs.
|
| 3 |
debug = false -- an option to enable or disable debug mode.
|
| 4 |
+
threads = 10 -- the amount of threads that the app will use to run (the value should be greater than 0).
|
| 5 |
|
| 6 |
-- ### Server ###
|
| 7 |
port = "8080" -- port on which server should be launched
|