0
0
mirror of https://github.com/neon-mmd/websurfx.git synced 2024-11-21 13:38:21 -05: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]
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 {}",

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
/// 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<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>> {
let globals = context.globals();
@ -72,6 +77,8 @@ impl Config {
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 {
@ -80,6 +87,7 @@ impl Config {
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;

View File

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