librepages/src/settings.rs

193 lines
5.7 KiB
Rust
Raw Normal View History

/*
2022-03-29 17:36:22 +05:30
* 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 std::sync::Arc;
2022-09-10 19:21:49 +05:30
use config::{Config, ConfigError, Environment, File};
use derive_more::Display;
#[cfg(not(test))]
use log::{error, warn};
#[cfg(test)]
use std::{println as warn, println as error};
use serde::Deserialize;
2022-09-10 19:21:49 +05:30
use serde::Serialize;
use url::Url;
2022-04-26 20:12:17 +05:30
use crate::errors::*;
use crate::page::Page;
2022-09-16 14:49:13 +05:30
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Server {
pub port: u32,
pub ip: String,
pub workers: Option<usize>,
2022-09-12 01:38:58 +05:30
pub cookie_secret: String,
2022-09-07 01:58:25 +05:30
pub domain: String,
}
impl Server {
#[cfg(not(tarpaulin_include))]
pub fn get_ip(&self) -> String {
format!("{}:{}", self.ip, self.port)
}
}
2022-09-10 19:21:49 +05:30
#[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())),
}
}
}
2022-09-16 14:49:13 +05:30
#[derive(Debug, Clone, Serialize, Deserialize)]
2022-09-10 19:21:49 +05:30
pub struct Database {
pub url: String,
pub pool: u32,
pub database_type: DBType,
}
2022-09-16 14:49:13 +05:30
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Settings {
2022-09-12 00:23:38 +05:30
pub allow_registration: bool,
2022-09-16 14:49:13 +05:30
pub support_email: String,
2022-09-10 19:21:49 +05:30
pub debug: bool,
pub server: Server,
pub source_code: String,
2022-09-10 19:21:49 +05:30
pub database: Database,
pub page: PageConfig,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PageConfig {
pub base_path: String,
pub base_domain: String,
}
#[cfg(not(tarpaulin_include))]
impl Settings {
2022-04-26 20:12:17 +05:30
pub fn new() -> ServiceResult<Self> {
2022-03-29 18:13:26 +05:30
let mut s = Config::builder();
const CURRENT_DIR: &str = "./config/default.toml";
const ETC: &str = "/etc/static-pages/config.toml";
2022-03-29 19:05:31 +05:30
let mut read_file = false;
if Path::new(ETC).exists() {
s = s.add_source(File::with_name(ETC));
read_file = true;
}
if Path::new(CURRENT_DIR).exists() {
// merging default config from file
2022-03-29 18:13:26 +05:30
s = s.add_source(File::with_name(CURRENT_DIR));
2022-03-29 19:05:31 +05:30
read_file = true;
}
if let Ok(path) = env::var("PAGES_CONFIG") {
s = s.add_source(File::with_name(&path));
read_file = true;
}
if !read_file {
warn!("configuration file not found");
}
2022-03-29 18:13:26 +05:30
s = s.add_source(Environment::with_prefix("PAGES").separator("__"));
match env::var("PORT") {
Ok(val) => {
2022-09-10 19:21:49 +05:30
s = s.set_override("server.port", val).unwrap();
}
Err(e) => warn!("couldn't interpret PORT: {}", e),
}
2022-09-10 19:21:49 +05:30
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 intermediate_config = s.build_cloned().unwrap();
s = s
.set_override(
"database.url",
format!(
r"postgres://{}:{}@{}:{}/{}",
intermediate_config
.get::<String>("database.username")
.expect("Couldn't access database username"),
intermediate_config
.get::<String>("database.password")
.expect("Couldn't access database password"),
intermediate_config
.get::<String>("database.hostname")
.expect("Couldn't access database hostname"),
intermediate_config
.get::<String>("database.port")
.expect("Couldn't access database port"),
intermediate_config
.get::<String>("database.name")
.expect("Couldn't access database name")
),
)
.expect("Couldn't set database url");
let settings = s.build()?.try_deserialize::<Settings>()?;
settings.check_url();
Ok(settings)
}
pub fn init(&self) {
fn create_dir_util(path: &Path) {
if path.exists() && path.is_file() {
panic!("Path is a file, should be a directory: {:?}", path);
}
if !path.exists() {
std::fs::create_dir_all(&path).unwrap();
2022-04-26 20:12:17 +05:30
}
}
create_dir_util(Path::new(&self.page.base_path));
}
2022-03-29 18:13:26 +05:30
#[cfg(not(tarpaulin_include))]
fn check_url(&self) {
Url::parse(&self.source_code).expect("Please enter a URL for source_code in settings");
}
}