mirror of
https://github.com/neon-mmd/websurfx.git
synced 2024-11-22 14:08:23 -05:00
Merge branch 'rolling' into patch-csrf-security-with-cors
This commit is contained in:
commit
9d3a8e065a
@ -4,7 +4,6 @@
|
|||||||
//! stdout and handles the command line arguments provided and launches the `websurfx` server.
|
//! stdout and handles the command line arguments provided and launches the `websurfx` server.
|
||||||
|
|
||||||
use std::net::TcpListener;
|
use std::net::TcpListener;
|
||||||
|
|
||||||
use websurfx::{config::parser::Config, run};
|
use websurfx::{config::parser::Config, run};
|
||||||
|
|
||||||
/// The function that launches the main server and registers all the routes of the website.
|
/// The function that launches the main server and registers all the routes of the website.
|
||||||
@ -16,15 +15,18 @@ use websurfx::{config::parser::Config, run};
|
|||||||
#[actix_web::main]
|
#[actix_web::main]
|
||||||
async fn main() -> std::io::Result<()> {
|
async fn main() -> std::io::Result<()> {
|
||||||
// Initialize the parsed config file.
|
// Initialize the parsed config file.
|
||||||
let config = Config::parse().unwrap();
|
let config = Config::parse(true).unwrap();
|
||||||
|
|
||||||
// Initializing logging middleware with level set to default or info.
|
log::info!(
|
||||||
if config.logging || config.debug {
|
"started server on port {} and IP {}",
|
||||||
use env_logger::Env;
|
config.port,
|
||||||
env_logger::Builder::from_env(Env::default().default_filter_or("info")).init();
|
config.binding_ip
|
||||||
}
|
);
|
||||||
|
log::info!(
|
||||||
log::info!("started server on port {}", config.port);
|
"Open http://{}:{}/ in your browser",
|
||||||
|
config.port,
|
||||||
|
config.binding_ip
|
||||||
|
);
|
||||||
|
|
||||||
let listener = TcpListener::bind((config.binding_ip.clone(), config.port))?;
|
let listener = TcpListener::bind((config.binding_ip.clone(), config.port))?;
|
||||||
|
|
||||||
|
@ -2,8 +2,9 @@
|
|||||||
//! into rust readable form.
|
//! into rust readable form.
|
||||||
|
|
||||||
use super::parser_models::Style;
|
use super::parser_models::Style;
|
||||||
|
use log::LevelFilter;
|
||||||
use rlua::Lua;
|
use rlua::Lua;
|
||||||
use std::{collections::HashMap, format, fs, path::Path};
|
use std::{collections::HashMap, format, fs, io::Write, path::Path, thread::available_parallelism};
|
||||||
|
|
||||||
// ------- Constants --------
|
// ------- Constants --------
|
||||||
static COMMON_DIRECTORY_NAME: &str = "websurfx";
|
static COMMON_DIRECTORY_NAME: &str = "websurfx";
|
||||||
@ -23,6 +24,7 @@ static CONFIG_FILE_NAME: &str = "config.lua";
|
|||||||
/// * `debug` - It stores the option to whether enable or disable debug mode.
|
/// * `debug` - It stores the option to whether enable or disable debug mode.
|
||||||
/// * `upstream_search_engines` - It stores all the engine names that were enabled by the user.
|
/// * `upstream_search_engines` - It stores all the engine names that were enabled by the user.
|
||||||
/// * `request_timeout` - It stores the time (secs) which controls the server request timeout.
|
/// * `request_timeout` - It stores the time (secs) which controls the server request timeout.
|
||||||
|
/// * `threads` - It stores the number of threads which controls the app will use to run.
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct Config {
|
pub struct Config {
|
||||||
pub port: u16,
|
pub port: u16,
|
||||||
@ -34,6 +36,7 @@ pub struct Config {
|
|||||||
pub debug: bool,
|
pub debug: bool,
|
||||||
pub upstream_search_engines: Vec<String>,
|
pub upstream_search_engines: Vec<String>,
|
||||||
pub request_timeout: u8,
|
pub request_timeout: u8,
|
||||||
|
pub threads: u8,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Configuration options for the aggregator.
|
/// Configuration options for the aggregator.
|
||||||
@ -51,12 +54,17 @@ impl Config {
|
|||||||
/// A function which parses the config.lua file and puts all the parsed options in the newly
|
/// A function which parses the config.lua file and puts all the parsed options in the newly
|
||||||
/// constructed Config struct and returns it.
|
/// constructed Config struct and returns it.
|
||||||
///
|
///
|
||||||
|
/// # Arguments
|
||||||
|
///
|
||||||
|
/// * `logging_initialized` - It takes a boolean which ensures that the logging doesn't get
|
||||||
|
/// initialized twice.
|
||||||
|
///
|
||||||
/// # Error
|
/// # Error
|
||||||
///
|
///
|
||||||
/// Returns a lua parse error if parsing of the config.lua file fails or has a syntax 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 constructed
|
/// or io error if the config.lua file doesn't exists otherwise it returns a newly constructed
|
||||||
/// Config struct with all the parsed config options from the parsed config file.
|
/// Config struct with all the parsed config options from the parsed config file.
|
||||||
pub fn parse() -> Result<Self, Box<dyn std::error::Error>> {
|
pub fn parse(logging_initialized: bool) -> Result<Self, Box<dyn std::error::Error>> {
|
||||||
Lua::new().context(|context| -> Result<Self, Box<dyn std::error::Error>> {
|
Lua::new().context(|context| -> Result<Self, Box<dyn std::error::Error>> {
|
||||||
let globals = context.globals();
|
let globals = context.globals();
|
||||||
|
|
||||||
@ -64,6 +72,38 @@ impl Config {
|
|||||||
.load(&fs::read_to_string(Config::config_path()?)?)
|
.load(&fs::read_to_string(Config::config_path()?)?)
|
||||||
.exec()?;
|
.exec()?;
|
||||||
|
|
||||||
|
let parsed_threads: u8 = globals.get::<_, u8>("threads")?;
|
||||||
|
|
||||||
|
let debug: bool = globals.get::<_, bool>("debug")?;
|
||||||
|
let logging:bool= globals.get::<_, bool>("logging")?;
|
||||||
|
|
||||||
|
// Check whether logging has not been initialized before.
|
||||||
|
if logging_initialized {
|
||||||
|
// Initializing logging middleware with level set to default or info.
|
||||||
|
let mut log_level: LevelFilter = LevelFilter::Off;
|
||||||
|
if logging && debug == false {
|
||||||
|
log_level = LevelFilter::Info;
|
||||||
|
} else if debug {
|
||||||
|
log_level = LevelFilter::Trace;
|
||||||
|
};
|
||||||
|
env_logger::Builder::new().filter(None, log_level).init();
|
||||||
|
}
|
||||||
|
|
||||||
|
let threads: u8 = if parsed_threads == 0 {
|
||||||
|
let total_num_of_threads:usize = available_parallelism()?.get() /2;
|
||||||
|
if debug || logging {
|
||||||
|
log::error!("Config Error: The value of `threads` option should be a non zero positive integer");
|
||||||
|
log::info!("Falling back to using {} threads", total_num_of_threads)
|
||||||
|
} else {
|
||||||
|
std::io::stdout()
|
||||||
|
.lock()
|
||||||
|
.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())?;
|
||||||
|
};
|
||||||
|
total_num_of_threads as u8
|
||||||
|
} else {
|
||||||
|
parsed_threads
|
||||||
|
};
|
||||||
|
|
||||||
Ok(Config {
|
Ok(Config {
|
||||||
port: globals.get::<_, u16>("port")?,
|
port: globals.get::<_, u16>("port")?,
|
||||||
binding_ip: globals.get::<_, String>("binding_ip")?,
|
binding_ip: globals.get::<_, String>("binding_ip")?,
|
||||||
@ -75,14 +115,15 @@ impl Config {
|
|||||||
aggregator: AggregatorConfig {
|
aggregator: AggregatorConfig {
|
||||||
random_delay: globals.get::<_, bool>("production_use")?,
|
random_delay: globals.get::<_, bool>("production_use")?,
|
||||||
},
|
},
|
||||||
logging: globals.get::<_, bool>("logging")?,
|
logging,
|
||||||
debug: globals.get::<_, bool>("debug")?,
|
debug,
|
||||||
upstream_search_engines: globals
|
upstream_search_engines: globals
|
||||||
.get::<_, HashMap<String, bool>>("upstream_search_engines")?
|
.get::<_, HashMap<String, bool>>("upstream_search_engines")?
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.filter_map(|(key, value)| value.then_some(key))
|
.filter_map(|(key, value)| value.then_some(key))
|
||||||
.collect(),
|
.collect(),
|
||||||
request_timeout: globals.get::<_, u8>("request_timeout")?,
|
request_timeout: globals.get::<_, u8>("request_timeout")?,
|
||||||
|
threads,
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -35,7 +35,7 @@ use handler::public_paths::public_path;
|
|||||||
/// use std::net::TcpListener;
|
/// use std::net::TcpListener;
|
||||||
/// use websurfx::{config::parser::Config, run};
|
/// use websurfx::{config::parser::Config, run};
|
||||||
///
|
///
|
||||||
/// let config = Config::parse().unwrap();
|
/// let config = Config::parse(true).unwrap();
|
||||||
/// let listener = TcpListener::bind("127.0.0.1:8080").expect("Failed to bind address");
|
/// let listener = TcpListener::bind("127.0.0.1:8080").expect("Failed to bind address");
|
||||||
/// let server = run(listener,config).expect("Failed to start server");
|
/// let server = run(listener,config).expect("Failed to start server");
|
||||||
/// ```
|
/// ```
|
||||||
@ -50,6 +50,8 @@ pub fn run(listener: TcpListener, config: Config) -> std::io::Result<Server> {
|
|||||||
|
|
||||||
let handlebars_ref: web::Data<Handlebars> = web::Data::new(handlebars);
|
let handlebars_ref: web::Data<Handlebars> = web::Data::new(handlebars);
|
||||||
|
|
||||||
|
let cloned_config_threads_opt: u8 = config.threads;
|
||||||
|
|
||||||
let server = HttpServer::new(move || {
|
let server = HttpServer::new(move || {
|
||||||
let cors: Cors = Cors::default()
|
let cors: Cors = Cors::default()
|
||||||
.allow_any_origin()
|
.allow_any_origin()
|
||||||
@ -82,6 +84,7 @@ pub fn run(listener: TcpListener, config: Config) -> std::io::Result<Server> {
|
|||||||
.service(routes::settings) // settings page
|
.service(routes::settings) // settings page
|
||||||
.default_service(web::route().to(routes::not_found)) // error page
|
.default_service(web::route().to(routes::not_found)) // error page
|
||||||
})
|
})
|
||||||
|
.workers(cloned_config_threads_opt as usize)
|
||||||
// Start server on 127.0.0.1 with the user provided port number. for example 127.0.0.1:8080.
|
// Start server on 127.0.0.1 with the user provided port number. for example 127.0.0.1:8080.
|
||||||
.listen(listener)?
|
.listen(listener)?
|
||||||
.run();
|
.run();
|
||||||
|
@ -8,7 +8,7 @@ fn spawn_app() -> String {
|
|||||||
// Binding to port 0 will trigger the OS to assign a port for us.
|
// Binding to port 0 will trigger the OS to assign a port for us.
|
||||||
let listener = TcpListener::bind("127.0.0.1:0").expect("Failed to bind random port");
|
let listener = TcpListener::bind("127.0.0.1:0").expect("Failed to bind random port");
|
||||||
let port = listener.local_addr().unwrap().port();
|
let port = listener.local_addr().unwrap().port();
|
||||||
let config = Config::parse().unwrap();
|
let config = Config::parse(true).unwrap();
|
||||||
let server = run(listener, config).expect("Failed to bind address");
|
let server = run(listener, config).expect("Failed to bind address");
|
||||||
|
|
||||||
tokio::spawn(server);
|
tokio::spawn(server);
|
||||||
@ -36,7 +36,7 @@ async fn test_index() {
|
|||||||
assert_eq!(res.status(), 200);
|
assert_eq!(res.status(), 200);
|
||||||
|
|
||||||
let handlebars = handlebars();
|
let handlebars = handlebars();
|
||||||
let config = Config::parse().unwrap();
|
let config = Config::parse(false).unwrap();
|
||||||
let template = handlebars.render("index", &config.style).unwrap();
|
let template = handlebars.render("index", &config.style).unwrap();
|
||||||
assert_eq!(res.text().await.unwrap(), template);
|
assert_eq!(res.text().await.unwrap(), template);
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
-- ### General ###
|
-- ### General ###
|
||||||
logging = true -- an option to enable or disable logs.
|
logging = true -- an option to enable or disable logs.
|
||||||
debug = false -- an option to enable or disable debug mode.
|
debug = false -- an option to enable or disable debug mode.
|
||||||
|
threads = 10 -- the amount of threads that the app will use to run (the value should be greater than 0).
|
||||||
|
|
||||||
-- ### Server ###
|
-- ### Server ###
|
||||||
port = "8080" -- port on which server should be launched
|
port = "8080" -- port on which server should be launched
|
||||||
|
Loading…
Reference in New Issue
Block a user