()
| 30 | |
| 31 | impl Config { |
| 32 | pub fn new() -> Result<Config, handle_errors::Error> { |
| 33 | let config = Config::parse(); |
| 34 | |
| 35 | if env::var("BAD_WORDS_API_KEY").is_err() { |
| 36 | panic!("BadWords API key not set"); |
| 37 | } |
| 38 | |
| 39 | if env::var("PASETO_KEY").is_err() { |
| 40 | panic!("PASETO_KEY not set"); |
| 41 | } |
| 42 | |
| 43 | let port = std::env::var("PORT") |
| 44 | .ok() |
| 45 | .map(|val| val.parse::<u16>()) |
| 46 | .unwrap_or(Ok(config.port)) |
| 47 | .map_err(handle_errors::Error::ParseError)?; |
| 48 | |
| 49 | let db_user = env::var("POSTGRES_USER").unwrap_or_else(|_| config.db_user.to_owned()); |
| 50 | let db_password = env::var("POSTGRES_PASSWORD").unwrap(); |
| 51 | let db_host = env::var("POSTGRES_HOST").unwrap_or_else(|_| config.db_host.to_owned()); |
| 52 | let db_port = env::var("POSTGRES_PORT").unwrap_or_else(|_| config.db_port.to_string()); |
| 53 | let db_name = env::var("POSTGRES_DB").unwrap_or_else(|_| config.db_name.to_owned()); |
| 54 | |
| 55 | Ok(Config { |
| 56 | log_level: config.log_level, |
| 57 | port, |
| 58 | db_user, |
| 59 | db_password, |
| 60 | db_host, |
| 61 | db_port: db_port |
| 62 | .parse::<u16>() |
| 63 | .map_err(handle_errors::Error::ParseError)?, |
| 64 | db_name, |
| 65 | }) |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | #[cfg(test)] |
nothing calls this directly
no outgoing calls
no test coverage detected