summaryrefslogtreecommitdiff
path: root/dichroism/src/config.rs
diff options
context:
space:
mode:
authorAdam T. Carpenter <atc@53hor.net>2020-10-07 09:37:41 -0400
committerAdam T. Carpenter <atc@53hor.net>2020-10-07 09:37:41 -0400
commit89722ebd6dcdc7067277050a431fbb7b9ea1dcf5 (patch)
treea23a5029b94e7ddec8d8ecdae0438f353cede71a /dichroism/src/config.rs
parent743ae22168b1fcdf2e1e54bcadbb1bb3fce3370d (diff)
downloadtheglassyladies-89722ebd6dcdc7067277050a431fbb7b9ea1dcf5.tar.xz
theglassyladies-89722ebd6dcdc7067277050a431fbb7b9ea1dcf5.zip
sorted out image repo, error handling
Diffstat (limited to 'dichroism/src/config.rs')
-rw-r--r--dichroism/src/config.rs33
1 files changed, 13 insertions, 20 deletions
diff --git a/dichroism/src/config.rs b/dichroism/src/config.rs
index c3dec51..c1c42f0 100644
--- a/dichroism/src/config.rs
+++ b/dichroism/src/config.rs
@@ -1,31 +1,24 @@
-use crate::constants::*;
-use crate::error::DichroismError;
+use crate::constants::DEFAULT_CONFIG;
use crate::result::Result;
-use async_std::fs::metadata;
-use async_std::path::PathBuf;
-use std::env;
+use serde::Deserialize;
+use std::env::var;
+use std::fs::File;
+use std::io::prelude::*;
use std::net::SocketAddr;
+use toml::from_str;
-#[derive(Debug, Clone)]
+#[derive(Debug, Clone, Deserialize)]
pub struct Config {
pub db_url: String,
- pub img_root: PathBuf,
+ pub img_root: String,
pub bind_addr: SocketAddr,
}
impl Config {
- pub async fn new_from_env() -> Result<Self> {
- let img_root = PathBuf::from(env::var(ENV_IMG_ROOT)?);
- let meta = metadata(&img_root).await?;
-
- if !meta.is_dir() || meta.permissions().readonly() {
- return Err(Box::new(DichroismError::InvalidImageRoot));
- }
-
- Ok(Config {
- db_url: env::var(ENV_DB_URL)?,
- img_root,
- bind_addr: env::var(ENV_BIND_ADDR)?.parse()?,
- })
+ pub fn from_toml() -> Result<Self> {
+ let path = var("DICHROISM_CONFIG").unwrap_or_else(|_| String::from(DEFAULT_CONFIG));
+ let mut config = String::new();
+ File::open(path)?.read_to_string(&mut config)?;
+ Ok(from_str(&config)?)
}
}