1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
use std::env;

#[derive(Debug)]
pub struct Config {
    pub pg_conn_str: String,
}

impl Default for Config {
    fn default() -> Self {
        Self {
            pg_conn_str: from_env_default(
                "POSTGRES_CONNECTION",
                "postgresql://postgres:postgres@0.0.0.0:5432/postgres",
            ),
        }
    }
}

/// source a variable from environment - use default if not exists
fn from_env_default(key: &str, default: &str) -> String {
    env::var(key).unwrap_or_else(|_| default.to_owned())
}