use std::env;
#[derive(serde::Deserialize, Debug)]
pub struct ServerConfig {
pub protocal: String,
pub server_port: usize,
pub backlog_size: u32,
pub worker_count: usize,
pub keepalive_time: u64,
pub min_stack_size: usize,
pub temp_folder: String,
pub resousrce_folder: String,
pub static_folder: String,
pub templdates_folder: String,
pub enable_simples: bool,
}
impl Default for ServerConfig {
fn default() -> Self {
ServerConfig {
protocal: "http".to_string(),
server_port: 8080,
backlog_size: 8192,
worker_count: 1,
keepalive_time: 75,
min_stack_size: 1024 * 1024 * 1, temp_folder: "".to_string(),
resousrce_folder: "".to_string(),
static_folder: "".to_string(),
templdates_folder: "".to_string(),
enable_simples: false,
}
}
}
pub fn load_env(the_env_folder: &str, env_files: Vec<&str>) {
let current_dir = env::current_dir().unwrap().display().to_string();
for file_name in env_files {
let current_file_name = std::path::Path::new(¤t_dir)
.join(the_env_folder)
.join(file_name)
.display()
.to_string();
match dotenv::from_filename(¤t_file_name) {
Ok(_) => println!(
"Load environment file success: file_path={}",
current_file_name
),
Err(e) => eprintln!(
"Could not load environment file: path={}, error_message='{}'",
current_file_name,
e
),
}
}
}
pub fn load_config<T>() -> T
where
T: serde::de::DeserializeOwned + std::fmt::Debug + Default,
{
match envy::from_env::<T>() {
Ok(config) => config,
Err(e) => {
let d = Default::default();
eprintln!(
"Couldn't read config, used default value: type={}, error_message='{}'",
std::any::type_name::<T>(),
e
);
d
}
}
}