176 lines
5.3 KiB
Rust
176 lines
5.3 KiB
Rust
/*
|
|
* Copyright (C) 2022 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;
|
|
use std::path::Path;
|
|
|
|
use config::{builder::DefaultState, Config, ConfigBuilder, ConfigError, Environment, File};
|
|
use derive_more::Display;
|
|
use serde::Deserialize;
|
|
use serde::Serialize;
|
|
use tracing::{info, warn};
|
|
use url::Url;
|
|
|
|
const PREFIX: &str = "LPFORMS";
|
|
const SEPARATOR: &str = "_";
|
|
|
|
#[derive(Debug, Clone, Deserialize)]
|
|
pub struct Dashboard {
|
|
pub api_key: String,
|
|
}
|
|
|
|
#[derive(Debug, Clone, Deserialize)]
|
|
pub struct Server {
|
|
pub port: u32,
|
|
pub domain: String,
|
|
pub ip: String,
|
|
pub url_prefix: Option<String>,
|
|
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 DBType {
|
|
#[display(fmt = "postgres")]
|
|
Postgres,
|
|
// #[display(fmt = "maria")]
|
|
// Maria,
|
|
}
|
|
|
|
impl DBType {
|
|
fn from_url(url: &Url) -> Result<Self, ConfigError> {
|
|
match url.scheme() {
|
|
// "mysql" => Ok(Self::Maria),
|
|
"postgres" => Ok(Self::Postgres),
|
|
_ => Err(ConfigError::Message("Unknown database type".into())),
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Clone, Deserialize)]
|
|
pub struct Database {
|
|
pub url: String,
|
|
pub pool: u32,
|
|
pub database_type: DBType,
|
|
}
|
|
|
|
#[derive(Debug, Clone, Deserialize)]
|
|
pub struct Settings {
|
|
pub debug: bool,
|
|
pub database: Database,
|
|
pub server: Server,
|
|
pub source_code: String,
|
|
pub dash: Dashboard,
|
|
}
|
|
|
|
#[cfg(not(tarpaulin_include))]
|
|
impl Settings {
|
|
pub fn new() -> Result<Self, ConfigError> {
|
|
let mut s = Config::builder();
|
|
|
|
const CURRENT_DIR: &str = "./config/default.toml";
|
|
const ETC: &str = "/etc/librepages/forms/config.toml";
|
|
|
|
if let Ok(path) = env::var("LPFORMS_CONFIG") {
|
|
s = s.add_source(File::with_name(&path));
|
|
} else if Path::new(CURRENT_DIR).exists() {
|
|
// merging default config from file
|
|
s = s.add_source(File::with_name(CURRENT_DIR));
|
|
} else if Path::new(ETC).exists() {
|
|
s = s.add_source(File::with_name(ETC));
|
|
} else {
|
|
warn!("configuration file not found");
|
|
}
|
|
|
|
s = s.add_source(Environment::with_prefix(PREFIX).separator(SEPARATOR));
|
|
s = set_separator_field(s);
|
|
|
|
match env::var("PORT") {
|
|
Ok(val) => {
|
|
s = s.set_override("server.port", val).unwrap();
|
|
}
|
|
Err(e) => warn!("couldn't interpret PORT: {}", e),
|
|
}
|
|
|
|
if let Ok(val) = env::var("DATABASE_URL") {
|
|
let url = Url::parse(&val).expect("couldn't parse Database URL");
|
|
s = s.set_override("database.url", url.to_string()).unwrap();
|
|
let database_type = DBType::from_url(&url).unwrap();
|
|
s = s
|
|
.set_override("database.database_type", database_type.to_string())
|
|
.unwrap();
|
|
}
|
|
|
|
let s = s.build()?;
|
|
match s.try_deserialize::<Self>() {
|
|
Ok(val) => {
|
|
Ok(val)
|
|
},
|
|
Err(e) => Err(ConfigError::Message(format!("\n\nError: {}. If it says missing fields, then please refer to https://git.batsense.net/LibrePages/forms to learn more about how forms reads configuration\n\n", e))),
|
|
}
|
|
}
|
|
}
|
|
|
|
#[cfg(not(tarpaulin_include))]
|
|
fn set_separator_field(mut s: ConfigBuilder<DefaultState>) -> ConfigBuilder<DefaultState> {
|
|
// ref: https://github.com/mehcode/config-rs/issues/391
|
|
|
|
fn from_env(
|
|
s: ConfigBuilder<DefaultState>,
|
|
env_name: &str,
|
|
config_name: &str,
|
|
) -> ConfigBuilder<DefaultState> {
|
|
if let Ok(val) = env::var(env_name) {
|
|
info!("Overriding {config_name} with data from env var {env_name}");
|
|
s.set_override(config_name, val)
|
|
.unwrap_or_else(|_| panic!("Couldn't set {config_name} from env var {env_name}"))
|
|
} else {
|
|
s
|
|
}
|
|
}
|
|
s = from_env(s, &format!("{PREFIX}{SEPARATOR}SOURCE_CODE"), "source_code");
|
|
s = from_env(
|
|
s,
|
|
&format!("{PREFIX}{SEPARATOR}DASH{SEPARATOR}API_KEY"),
|
|
"dash.api_key",
|
|
);
|
|
|
|
s = from_env(
|
|
s,
|
|
&format!("{PREFIX}{SEPARATOR}SERVER{SEPARATOR}URL_PREFIX"),
|
|
"server.url_prefix",
|
|
);
|
|
|
|
s = from_env(
|
|
s,
|
|
&format!("{PREFIX}{SEPARATOR}DATABASE{SEPARATOR}POOL"),
|
|
"server.url_prefix",
|
|
);
|
|
|
|
s = from_env(
|
|
s,
|
|
&format!("{PREFIX}{SEPARATOR}SERVER{SEPARATOR}PROXY_HAS_TLS"),
|
|
"server.proxy_has_tls",
|
|
);
|
|
s
|
|
}
|