#![allow(missing_docs)]
#![allow(unused)]
use serde::{Deserialize, Serialize};
use anyhow::Error;
use rand::distributions::Alphanumeric;
use rand::{thread_rng, Rng};
use std::fs;
use toml;
use crate::resource_management::models::caching::{CacheConf, RedisCidrAllowList};
use crate::resource_management::models::storage::{DatabaseConf, PostgresCidrAllowList};
use crate::logger::prelude::*;
use crate::utils::random::*;
use crate::utils::stringify::Stringify;
use crate::LOGGER;
use colored::Colorize;
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct Conf {
pub database: Option<DatabaseConf>,
pub redis: Option<CacheConf>,
}
impl Conf {
fn populate_blank_values(config: &mut Conf) {
if let Some(database) = config.database.as_mut() {
if database.databaseName.as_deref() == Some("") {
database.databaseName = Some(GENERATE_UNIQUE_NAME());
}
if database.databaseUser.as_deref() == Some("") {
database.databaseUser = Some(GENERATE_UNIQUE_NAME());
}
if database.name.as_deref() == Some("") {
database.name = Some(GENERATE_UNIQUE_NAME());
}
if database.cidrBlocks.is_empty() {
database.cidrBlocks.push(PostgresCidrAllowList {
cidrBlock: "0.0.0.0/0".to_string(),
description: "Everywhere".to_string(),
});
}
}
if let Some(redis) = config.redis.as_mut() {
if redis.name.as_deref() == Some("") {
redis.name = Some(GENERATE_UNIQUE_NAME());
}
if redis.cidrBlocks.is_empty() {
redis.cidrBlocks.push(RedisCidrAllowList {
cidrBlock: "0.0.0.0/0".to_string(),
description: "Everywhere".to_string(),
});
}
if redis.plan.is_empty() {
redis.plan = "starter".to_owned();
}
}
}
pub fn read_configuration_file(config_path: &str) -> Result<Self, Error> {
let contents = fs::read_to_string(config_path)
.unwrap_or_else(|_| panic!("Unable to READ configuration: {:?}", config_path));
let mut config: Conf = toml::from_str(&contents)
.unwrap_or_else(|_| panic!("Unable to PARSE configuration: {:?}", config_path));
if config.database.is_none() && config.redis.is_none() {
LOGGER!(
"\nFound empty configuration file -> ",
&config.stringify(),
LogLevel::CRITICAL
);
return Err(anyhow::anyhow!("Found empty configuration file!"));
}
Self::populate_blank_values(&mut config);
LOGGER!(
"\n -> Reading [CONFIG]\n\n",
&config.stringify(),
LogLevel::SUCCESS
);
Ok(Self {
database: config.database,
redis: config.redis,
})
}
}
#[cfg(test)]
mod config_test {
use super::*;
const CONFIG_PATH: &str = "./samples/sample.conf";
#[test]
fn test_read_configuration_file() {
let config = Conf::read_configuration_file(&CONFIG_PATH);
assert!(config.is_ok());
}
#[test]
fn test_generate_random_string() {
let config = Conf::read_configuration_file(&CONFIG_PATH).unwrap();
let result = GENERATE_RANDOM_STRING(10);
assert!(!result.is_empty());
}
#[test]
fn test_generate_unique_name() {
let config = Conf::read_configuration_file(&CONFIG_PATH).unwrap();
let result = GENERATE_UNIQUE_NAME();
assert!(!result.is_empty());
}
#[test]
fn test_conf_to_json_string() {
let config = Conf::read_configuration_file(&CONFIG_PATH).unwrap();
let result = config.stringify();
assert_eq!(std::any::type_name_of_val(&result), "alloc::string::String");
}
}