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

⚙️ refactor: replace rlua with mlua code implementation (#180)(#178)

This commit is contained in:
neon_arch 2023-08-27 20:55:34 +03:00
parent 5f1a43976f
commit e4476aae28

View File

@ -5,7 +5,7 @@ use crate::handler::paths::{file_path, FileType};
use super::parser_models::Style; use super::parser_models::Style;
use log::LevelFilter; use log::LevelFilter;
use rlua::Lua; use mlua::Lua;
use std::{collections::HashMap, fs, thread::available_parallelism}; use std::{collections::HashMap, fs, thread::available_parallelism};
/// A named struct which stores the parsed config file options. /// A named struct which stores the parsed config file options.
@ -63,53 +63,53 @@ impl Config {
/// 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(logging_initialized: bool) -> 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 lua = Lua::new();
let globals = context.globals(); let globals = lua.globals();
context lua.load(&fs::read_to_string(file_path(FileType::Config)?)?)
.load(&fs::read_to_string(file_path(FileType::Config)?)?) .exec()?;
.exec()?;
let parsed_threads: u8 = globals.get::<_, u8>("threads")?; let parsed_threads: u8 = globals.get::<_, u8>("threads")?;
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")?;
if !logging_initialized { if !logging_initialized {
set_logging_level(debug, logging); set_logging_level(debug, logging);
} }
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;
log::error!("Config Error: The value of `threads` option should be a non zero positive integer"); log::error!(
log::error!("Falling back to using {} threads", total_num_of_threads); "Config Error: The value of `threads` option should be a non zero positive integer"
total_num_of_threads as u8 );
} else { log::error!("Falling back to using {} threads", total_num_of_threads);
parsed_threads 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")?,
style: Style::new( style: Style::new(
globals.get::<_, String>("theme")?, globals.get::<_, String>("theme")?,
globals.get::<_, String>("colorscheme")?, globals.get::<_, String>("colorscheme")?,
), ),
redis_url: globals.get::<_, String>("redis_url")?, redis_url: globals.get::<_, String>("redis_url")?,
aggregator: AggregatorConfig { aggregator: AggregatorConfig {
random_delay: globals.get::<_, bool>("production_use")?, random_delay: globals.get::<_, bool>("production_use")?,
}, },
logging, logging,
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))
.filter_map(|engine| crate::engines::engine_models::EngineHandler::new(&engine)) .filter_map(|engine| crate::engines::engine_models::EngineHandler::new(&engine))
.collect(), .collect(),
request_timeout: globals.get::<_, u8>("request_timeout")?, request_timeout: globals.get::<_, u8>("request_timeout")?,
threads, threads,
})
}) })
} }
} }