aboutsummaryrefslogtreecommitdiff
path: root/src/config.rs
blob: 9bacc6ad0e770da4e0eba858e49172aa8ca22385 (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
33
34
35
36
37
38
39
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);
        }
    }
}