summaryrefslogtreecommitdiff
path: root/dichroism/src/config.rs
blob: c3dec51fd422e768e07b3aef14825dba065d5832 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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()?,
        })
    }
}