mirror of
https://github.com/neon-mmd/websurfx.git
synced 2024-11-23 22:48:21 -05:00
refactor config helper function
This commit is contained in:
parent
1f90b4e15c
commit
e2907edf6e
@ -24,46 +24,35 @@ pub struct Config {
|
|||||||
pub binding_ip_addr: String,
|
pub binding_ip_addr: String,
|
||||||
pub style: Style,
|
pub style: Style,
|
||||||
pub redis_connection_url: String,
|
pub redis_connection_url: String,
|
||||||
pub aggregator: AggreatorConfig,
|
pub aggregator: AggregatorConfig,
|
||||||
pub logging: bool,
|
pub logging: bool,
|
||||||
pub debug: bool,
|
pub debug: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Configuration options for the aggregator.
|
/// Configuration options for the aggregator.
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct AggreatorConfig {
|
pub struct AggregatorConfig {
|
||||||
/// Whether to introduce a random delay before sending the request to the search engine.
|
/// Whether to introduce a random delay before sending the request to the search engine.
|
||||||
pub random_delay: bool,
|
pub random_delay: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Config {
|
impl Config {
|
||||||
/// A function which parses the config.lua file and puts all the parsed options in the newly
|
/// A function which parses the config.lua file and puts all the parsed options in the newly
|
||||||
/// contructed Config struct and returns it.
|
/// constructed Config struct and returns it.
|
||||||
///
|
///
|
||||||
/// # Error
|
/// # Error
|
||||||
///
|
///
|
||||||
/// Returns a lua parse error if parsing of the config.lua file fails or has a syntax 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 contructed
|
/// 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() -> Result<Self, Box<dyn std::error::Error>> {
|
pub fn parse() -> Result<Self, Box<dyn std::error::Error>> {
|
||||||
Lua::new().context(|context| -> Result<Self, Box<dyn std::error::Error>> {
|
Lua::new().context(|context| -> Result<Self, Box<dyn std::error::Error>> {
|
||||||
let globals = context.globals();
|
let globals = context.globals();
|
||||||
|
|
||||||
context
|
context
|
||||||
.load(&fs::read_to_string(
|
.load(&fs::read_to_string(Config::get_config_path()?)?)
|
||||||
Config::handle_different_config_file_path()?,
|
|
||||||
)?)
|
|
||||||
.exec()?;
|
.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 {
|
Ok(Config {
|
||||||
port: globals.get::<_, u16>("port")?,
|
port: globals.get::<_, u16>("port")?,
|
||||||
binding_ip_addr: globals.get::<_, String>("binding_ip_addr")?,
|
binding_ip_addr: globals.get::<_, String>("binding_ip_addr")?,
|
||||||
@ -72,7 +61,9 @@ impl Config {
|
|||||||
globals.get::<_, String>("colorscheme")?,
|
globals.get::<_, String>("colorscheme")?,
|
||||||
),
|
),
|
||||||
redis_connection_url: globals.get::<_, String>("redis_connection_url")?,
|
redis_connection_url: globals.get::<_, String>("redis_connection_url")?,
|
||||||
aggregator: aggregator_config,
|
aggregator: AggregatorConfig {
|
||||||
|
random_delay: globals.get::<_, bool>("production_use")?,
|
||||||
|
},
|
||||||
logging: globals.get::<_, bool>("logging")?,
|
logging: globals.get::<_, bool>("logging")?,
|
||||||
debug: globals.get::<_, bool>("debug")?,
|
debug: globals.get::<_, bool>("debug")?,
|
||||||
})
|
})
|
||||||
@ -90,35 +81,37 @@ impl Config {
|
|||||||
/// one (3).
|
/// one (3).
|
||||||
/// 3. `websurfx/` (under project folder ( or codebase in other words)) if it is not present
|
/// 3. `websurfx/` (under project folder ( or codebase in other words)) if it is not present
|
||||||
/// here then it returns an error as mentioned above.
|
/// here then it returns an error as mentioned above.
|
||||||
fn handle_different_config_file_path() -> Result<String, Box<dyn std::error::Error>> {
|
fn get_config_path() -> Result<String, Box<dyn std::error::Error>> {
|
||||||
if Path::new(
|
// check user config
|
||||||
format!(
|
|
||||||
|
let path = format!(
|
||||||
"{}/.config/{}/config.lua",
|
"{}/.config/{}/config.lua",
|
||||||
std::env::var("HOME").unwrap(),
|
std::env::var("HOME").unwrap(),
|
||||||
COMMON_DIRECTORY_NAME
|
COMMON_DIRECTORY_NAME
|
||||||
)
|
);
|
||||||
.as_str(),
|
if Path::new(path.as_str()).exists() {
|
||||||
)
|
return Ok(format!(
|
||||||
.exists()
|
|
||||||
{
|
|
||||||
Ok(format!(
|
|
||||||
"{}/.config/{}/{}",
|
"{}/.config/{}/{}",
|
||||||
std::env::var("HOME").unwrap(),
|
std::env::var("HOME").unwrap(),
|
||||||
COMMON_DIRECTORY_NAME,
|
COMMON_DIRECTORY_NAME,
|
||||||
CONFIG_FILE_NAME
|
CONFIG_FILE_NAME
|
||||||
))
|
));
|
||||||
} else if Path::new(
|
}
|
||||||
format!("/etc/xdg/{}/{}", COMMON_DIRECTORY_NAME, CONFIG_FILE_NAME).as_str(),
|
|
||||||
)
|
// look for config in /etc/xdg
|
||||||
|
if Path::new(format!("/etc/xdg/{}/{}", COMMON_DIRECTORY_NAME, CONFIG_FILE_NAME).as_str())
|
||||||
.exists()
|
.exists()
|
||||||
{
|
{
|
||||||
Ok("/etc/xdg/websurfx/config.lua".to_string())
|
return Ok("/etc/xdg/websurfx/config.lua".to_string());
|
||||||
} else if Path::new(format!("./{}/{}", COMMON_DIRECTORY_NAME, CONFIG_FILE_NAME).as_str())
|
}
|
||||||
.exists()
|
|
||||||
|
// use dev config
|
||||||
|
if Path::new(format!("./{}/{}", COMMON_DIRECTORY_NAME, CONFIG_FILE_NAME).as_str()).exists()
|
||||||
{
|
{
|
||||||
Ok("./websurfx/config.lua".to_string())
|
return Ok("./websurfx/config.lua".to_string());
|
||||||
} else {
|
}
|
||||||
|
|
||||||
|
// if no of the configs above exist, return error
|
||||||
Err("Config file not found!!".to_string().into())
|
Err("Config file not found!!".to_string().into())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
//! This module provides public models for handling, storing and serializing parsed config file
|
//! This module provides public models for handling, storing and serializing parsed config file
|
||||||
//! options from config.lua by grouping them togather.
|
//! options from config.lua by grouping them together.
|
||||||
|
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user