aboutsummaryrefslogtreecommitdiffstats
path: root/src/config.rs
blob: 96a6849852e055a981996a7424a7ef9e5317a42d (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
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
use std::env;

pub const TEMP_PATH: &str = "/tmp/senator_sperma";

#[derive(Debug, Clone)]
pub struct Config {
    pub token: String,
    pub status_channel_id: Option<u64>,
    pub assistant_keyword: String,
    pub copyparty_url: String,
    pub copyparty_url_upload: String,
    pub copyparty_password: Option<String>,
    pub openrouter_api_key: Option<String>,
    pub openrouter_model: Option<String>,
    pub database_path: String,
}

struct EnvVar {
    name: &'static str,
    required: bool,
    default: Option<&'static str>,
    description: &'static str,
}

const ENV_VARS: &[EnvVar] = &[
    EnvVar {
        name: "TOKEN",
        required: true,
        default: None,
        description: "Discord bot token",
    },
    EnvVar {
        name: "STATUS_CHANNEL_ID",
        required: false,
        default: None,
        description: "status channel ID",
    },
    EnvVar {
        name: "ASSISTANT_KEYWORD",
        required: false,
        default: Some("jarvis"),
        description: "keyword for AI assistant",
    },
    EnvVar {
        name: "COPYPARTY_URL",
        required: false,
        default: Some("http://localhost:3939"),
        description: "Copyparty base URL",
    },
    EnvVar {
        name: "COPYPARTY_URL_UPLOAD",
        required: false,
        default: Some("http://localhost:3939"),
        description: "Copyparty upload URL",
    },
    EnvVar {
        name: "COPYPARTY_PASSWORD",
        required: false,
        default: None,
        description: "Copyparty auth password",
    },
    EnvVar {
        name: "OPENROUTER_API_KEY",
        required: false,
        default: None,
        description: "OpenRouter API key (disables AI if missing)",
    },
    EnvVar {
        name: "OPENROUTER_MODEL",
        required: false,
        default: Some("cognitivecomputations/dolphin-mistral-24b-venice-edition:free"),
        description: "OpenRouter model",
    },
    EnvVar {
        name: "SENATOR_DB_PATH",
        required: false,
        default: Some("data/senator.db"),
        description: "SQLite database path",
    },
    EnvVar {
        name: "RUST_LOG",
        required: false,
        default: Some("info"),
        description: "tracing/logging level filter",
    },
];

pub fn check_env_vars() {
    let mut missing_required = Vec::new();
    let mut missing_optional = Vec::new();

    for var in ENV_VARS {
        match env::var(var.name) {
            Ok(_) => continue,
            Err(_) => {
                if var.required {
                    tracing::error!("MISSING REQUIRED: {} ({})", var.name, var.description);
                    missing_required.push(var.name);
                } else {
                    match var.default {
                        Some(default) => {
                            tracing::warn!("{} not set, using default: {}", var.name, default)
                        }
                        None => tracing::warn!("{} not set ({})", var.name, var.description),
                    }
                    missing_optional.push(var.name);
                }
            }
        }
    }

    if !missing_required.is_empty() {
        tracing::error!(
            "Missing required environment variables: {}. The application will not function correctly.",
            missing_required.join(", ")
        );
    }

    if !missing_optional.is_empty() {
        tracing::warn!(
            "Missing optional environment variables: {}. Some features will be disabled or use defaults.",
            missing_optional.join(", ")
        );
    }
}

impl Config {
    pub fn from_env() -> Self {
        Self {
            token: env::var("TOKEN").expect("Missing TOKEN"),
            status_channel_id: env::var("STATUS_CHANNEL_ID")
                .ok()
                .and_then(|v| v.parse().ok()),
            assistant_keyword: env::var("ASSISTANT_KEYWORD").unwrap_or_else(|_| "jarvis".into()),
            copyparty_url: env::var("COPYPARTY_URL")
                .unwrap_or_else(|_| "http://localhost:3939".into()),
            copyparty_url_upload: env::var("COPYPARTY_URL_UPLOAD")
                .unwrap_or_else(|_| "http://localhost:3939".into()),
            copyparty_password: env::var("COPYPARTY_PASSWORD").ok(),
            openrouter_api_key: env::var("OPENROUTER_API_KEY").ok(),
            openrouter_model: env::var("OPENROUTER_MODEL").ok(),
            database_path: env::var("SENATOR_DB_PATH").unwrap_or_else(|_| "data/senator.db".into()),
        }
    }
}