diff options
Diffstat (limited to 'src/config.rs')
| -rw-r--r-- | src/config.rs | 145 |
1 files changed, 145 insertions, 0 deletions
diff --git a/src/config.rs b/src/config.rs new file mode 100644 index 0000000..96a6849 --- /dev/null +++ b/src/config.rs | |||
| @@ -0,0 +1,145 @@ | |||
| 1 | use std::env; | ||
| 2 | |||
| 3 | pub const TEMP_PATH: &str = "/tmp/senator_sperma"; | ||
| 4 | |||
| 5 | #[derive(Debug, Clone)] | ||
| 6 | pub struct Config { | ||
| 7 | pub token: String, | ||
| 8 | pub status_channel_id: Option<u64>, | ||
| 9 | pub assistant_keyword: String, | ||
| 10 | pub copyparty_url: String, | ||
| 11 | pub copyparty_url_upload: String, | ||
| 12 | pub copyparty_password: Option<String>, | ||
| 13 | pub openrouter_api_key: Option<String>, | ||
| 14 | pub openrouter_model: Option<String>, | ||
| 15 | pub database_path: String, | ||
| 16 | } | ||
| 17 | |||
| 18 | struct EnvVar { | ||
| 19 | name: &'static str, | ||
| 20 | required: bool, | ||
| 21 | default: Option<&'static str>, | ||
| 22 | description: &'static str, | ||
| 23 | } | ||
| 24 | |||
| 25 | const ENV_VARS: &[EnvVar] = &[ | ||
| 26 | EnvVar { | ||
| 27 | name: "TOKEN", | ||
| 28 | required: true, | ||
| 29 | default: None, | ||
| 30 | description: "Discord bot token", | ||
| 31 | }, | ||
| 32 | EnvVar { | ||
| 33 | name: "STATUS_CHANNEL_ID", | ||
| 34 | required: false, | ||
| 35 | default: None, | ||
| 36 | description: "status channel ID", | ||
| 37 | }, | ||
| 38 | EnvVar { | ||
| 39 | name: "ASSISTANT_KEYWORD", | ||
| 40 | required: false, | ||
| 41 | default: Some("jarvis"), | ||
| 42 | description: "keyword for AI assistant", | ||
| 43 | }, | ||
| 44 | EnvVar { | ||
| 45 | name: "COPYPARTY_URL", | ||
| 46 | required: false, | ||
| 47 | default: Some("http://localhost:3939"), | ||
| 48 | description: "Copyparty base URL", | ||
| 49 | }, | ||
| 50 | EnvVar { | ||
| 51 | name: "COPYPARTY_URL_UPLOAD", | ||
| 52 | required: false, | ||
| 53 | default: Some("http://localhost:3939"), | ||
| 54 | description: "Copyparty upload URL", | ||
| 55 | }, | ||
| 56 | EnvVar { | ||
| 57 | name: "COPYPARTY_PASSWORD", | ||
| 58 | required: false, | ||
| 59 | default: None, | ||
| 60 | description: "Copyparty auth password", | ||
| 61 | }, | ||
| 62 | EnvVar { | ||
| 63 | name: "OPENROUTER_API_KEY", | ||
| 64 | required: false, | ||
| 65 | default: None, | ||
| 66 | description: "OpenRouter API key (disables AI if missing)", | ||
| 67 | }, | ||
| 68 | EnvVar { | ||
| 69 | name: "OPENROUTER_MODEL", | ||
| 70 | required: false, | ||
| 71 | default: Some("cognitivecomputations/dolphin-mistral-24b-venice-edition:free"), | ||
| 72 | description: "OpenRouter model", | ||
| 73 | }, | ||
| 74 | EnvVar { | ||
| 75 | name: "SENATOR_DB_PATH", | ||
| 76 | required: false, | ||
| 77 | default: Some("data/senator.db"), | ||
| 78 | description: "SQLite database path", | ||
| 79 | }, | ||
| 80 | EnvVar { | ||
| 81 | name: "RUST_LOG", | ||
| 82 | required: false, | ||
| 83 | default: Some("info"), | ||
| 84 | description: "tracing/logging level filter", | ||
| 85 | }, | ||
| 86 | ]; | ||
| 87 | |||
| 88 | pub fn check_env_vars() { | ||
| 89 | let mut missing_required = Vec::new(); | ||
| 90 | let mut missing_optional = Vec::new(); | ||
| 91 | |||
| 92 | for var in ENV_VARS { | ||
| 93 | match env::var(var.name) { | ||
| 94 | Ok(_) => continue, | ||
| 95 | Err(_) => { | ||
| 96 | if var.required { | ||
| 97 | tracing::error!("MISSING REQUIRED: {} ({})", var.name, var.description); | ||
| 98 | missing_required.push(var.name); | ||
| 99 | } else { | ||
| 100 | match var.default { | ||
| 101 | Some(default) => { | ||
| 102 | tracing::warn!("{} not set, using default: {}", var.name, default) | ||
| 103 | } | ||
| 104 | None => tracing::warn!("{} not set ({})", var.name, var.description), | ||
| 105 | } | ||
| 106 | missing_optional.push(var.name); | ||
| 107 | } | ||
| 108 | } | ||
| 109 | } | ||
| 110 | } | ||
| 111 | |||
| 112 | if !missing_required.is_empty() { | ||
| 113 | tracing::error!( | ||
| 114 | "Missing required environment variables: {}. The application will not function correctly.", | ||
| 115 | missing_required.join(", ") | ||
| 116 | ); | ||
| 117 | } | ||
| 118 | |||
| 119 | if !missing_optional.is_empty() { | ||
| 120 | tracing::warn!( | ||
| 121 | "Missing optional environment variables: {}. Some features will be disabled or use defaults.", | ||
| 122 | missing_optional.join(", ") | ||
| 123 | ); | ||
| 124 | } | ||
| 125 | } | ||
| 126 | |||
| 127 | impl Config { | ||
| 128 | pub fn from_env() -> Self { | ||
| 129 | Self { | ||
| 130 | token: env::var("TOKEN").expect("Missing TOKEN"), | ||
| 131 | status_channel_id: env::var("STATUS_CHANNEL_ID") | ||
| 132 | .ok() | ||
| 133 | .and_then(|v| v.parse().ok()), | ||
| 134 | assistant_keyword: env::var("ASSISTANT_KEYWORD").unwrap_or_else(|_| "jarvis".into()), | ||
| 135 | copyparty_url: env::var("COPYPARTY_URL") | ||
| 136 | .unwrap_or_else(|_| "http://localhost:3939".into()), | ||
| 137 | copyparty_url_upload: env::var("COPYPARTY_URL_UPLOAD") | ||
| 138 | .unwrap_or_else(|_| "http://localhost:3939".into()), | ||
| 139 | copyparty_password: env::var("COPYPARTY_PASSWORD").ok(), | ||
| 140 | openrouter_api_key: env::var("OPENROUTER_API_KEY").ok(), | ||
| 141 | openrouter_model: env::var("OPENROUTER_MODEL").ok(), | ||
| 142 | database_path: env::var("SENATOR_DB_PATH").unwrap_or_else(|_| "data/senator.db".into()), | ||
| 143 | } | ||
| 144 | } | ||
| 145 | } | ||
