2026-03-10 14:21:23 +03:00
|
|
|
use serde::Deserialize;
|
|
|
|
|
use std::path::Path;
|
|
|
|
|
|
|
|
|
|
const DEFAULT_CONFIG_PATH: &str = "config.toml";
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Deserialize)]
|
|
|
|
|
pub struct GeneralServiceConfig {
|
|
|
|
|
pub postgres_host: String,
|
|
|
|
|
pub pg_database: String,
|
|
|
|
|
pub postgres_user: String,
|
2026-03-22 19:33:23 +03:00
|
|
|
pub file_storage: String,
|
2026-03-10 14:21:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub type ConfigResult<T> = Result<T, Box<dyn std::error::Error>>;
|
|
|
|
|
|
|
|
|
|
pub fn load_config(path: impl AsRef<Path>) -> ConfigResult<GeneralServiceConfig> {
|
|
|
|
|
let raw = std::fs::read_to_string(path.as_ref())?;
|
|
|
|
|
let config: GeneralServiceConfig = toml::from_str(&raw)?;
|
|
|
|
|
|
|
|
|
|
if config.pg_database.trim().is_empty() {
|
|
|
|
|
return Err(std::io::Error::new(
|
|
|
|
|
std::io::ErrorKind::InvalidData,
|
|
|
|
|
"config database is empty",
|
|
|
|
|
)
|
|
|
|
|
.into());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if config.postgres_user.trim().is_empty() {
|
|
|
|
|
return Err(std::io::Error::new(
|
|
|
|
|
std::io::ErrorKind::InvalidData,
|
|
|
|
|
"config user is empty",
|
|
|
|
|
)
|
|
|
|
|
.into());
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-22 19:33:23 +03:00
|
|
|
if config.file_storage.trim().is_empty() {
|
|
|
|
|
return Err(std::io::Error::new(
|
|
|
|
|
std::io::ErrorKind::InvalidData,
|
|
|
|
|
"config file_storage is empty",
|
|
|
|
|
)
|
|
|
|
|
.into());
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-10 14:21:23 +03:00
|
|
|
Ok(config)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn load_config_default() -> ConfigResult<GeneralServiceConfig> {
|
|
|
|
|
load_config(DEFAULT_CONFIG_PATH)
|
|
|
|
|
}
|