/* * Copyright (C) 2022 Aravinth Manivannan * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as * published by the Free Software Foundation, either version 3 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ use std::env; use std::path::Path; use config::{Config, ConfigError, Environment, File}; use derive_more::Display; use log::warn; use serde::Deserialize; use serde::Serialize; use url::Url; #[derive(Debug, Clone, Deserialize)] pub struct Server { pub port: u32, pub domain: String, pub ip: String, pub url_prefix: Option, pub proxy_has_tls: bool, } impl Server { #[cfg(not(tarpaulin_include))] pub fn get_ip(&self) -> String { format!("{}:{}", self.ip, self.port) } } #[derive(Deserialize, Serialize, Display, Eq, PartialEq, Clone, Debug)] #[serde(rename_all = "lowercase")] pub enum ConductorType { /// doesn't do anything #[display(fmt = "dummy")] Dummy, } #[derive(Debug, Clone, Deserialize)] pub struct Settings { pub debug: bool, pub server: Server, pub source_code: String, pub conductor: ConductorType, } #[cfg(not(tarpaulin_include))] impl Settings { pub fn new() -> Result { let mut s = Config::new(); const CURRENT_DIR: &str = "./config/default.toml"; const ETC: &str = "/etc/lpconductor/config.toml"; if let Ok(path) = env::var("LPCONDUCTOR_CONFIG") { s.merge(File::with_name(&path))?; } else if Path::new(CURRENT_DIR).exists() { // merging default config from file s.merge(File::with_name(CURRENT_DIR))?; } else if Path::new(ETC).exists() { s.merge(File::with_name(ETC))?; } else { log::warn!("configuration file not found"); } s.merge(Environment::with_prefix("LPCONDUCTOR").separator("_"))?; check_url(&s); match env::var("PORT") { Ok(val) => { s.set("server.port", val).unwrap(); } Err(e) => warn!("couldn't interpret PORT: {}", e), } match s.try_into::() { Ok(val) => { Ok(val) }, Err(e) => Err(ConfigError::Message(format!("\n\nError: {}. If it says missing fields, then please refer to https://github.com/mCaptcha/mcaptcha#configuration to learn more about how mcaptcha reads configuration\n\n", e))), } } } #[cfg(not(tarpaulin_include))] fn check_url(s: &Config) { let url = s .get::("source_code") .expect("Couldn't access source_code"); Url::parse(&url).expect("Please enter a URL for source_code in settings"); }