|
| 1 | +use std::time::Duration; |
| 2 | + |
| 3 | +use bytesize::ByteSize; |
| 4 | +use trogon_nats::NatsConfig; |
| 5 | +use trogon_std::env::ReadEnv; |
| 6 | + |
| 7 | +use crate::constants::{ |
| 8 | + DEFAULT_MAX_BODY_SIZE, DEFAULT_NATS_ACK_TIMEOUT, DEFAULT_PORT, DEFAULT_STREAM_MAX_AGE, |
| 9 | + DEFAULT_STREAM_NAME, DEFAULT_SUBJECT_PREFIX, |
| 10 | +}; |
| 11 | + |
| 12 | +/// Configuration for the Telegram webhook source. |
| 13 | +/// |
| 14 | +/// Resolved from environment variables: |
| 15 | +/// - `TELEGRAM_WEBHOOK_SECRET`: secret token configured via `setWebhook` (**required**) |
| 16 | +/// - `TELEGRAM_SOURCE_PORT`: HTTP listening port (default: 8080) |
| 17 | +/// - `TELEGRAM_SUBJECT_PREFIX`: NATS subject prefix (default: `telegram`) |
| 18 | +/// - `TELEGRAM_STREAM_NAME`: JetStream stream name (default: `TELEGRAM`) |
| 19 | +/// - `TELEGRAM_STREAM_MAX_AGE_SECS`: max age in seconds (default: 604800 / 7 days) |
| 20 | +/// - `TELEGRAM_NATS_ACK_TIMEOUT_SECS`: NATS ack timeout in seconds (default: 10) |
| 21 | +/// - `TELEGRAM_MAX_BODY_SIZE`: maximum body size in bytes (default: 10 MB) |
| 22 | +/// - Standard `NATS_*` variables for NATS connection (see `trogon-nats`) |
| 23 | +pub struct TelegramSourceConfig { |
| 24 | + pub webhook_secret: String, |
| 25 | + pub port: u16, |
| 26 | + pub subject_prefix: String, |
| 27 | + pub stream_name: String, |
| 28 | + pub stream_max_age: Duration, |
| 29 | + pub nats_ack_timeout: Duration, |
| 30 | + pub max_body_size: ByteSize, |
| 31 | + pub nats: NatsConfig, |
| 32 | +} |
| 33 | + |
| 34 | +impl TelegramSourceConfig { |
| 35 | + pub fn from_env<E: ReadEnv>(env: &E) -> Self { |
| 36 | + Self { |
| 37 | + webhook_secret: env |
| 38 | + .var("TELEGRAM_WEBHOOK_SECRET") |
| 39 | + .ok() |
| 40 | + .filter(|s| !s.is_empty()) |
| 41 | + .expect("TELEGRAM_WEBHOOK_SECRET is required"), |
| 42 | + port: env |
| 43 | + .var("TELEGRAM_SOURCE_PORT") |
| 44 | + .ok() |
| 45 | + .and_then(|p| p.parse().ok()) |
| 46 | + .unwrap_or(DEFAULT_PORT), |
| 47 | + subject_prefix: env |
| 48 | + .var("TELEGRAM_SUBJECT_PREFIX") |
| 49 | + .unwrap_or_else(|_| DEFAULT_SUBJECT_PREFIX.to_string()), |
| 50 | + stream_name: env |
| 51 | + .var("TELEGRAM_STREAM_NAME") |
| 52 | + .unwrap_or_else(|_| DEFAULT_STREAM_NAME.to_string()), |
| 53 | + stream_max_age: env |
| 54 | + .var("TELEGRAM_STREAM_MAX_AGE_SECS") |
| 55 | + .ok() |
| 56 | + .and_then(|v| v.parse().ok()) |
| 57 | + .map(Duration::from_secs) |
| 58 | + .unwrap_or(DEFAULT_STREAM_MAX_AGE), |
| 59 | + nats_ack_timeout: env |
| 60 | + .var("TELEGRAM_NATS_ACK_TIMEOUT_SECS") |
| 61 | + .ok() |
| 62 | + .and_then(|v| v.parse().ok()) |
| 63 | + .map(Duration::from_secs) |
| 64 | + .unwrap_or(DEFAULT_NATS_ACK_TIMEOUT), |
| 65 | + max_body_size: env |
| 66 | + .var("TELEGRAM_MAX_BODY_SIZE") |
| 67 | + .ok() |
| 68 | + .and_then(|v| v.parse::<u64>().ok()) |
| 69 | + .map(ByteSize) |
| 70 | + .unwrap_or(DEFAULT_MAX_BODY_SIZE), |
| 71 | + nats: NatsConfig::from_env(env), |
| 72 | + } |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +#[cfg(test)] |
| 77 | +mod tests { |
| 78 | + use super::*; |
| 79 | + use trogon_std::env::InMemoryEnv; |
| 80 | + |
| 81 | + fn env_with_secret() -> InMemoryEnv { |
| 82 | + let env = InMemoryEnv::new(); |
| 83 | + env.set("TELEGRAM_WEBHOOK_SECRET", "test-secret"); |
| 84 | + env |
| 85 | + } |
| 86 | + |
| 87 | + #[test] |
| 88 | + fn defaults_with_required_secret() { |
| 89 | + let env = env_with_secret(); |
| 90 | + let config = TelegramSourceConfig::from_env(&env); |
| 91 | + |
| 92 | + assert_eq!(config.webhook_secret, "test-secret"); |
| 93 | + assert_eq!(config.port, 8080); |
| 94 | + assert_eq!(config.subject_prefix, "telegram"); |
| 95 | + assert_eq!(config.stream_name, "TELEGRAM"); |
| 96 | + assert_eq!( |
| 97 | + config.stream_max_age, |
| 98 | + Duration::from_secs(7 * 24 * 60 * 60) |
| 99 | + ); |
| 100 | + assert_eq!(config.nats_ack_timeout, Duration::from_secs(10)); |
| 101 | + assert_eq!(config.max_body_size, ByteSize::mib(10)); |
| 102 | + } |
| 103 | + |
| 104 | + #[test] |
| 105 | + fn reads_all_env_vars() { |
| 106 | + let env = InMemoryEnv::new(); |
| 107 | + env.set("TELEGRAM_WEBHOOK_SECRET", "my-secret"); |
| 108 | + env.set("TELEGRAM_SOURCE_PORT", "9090"); |
| 109 | + env.set("TELEGRAM_SUBJECT_PREFIX", "tg"); |
| 110 | + env.set("TELEGRAM_STREAM_NAME", "TG_EVENTS"); |
| 111 | + env.set("TELEGRAM_STREAM_MAX_AGE_SECS", "3600"); |
| 112 | + env.set("TELEGRAM_NATS_ACK_TIMEOUT_SECS", "30"); |
| 113 | + env.set("TELEGRAM_MAX_BODY_SIZE", "1048576"); |
| 114 | + |
| 115 | + let config = TelegramSourceConfig::from_env(&env); |
| 116 | + |
| 117 | + assert_eq!(config.webhook_secret, "my-secret"); |
| 118 | + assert_eq!(config.port, 9090); |
| 119 | + assert_eq!(config.subject_prefix, "tg"); |
| 120 | + assert_eq!(config.stream_name, "TG_EVENTS"); |
| 121 | + assert_eq!(config.stream_max_age, Duration::from_secs(3600)); |
| 122 | + assert_eq!(config.nats_ack_timeout, Duration::from_secs(30)); |
| 123 | + assert_eq!(config.max_body_size, ByteSize::mib(1)); |
| 124 | + } |
| 125 | + |
| 126 | + #[test] |
| 127 | + #[should_panic(expected = "TELEGRAM_WEBHOOK_SECRET is required")] |
| 128 | + fn missing_webhook_secret_panics() { |
| 129 | + let env = InMemoryEnv::new(); |
| 130 | + TelegramSourceConfig::from_env(&env); |
| 131 | + } |
| 132 | + |
| 133 | + #[test] |
| 134 | + #[should_panic(expected = "TELEGRAM_WEBHOOK_SECRET is required")] |
| 135 | + fn empty_webhook_secret_panics() { |
| 136 | + let env = InMemoryEnv::new(); |
| 137 | + env.set("TELEGRAM_WEBHOOK_SECRET", ""); |
| 138 | + TelegramSourceConfig::from_env(&env); |
| 139 | + } |
| 140 | + |
| 141 | + #[test] |
| 142 | + fn invalid_port_falls_back_to_default() { |
| 143 | + let env = env_with_secret(); |
| 144 | + env.set("TELEGRAM_SOURCE_PORT", "not-a-number"); |
| 145 | + let config = TelegramSourceConfig::from_env(&env); |
| 146 | + assert_eq!(config.port, 8080); |
| 147 | + } |
| 148 | + |
| 149 | + #[test] |
| 150 | + fn invalid_max_age_falls_back_to_default() { |
| 151 | + let env = env_with_secret(); |
| 152 | + env.set("TELEGRAM_STREAM_MAX_AGE_SECS", "not-a-number"); |
| 153 | + let config = TelegramSourceConfig::from_env(&env); |
| 154 | + assert_eq!(config.stream_max_age, DEFAULT_STREAM_MAX_AGE); |
| 155 | + } |
| 156 | + |
| 157 | + #[test] |
| 158 | + fn invalid_nats_ack_timeout_falls_back_to_default() { |
| 159 | + let env = env_with_secret(); |
| 160 | + env.set("TELEGRAM_NATS_ACK_TIMEOUT_SECS", "not-a-number"); |
| 161 | + let config = TelegramSourceConfig::from_env(&env); |
| 162 | + assert_eq!(config.nats_ack_timeout, DEFAULT_NATS_ACK_TIMEOUT); |
| 163 | + } |
| 164 | + |
| 165 | + #[test] |
| 166 | + fn invalid_max_body_size_falls_back_to_default() { |
| 167 | + let env = env_with_secret(); |
| 168 | + env.set("TELEGRAM_MAX_BODY_SIZE", "not-a-number"); |
| 169 | + let config = TelegramSourceConfig::from_env(&env); |
| 170 | + assert_eq!(config.max_body_size, DEFAULT_MAX_BODY_SIZE); |
| 171 | + } |
| 172 | +} |
0 commit comments