|
|
|
|
|
|
|
use super::parser_models::Style; |
|
use rlua::Lua; |
|
use std::{format, fs, path::Path}; |
|
|
|
|
|
static COMMON_DIRECTORY_NAME: &str = "websurfx"; |
|
static CONFIG_FILE_NAME: &str = "config.lua"; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Clone)] |
|
pub struct Config { |
|
pub port: u16, |
|
pub binding_ip_addr: String, |
|
pub style: Style, |
|
pub redis_connection_url: String, |
|
pub aggregator: AggreatorConfig, |
|
pub logging: bool, |
|
} |
|
|
|
|
|
#[derive(Clone)] |
|
pub struct AggreatorConfig { |
|
|
|
pub random_delay: bool, |
|
} |
|
|
|
impl Config { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn parse() -> Result<Self, Box<dyn std::error::Error>> { |
|
Lua::new().context(|context| -> Result<Self, Box<dyn std::error::Error>> { |
|
let globals = context.globals(); |
|
|
|
context |
|
.load(&fs::read_to_string( |
|
Config::handle_different_config_file_path()?, |
|
)?) |
|
.exec()?; |
|
|
|
let production_use = globals.get::<_, bool>("production_use")?; |
|
let aggregator_config = if production_use { |
|
AggreatorConfig { random_delay: true } |
|
} else { |
|
AggreatorConfig { |
|
random_delay: false, |
|
} |
|
}; |
|
|
|
Ok(Config { |
|
port: globals.get::<_, u16>("port")?, |
|
binding_ip_addr: globals.get::<_, String>("binding_ip_addr")?, |
|
style: Style::new( |
|
globals.get::<_, String>("theme")?, |
|
globals.get::<_, String>("colorscheme")?, |
|
), |
|
redis_connection_url: globals.get::<_, String>("redis_connection_url")?, |
|
aggregator: aggregator_config, |
|
logging: globals.get::<_, bool>("logging")?, |
|
}) |
|
}) |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn handle_different_config_file_path() -> Result<String, Box<dyn std::error::Error>> { |
|
if Path::new( |
|
format!( |
|
"{}/.config/{}/config.lua", |
|
std::env::var("HOME").unwrap(), |
|
COMMON_DIRECTORY_NAME |
|
) |
|
.as_str(), |
|
) |
|
.exists() |
|
{ |
|
Ok(format!( |
|
"{}/.config/{}/{}", |
|
std::env::var("HOME").unwrap(), |
|
COMMON_DIRECTORY_NAME, |
|
CONFIG_FILE_NAME |
|
)) |
|
} else if Path::new( |
|
format!("/etc/xdg/{}/{}", COMMON_DIRECTORY_NAME, CONFIG_FILE_NAME).as_str(), |
|
) |
|
.exists() |
|
{ |
|
Ok("/etc/xdg/websurfx/config.lua".to_string()) |
|
} else if Path::new(format!("./{}/{}", COMMON_DIRECTORY_NAME, CONFIG_FILE_NAME).as_str()) |
|
.exists() |
|
{ |
|
Ok("./websurfx/config.lua".to_string()) |
|
} else { |
|
Err(format!("Config file not found!!").into()) |
|
} |
|
} |
|
} |
|
|