summaryrefslogtreecommitdiff
path: root/dichroism/src/config.rs
blob: 0dbc10e8b080369a8a779a4cbfd8ea769a914cfd (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
32
use crate::constants::DEFAULT_CONFIG;
use crate::result::Result;
use once_cell::sync::Lazy;
use serde::Deserialize;
use std::env::var;
use std::fs::File;
use std::io::prelude::*;
use std::net::SocketAddr;
use toml::from_str;

pub static CONFIG_INSTANCE: Lazy<Config> = Lazy::new(|| {
    Config::from_toml().unwrap_or_else(|e| {
        eprintln!("Error: {}", e.to_string());
        std::process::exit(1);
    })
});

#[derive(Debug, Clone, Deserialize)]
pub struct Config {
    pub db_url: String,
    pub img_root: String,
    pub bind_addr: SocketAddr,
}

impl Config {
    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)?)
    }
}