diff options
author | Cara Salter <cara@devcara.com> | 2022-07-19 12:49:28 -0400 |
---|---|---|
committer | Cara Salter <cara@devcara.com> | 2022-07-19 12:49:28 -0400 |
commit | aadc1975d459f0e2e2807dbb3e2a36098476128b (patch) | |
tree | 231efcabf21f95e46ae5ffdedadf55bf3ccea66b /src/config.rs | |
download | nccd-aadc1975d459f0e2e2807dbb3e2a36098476128b.tar.gz nccd-aadc1975d459f0e2e2807dbb3e2a36098476128b.zip |
Initial commit
Diffstat (limited to 'src/config.rs')
-rw-r--r-- | src/config.rs | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/src/config.rs b/src/config.rs new file mode 100644 index 0000000..9bacc6a --- /dev/null +++ b/src/config.rs @@ -0,0 +1,40 @@ +use serde::Deserialize; +use tracing::error; + +use crate::errors::ServiceError; +use std::fs; +use std::str; + +#[derive(Deserialize)] +pub struct Config { + pub server: ServerConfig, + pub database: DbConfig, +} + +#[derive(Deserialize)] +pub struct ServerConfig { + pub bind_addr: String, +} + +#[derive(Deserialize, Clone)] +pub struct DbConfig { + pub postgres_url: String, + pub max_connections: u32, +} + +impl Config { + pub fn init(path: String) -> Result<Config, ServiceError> { + if let Ok(c) = fs::read(path) { + if c.len() == 0 { + error!("Config file empty."); + return Err(ServiceError::MissingConfig); + } else { + let config: Config = toml::from_str(str::from_utf8(&c).unwrap())?; + return Ok(config); + } + } else { + error!("Unable to read from config file."); + return Err(ServiceError::MissingConfig); + } + } +} |