2021-10-31 15:12:12 +05:30
|
|
|
/*
|
|
|
|
* Copyright (C) 2021 Aravinth Manivannan <realaravinth@batsense.net>
|
|
|
|
*
|
|
|
|
* 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 <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
use std::env;
|
2021-11-02 15:47:55 +05:30
|
|
|
use std::fs;
|
2021-10-31 15:12:12 +05:30
|
|
|
use std::path::Path;
|
|
|
|
|
|
|
|
use config::{Config, ConfigError, Environment, File};
|
|
|
|
use log::warn;
|
|
|
|
use serde::Deserialize;
|
|
|
|
use url::Url;
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Deserialize)]
|
2025-01-20 04:31:15 +03:00
|
|
|
pub struct Settings {
|
|
|
|
pub debug: bool,
|
|
|
|
pub cache: Option<String>,
|
2021-10-31 15:12:12 +05:30
|
|
|
pub port: u32,
|
|
|
|
pub domain: String,
|
|
|
|
pub ip: String,
|
|
|
|
pub proxy_has_tls: bool,
|
|
|
|
pub workers: Option<usize>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(tarpaulin_include))]
|
|
|
|
impl Settings {
|
|
|
|
pub fn new() -> Result<Self, ConfigError> {
|
2023-10-20 14:31:21 +05:30
|
|
|
let mut s = Config::builder();
|
2021-10-31 15:12:12 +05:30
|
|
|
|
|
|
|
// setting default values
|
2021-10-31 22:47:09 +05:30
|
|
|
if let Ok(path) = env::var("LIBMEDIUM") {
|
2023-10-20 14:31:21 +05:30
|
|
|
s = s.add_source(File::with_name(&path));
|
2025-01-20 05:54:35 +03:00
|
|
|
} else if Path::new("./config.toml").exists() {
|
2025-01-20 06:04:17 +03:00
|
|
|
s = s.add_source(File::with_name("./config.toml"));
|
2021-10-31 15:12:12 +05:30
|
|
|
} else {
|
|
|
|
log::warn!("configuration file not found");
|
|
|
|
}
|
|
|
|
|
2023-10-20 14:31:21 +05:30
|
|
|
s = s.add_source(Environment::with_prefix("PAGES").separator("__"));
|
2021-10-31 15:12:12 +05:30
|
|
|
|
|
|
|
match env::var("PORT") {
|
|
|
|
Ok(val) => {
|
2023-10-20 14:31:21 +05:30
|
|
|
s = s.set_override("server.port", val).unwrap();
|
2021-10-31 15:12:12 +05:30
|
|
|
}
|
|
|
|
Err(e) => warn!("couldn't interpret PORT: {}", e),
|
|
|
|
}
|
|
|
|
|
2023-10-20 14:31:21 +05:30
|
|
|
let mut settings: Settings = s.build()?.try_deserialize::<Settings>()?;
|
2021-10-31 15:12:12 +05:30
|
|
|
|
2021-11-02 16:03:10 +05:30
|
|
|
if settings.cache.is_none() {
|
|
|
|
let tmp = env::temp_dir().join("libmedium_cache_path");
|
|
|
|
if !tmp.exists() {
|
|
|
|
fs::create_dir_all(&tmp).unwrap()
|
|
|
|
}
|
|
|
|
settings.cache = Some(tmp.to_str().unwrap().to_string())
|
|
|
|
}
|
|
|
|
|
|
|
|
let cache_path = settings.cache.as_ref().unwrap();
|
|
|
|
let cache_path = Path::new(&cache_path);
|
2021-11-02 15:47:55 +05:30
|
|
|
if !cache_path.exists() {
|
2023-02-19 20:08:50 +05:30
|
|
|
fs::create_dir(cache_path).unwrap();
|
2021-11-02 15:47:55 +05:30
|
|
|
}
|
|
|
|
if !cache_path.is_dir() {
|
2021-11-02 16:03:10 +05:30
|
|
|
panic!(
|
|
|
|
"Cache path {} must be a directory",
|
|
|
|
&settings.cache.as_ref().unwrap()
|
|
|
|
);
|
2021-11-02 15:47:55 +05:30
|
|
|
}
|
2021-10-31 15:12:12 +05:30
|
|
|
Ok(settings)
|
|
|
|
}
|
|
|
|
|
2025-01-20 04:31:15 +03:00
|
|
|
#[cfg(not(tarpaulin_include))]
|
|
|
|
pub fn get_ip(&self) -> String {
|
|
|
|
format!("{}:{}", self.ip, self.port)
|
|
|
|
}
|
2021-10-31 15:12:12 +05:30
|
|
|
}
|