summaryrefslogtreecommitdiff
path: root/src/config.rs
blob: 04bd94a0ea992b5556875d05f4044b42db641a1f (plain)
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
use std::env;
use std::path::PathBuf;

#[derive(Debug, Clone)]
pub struct Config {
    pub database_url: String,
    #[allow(dead_code)]
    pub openweathermap_api_key: Option<String>,
    pub admin_token: Option<String>,
    pub server_port: u16,
    pub server_host: String,
    pub log_level: String,
}

impl Config {
    pub fn from_env() -> Self {
        Self::from_env_vars(&env::vars().collect::<Vec<_>>())
    }

    pub fn from_env_vars(env_vars: &[(String, String)]) -> Self {
        let get_env = |key: &str| {
            env_vars
                .iter()
                .find(|(k, _)| k == key)
                .map(|(_, v)| v.clone())
        };

        let database_url = Self::get_database_url_from_env_vars(env_vars);
        let openweathermap_api_key = get_env("OPENWEATHERMAP_API_KEY");
        let admin_token = get_env("ADMIN_TOKEN");
        let server_port = get_env("PORT")
            .unwrap_or_else(|| "4000".to_string())
            .parse()
            .unwrap_or(4000);
        let server_host = get_env("HOST").unwrap_or_else(|| "0.0.0.0".to_string());
        let log_level = get_env("LOG_LEVEL").unwrap_or_else(|| "info".to_string());

        Self {
            database_url,
            openweathermap_api_key,
            admin_token,
            server_port,
            server_host,
            log_level,
        }
    }

    fn get_database_url_from_env_vars(env_vars: &[(String, String)]) -> String {
        let get_env = |key: &str| {
            env_vars
                .iter()
                .find(|(k, _)| k == key)
                .map(|(_, v)| v.clone())
        };

        // First, check if DATABASE_URL is explicitly set
        if let Some(url) = get_env("DATABASE_URL") {
            return url;
        }

        // If not set, construct the path using XDG_DATA_HOME or fallback
        let data_dir = if let Some(xdg_data_home) = get_env("XDG_DATA_HOME") {
            PathBuf::from(xdg_data_home)
        } else {
            // Fallback to ~/.local/share
            let home = get_env("HOME").unwrap_or_else(|| "/tmp".to_string());
            PathBuf::from(home).join(".local").join("share")
        };

        let db_path = data_dir.join("silmataivas").join("silmataivas.db");

        // Ensure the directory exists
        if let Some(parent) = db_path.parent() {
            let _ = std::fs::create_dir_all(parent);
        }

        // Use the correct SQLx format: sqlite:path
        format!("sqlite:{}", db_path.display())
    }

    #[allow(dead_code)]
    pub fn validate(&self) -> Result<(), String> {
        if self.openweathermap_api_key.is_none() {
            return Err("OPENWEATHERMAP_API_KEY environment variable is required".to_string());
        }
        Ok(())
    }

    #[allow(dead_code)]
    pub fn validate_optional(&self) -> Result<(), String> {
        // For optional validation (e.g., during development)
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_database_url_fallback() {
        let env_vars = vec![("HOME".to_string(), "/home/testuser".to_string())];

        let config = Config::from_env_vars(&env_vars);
        let expected_path = "sqlite:/home/testuser/.local/share/silmataivas/silmataivas.db";
        assert_eq!(config.database_url, expected_path);
    }

    #[test]
    fn test_database_url_xdg_data_home() {
        let env_vars = vec![("XDG_DATA_HOME".to_string(), "/custom/data/path".to_string())];

        let config = Config::from_env_vars(&env_vars);
        let expected_path = "sqlite:/custom/data/path/silmataivas/silmataivas.db";
        assert_eq!(config.database_url, expected_path);
    }

    #[test]
    fn test_database_url_explicit() {
        let env_vars = vec![(
            "DATABASE_URL".to_string(),
            "sqlite:/explicit/path.db".to_string(),
        )];

        let config = Config::from_env_vars(&env_vars);
        assert_eq!(config.database_url, "sqlite:/explicit/path.db");
    }

    #[test]
    fn test_server_config() {
        let env_vars = vec![
            ("PORT".to_string(), "8080".to_string()),
            ("HOST".to_string(), "127.0.0.1".to_string()),
            ("LOG_LEVEL".to_string(), "debug".to_string()),
        ];

        let config = Config::from_env_vars(&env_vars);
        assert_eq!(config.server_port, 8080);
        assert_eq!(config.server_host, "127.0.0.1");
        assert_eq!(config.log_level, "debug");
    }

    #[test]
    fn test_default_values() {
        let env_vars = vec![];

        let config = Config::from_env_vars(&env_vars);
        assert_eq!(config.server_port, 4000);
        assert_eq!(config.server_host, "0.0.0.0");
        assert_eq!(config.log_level, "info");
    }

    #[test]
    fn test_home_fallback_to_tmp() {
        let env_vars = vec![];

        let config = Config::from_env_vars(&env_vars);
        let expected_path = "sqlite:/tmp/.local/share/silmataivas/silmataivas.db";
        assert_eq!(config.database_url, expected_path);
    }
}