0
0
mirror of https://github.com/neon-mmd/websurfx.git synced 2024-10-18 06:22:53 -04:00

feat: add control check to avoid reinitializing logging & fix tests

This commit is contained in:
neon_arch 2023-08-03 00:12:09 +03:00
parent 26b59078b8
commit 4b4dc28cd2
3 changed files with 20 additions and 12 deletions

View File

@ -15,7 +15,7 @@ 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();
log::info!( log::info!(
"started server on port {} and IP {}", "started server on port {} and IP {}",

View File

@ -54,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();
@ -72,14 +77,17 @@ impl Config {
let debug: bool = globals.get::<_, bool>("debug")?; let debug: bool = globals.get::<_, bool>("debug")?;
let logging:bool= globals.get::<_, bool>("logging")?; let logging:bool= globals.get::<_, bool>("logging")?;
// Initializing logging middleware with level set to default or info. // Check whether logging has not been initialized before.
let mut log_level: LevelFilter = LevelFilter::Off; if logging_initialized {
if logging && debug == false { // Initializing logging middleware with level set to default or info.
log_level = LevelFilter::Info; let mut log_level: LevelFilter = LevelFilter::Off;
} else if debug { if logging && debug == false {
log_level = LevelFilter::Trace; log_level = LevelFilter::Info;
}; } else if debug {
env_logger::Builder::new().filter(None, log_level).init(); log_level = LevelFilter::Trace;
};
env_logger::Builder::new().filter(None, log_level).init();
}
let threads: u8 = if parsed_threads == 0 { let threads: u8 = if parsed_threads == 0 {
let total_num_of_threads:usize = available_parallelism()?.get() /2; let total_num_of_threads:usize = available_parallelism()?.get() /2;

View File

@ -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);
} }