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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
use crate::blaze::codec::Port;
use log::LevelFilter;
use std::str::FromStr;

pub const REDIRECTOR_PORT: (&str, Port) = ("PR_REDIRECTOR_PORT", 42127);
pub const MAIN_PORT: (&str, Port) = ("PR_MAIN_PORT", 14219);
pub const HTTP_PORT: (&str, Port) = ("PR_HTTP_PORT", 80);

pub const MENU_MESSAGE: (&str, &str) = (
    "PR_MENU_MESSAGE",
    "<font color='#B2B2B2'>Pocket Relay</font> - <font color='#FFFF66'>Logged as: {n}</font>",
);

pub const DATABASE_FILE: (&str, &str) = ("PR_DATABASE_FILE", "data/app.db");
pub const DATABASE_URL: &str = "PR_DATABASE_URL";

pub const GAW_DAILY_DECAY: (&str, f32) = ("PR_GAW_DAILY_DECAY", 0.0);
pub const GAW_PROMOTIONS: (&str, bool) = ("PR_GAW_PROMOTIONS", true);

pub const RETRIEVER: (&str, bool) = ("PR_RETRIEVER", true);

pub const ORIGIN_FETCH: (&str, bool) = ("PR_ORIGIN_FETCH", true);
pub const ORIGIN_FETCH_DATA: (&str, bool) = ("PR_ORIGIN_FETCH_DATA", true);

pub const MITM_ENABLED: (&str, bool) = ("PR_MITM_ENABLED", false);

pub const LOGGING_LEVEL: (&str, LevelFilter) = ("PR_LOG_LEVEL", LevelFilter::Info);
pub const LOGGING_DIR: (&str, &str) = ("PR_LOGGING_DIR", "data/logs");
pub const LOG_COMPRESSION: (&str, bool) = ("PR_LOG_COMPRESSION", true);

pub const API: (&str, bool) = ("PR_API", false);
pub const API_USERNAME: (&str, &str) = ("PR_API_USERNAME", "admin");
pub const API_PASSWORD: (&str, &str) = ("PR_API_PASSWORD", "admin");

#[inline]
pub fn env(pair: (&str, &str)) -> String {
    std::env::var(pair.0).unwrap_or_else(|_| pair.1.to_string())
}

#[inline]
pub fn from_env<F: FromStr>(pair: (&str, F)) -> F {
    if let Ok(value) = std::env::var(pair.0) {
        if let Ok(value) = F::from_str(&value) {
            return value;
        }
    }
    pair.1
}

#[cfg(test)]
mod test {
    use crate::env::from_env;

    #[test]
    fn test_bool() {
        std::env::set_var("TEST", "false");
        assert_eq!(from_env(("TEST", true)), false);

        std::env::set_var("TEST", "False");
        assert_eq!(from_env(("TEST", true)), true);

        std::env::set_var("TEST", "true");
        assert_eq!(from_env(("TEST", false)), true);

        std::env::set_var("TEST", "True");
        assert_eq!(from_env(("TEST", false)), false);

        std::env::set_var("TEST", "12");
        assert_eq!(from_env(("TEST", 0)), 12);
    }
}