diff --git a/src/bin/websurfx.rs b/src/bin/websurfx.rs index 57682d9..b4b989e 100644 --- a/src/bin/websurfx.rs +++ b/src/bin/websurfx.rs @@ -15,7 +15,7 @@ use websurfx::{config::parser::Config, run}; #[actix_web::main] async fn main() -> std::io::Result<()> { // Initialize the parsed config file. - let config = Config::parse().unwrap(); + let config = Config::parse(true).unwrap(); log::info!( "started server on port {} and IP {}", diff --git a/src/config/parser.rs b/src/config/parser.rs index 079084e..51a83ea 100644 --- a/src/config/parser.rs +++ b/src/config/parser.rs @@ -54,12 +54,17 @@ impl Config { /// A function which parses the config.lua file and puts all the parsed options in the newly /// constructed Config struct and returns it. /// + /// # Arguments + /// + /// * `logging_initialized` - It takes a boolean which ensures that the logging doesn't get + /// initialized twice. + /// /// # 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 /// Config struct with all the parsed config options from the parsed config file. - pub fn parse() -> Result> { + pub fn parse(logging_initialized: bool) -> Result> { Lua::new().context(|context| -> Result> { let globals = context.globals(); @@ -72,14 +77,17 @@ impl Config { let debug: bool = globals.get::<_, bool>("debug")?; let logging:bool= globals.get::<_, bool>("logging")?; - // 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(); + // 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; diff --git a/tests/index.rs b/tests/index.rs index 657a466..d886e13 100644 --- a/tests/index.rs +++ b/tests/index.rs @@ -8,7 +8,7 @@ fn spawn_app() -> String { // 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 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"); tokio::spawn(server); @@ -36,7 +36,7 @@ async fn test_index() { assert_eq!(res.status(), 200); let handlebars = handlebars(); - let config = Config::parse().unwrap(); + let config = Config::parse(false).unwrap(); let template = handlebars.render("index", &config.style).unwrap(); assert_eq!(res.text().await.unwrap(), template); }