summaryrefslogblamecommitdiff
path: root/dichroism/src/config.rs
blob: c3dec51fd422e768e07b3aef14825dba065d5832 (plain) (tree)






























                                                                   
use crate::constants::*;
use crate::error::DichroismError;
use crate::result::Result;
use async_std::fs::metadata;
use async_std::path::PathBuf;
use std::env;
use std::net::SocketAddr;

#[derive(Debug, Clone)]
pub struct Config {
    pub db_url: String,
    pub img_root: PathBuf,
    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()?,
        })
    }
}