]>
Commit | Line | Data |
---|---|---|
768227f7 RBR |
1 | //! # Configuration |
2 | //! | |
3 | //! This module defines the configuration used to initialize both proxies. | |
4 | ||
5 | use log::error; | |
dc3d6821 | 6 | use std::env; |
768227f7 | 7 | use std::fmt::Debug; |
dc3d6821 | 8 | use std::sync::Arc; |
768227f7 RBR |
9 | use thiserror::Error; |
10 | ||
11 | #[derive(Error, Debug)] | |
494920f1 | 12 | pub enum Error { |
768227f7 RBR |
13 | #[error("Environment variable {0} not set.")] |
14 | MissingEnvVar(String), | |
15 | #[error("Failed to parse {0}.")] | |
16 | ParseError(String), | |
17 | } | |
dc3d6821 | 18 | |
768227f7 RBR |
19 | /// Configuration for any proxy |
20 | #[derive(Debug)] | |
2fdda21d | 21 | pub struct Proxy { |
dc3d6821 | 22 | pub local_port: u16, |
768227f7 RBR |
23 | pub bind_address: String, |
24 | pub remote_host: String, | |
dc3d6821 RBR |
25 | pub remote_port: u16, |
26 | pub protocol: &'static str, | |
27 | } | |
28 | ||
768227f7 | 29 | /// Aggregated configuration for both proxies, already in a reference counter. |
dc3d6821 | 30 | pub struct Configuration { |
2fdda21d RBR |
31 | pub imap_configuration: Arc<Proxy>, |
32 | pub smtp_configuration: Arc<Proxy>, | |
dc3d6821 RBR |
33 | } |
34 | ||
35 | impl Configuration { | |
768227f7 RBR |
36 | /// Creates a new configuration object by reading the environment |
37 | /// variables. Exits if the right ones aren't found. | |
494920f1 | 38 | pub fn new() -> Result<Self, Error> { |
768227f7 | 39 | Ok(Configuration { |
2fdda21d | 40 | imap_configuration: Arc::new(Proxy { |
768227f7 RBR |
41 | local_port: get_env_number("LOCAL_IMAP_PORT", 143)?, |
42 | bind_address: get_env_var("LOCAL_IMAP_BIND_ADDRESS", Some("0.0.0.0".to_string()))?, | |
6dca7b58 | 43 | remote_host: get_env_var("REMOTE_IMAP_HOST", None)?, |
768227f7 | 44 | remote_port: get_env_number("REMOTE_IMAP_PORT", 993)?, |
dc3d6821 RBR |
45 | protocol: "IMAP", |
46 | }), | |
2fdda21d | 47 | smtp_configuration: Arc::new(Proxy { |
768227f7 RBR |
48 | local_port: get_env_number("LOCAL_SMTP_PORT", 25)?, |
49 | bind_address: get_env_var("LOCAL_SMTP_BIND_ADDRESS", Some("0.0.0.0".to_string()))?, | |
6dca7b58 | 50 | remote_host: get_env_var("REMOTE_SMTP_HOST", None)?, |
99248d00 | 51 | remote_port: get_env_number("REMOTE_SMTP_PORT", 465)?, |
dc3d6821 RBR |
52 | protocol: "SMTP", |
53 | }), | |
768227f7 RBR |
54 | }) |
55 | } | |
56 | } | |
57 | ||
58 | /// Get an environment variable or return an error. | |
494920f1 | 59 | fn get_env_var(name: &str, default: Option<String>) -> Result<String, Error> { |
768227f7 RBR |
60 | match env::var(name) { |
61 | Ok(value) => Ok(value), | |
62 | Err(_) => match default { | |
63 | Some(default_value) => Ok(default_value), | |
494920f1 | 64 | None => Err(Error::MissingEnvVar(name.to_string())), |
768227f7 RBR |
65 | }, |
66 | } | |
67 | } | |
68 | ||
69 | /// Get an environment variable and parse it as a number. Return a default | |
70 | /// if not set. | |
494920f1 | 71 | fn get_env_number(name: &str, default: u16) -> Result<u16, Error> { |
768227f7 RBR |
72 | match env::var(name) { |
73 | Ok(value) => value | |
74 | .parse() | |
494920f1 | 75 | .map_err(|_| Error::ParseError(name.to_string())), |
768227f7 | 76 | Err(_) => Ok(default), |
dc3d6821 RBR |
77 | } |
78 | } |