0
0
mirror of https://github.com/neon-mmd/websurfx.git synced 2024-11-22 14:08:23 -05:00

Compare commits

..

2 Commits

Author SHA1 Message Date
Ilan Joselevich
15aaa1b63f
Merge 4de15eda66 into 280c7e2b5e 2024-02-07 00:26:35 +00:00
Ilan Joselevich
4de15eda66
config: restructure and add json support 2024-02-07 02:26:17 +02:00
3 changed files with 34 additions and 29 deletions

View File

@ -4,7 +4,7 @@
use std::fs::File; use std::fs::File;
use std::io::BufReader; use std::io::BufReader;
use crate::config::{process_settings, set_logging_level, Config}; use crate::config::{process_settings, Config};
use crate::handler::{file_path, FileType}; use crate::handler::{file_path, FileType};
impl Config { impl Config {
@ -20,7 +20,11 @@ impl Config {
Ok(f) => f, Ok(f) => f,
Err(_) => { Err(_) => {
log::error!("Config Error: No config file found, falling back to defaults"); log::error!("Config Error: No config file found, falling back to defaults");
return Ok(Self::default()); let conf = Self::default();
if !logging_initialized {
conf.set_logging_level();
}
return Ok(conf);
} }
}; };
@ -29,7 +33,7 @@ impl Config {
let mut conf: Config = serde_json::from_reader(reader)?; let mut conf: Config = serde_json::from_reader(reader)?;
if !logging_initialized { if !logging_initialized {
set_logging_level(conf.server.debug, conf.server.logging) conf.set_logging_level();
} }
conf = process_settings(conf)?; conf = process_settings(conf)?;

View File

@ -4,7 +4,7 @@ use std::fs;
use mlua::{Lua, LuaSerdeExt}; use mlua::{Lua, LuaSerdeExt};
use crate::config::{process_settings, set_logging_level, Config}; use crate::config::{process_settings, Config};
use crate::handler::{file_path, FileType}; use crate::handler::{file_path, FileType};
impl Config { impl Config {
@ -21,7 +21,11 @@ impl Config {
Ok(f) => f, Ok(f) => f,
Err(_) => { Err(_) => {
log::error!("Config Error: No config file found, falling back to defaults"); log::error!("Config Error: No config file found, falling back to defaults");
return Ok(Self::default()); let conf = Self::default();
if !logging_initialized {
conf.set_logging_level();
}
return Ok(conf);
} }
}; };
let val = lua.load(fs::read_to_string(config_file)?).eval()?; let val = lua.load(fs::read_to_string(config_file)?).eval()?;
@ -29,7 +33,7 @@ impl Config {
let mut conf: Config = lua.from_value(val)?; let mut conf: Config = lua.from_value(val)?;
if !logging_initialized { if !logging_initialized {
set_logging_level(conf.server.debug, conf.server.logging) conf.set_logging_level();
} }
conf = process_settings(conf)?; conf = process_settings(conf)?;

View File

@ -20,31 +20,28 @@ pub struct Config {
pub search: Search, pub search: Search,
} }
/// a helper function that sets the proper logging level impl Config {
/// /// a helper function that sets the proper logging level
/// # Arguments fn set_logging_level(&self) {
/// if let Ok(pkg_env_var) = std::env::var("PKG_ENV") {
/// * `debug` - It takes the option to whether enable or disable debug mode. if pkg_env_var.to_lowercase() == "dev" {
/// * `logging` - It takes the option to whether enable or disable logs. env_logger::Builder::new()
fn set_logging_level(debug: bool, logging: bool) { .filter(None, LevelFilter::Trace)
if let Ok(pkg_env_var) = std::env::var("PKG_ENV") { .init();
if pkg_env_var.to_lowercase() == "dev" { return;
env_logger::Builder::new() }
.filter(None, LevelFilter::Trace)
.init();
return;
} }
// Initializing logging middleware with level set to default or info.
let log_level = match (self.server.debug, self.server.logging) {
(true, true) => LevelFilter::Debug,
(true, false) => LevelFilter::Debug,
(false, true) => LevelFilter::Info,
(false, false) => LevelFilter::Error,
};
env_logger::Builder::new().filter(None, log_level).init();
} }
// Initializing logging middleware with level set to default or info.
let log_level = match (debug, logging) {
(true, true) => LevelFilter::Debug,
(true, false) => LevelFilter::Debug,
(false, true) => LevelFilter::Info,
(false, false) => LevelFilter::Error,
};
env_logger::Builder::new().filter(None, log_level).init();
} }
fn process_settings(mut conf: Config) -> Result<Config, Box<dyn std::error::Error>> { fn process_settings(mut conf: Config) -> Result<Config, Box<dyn std::error::Error>> {