2021-10-29 20:23:07 +05:30
|
|
|
/*
|
2022-03-29 17:36:22 +05:30
|
|
|
* Copyright (C) 2022 Aravinth Manivannan <realaravinth@batsense.net>
|
2021-10-29 20:23:07 +05:30
|
|
|
*
|
|
|
|
* 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;
|
2022-04-27 10:52:24 +05:30
|
|
|
use std::sync::Arc;
|
2021-10-29 20:23:07 +05:30
|
|
|
|
2022-09-10 19:21:49 +05:30
|
|
|
use config::{Config, ConfigError, Environment, File};
|
|
|
|
use derive_more::Display;
|
2022-04-27 11:50:06 +05:30
|
|
|
#[cfg(not(test))]
|
|
|
|
use log::{error, warn};
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
use std::{println as warn, println as error};
|
|
|
|
|
2021-10-29 20:23:07 +05:30
|
|
|
use serde::Deserialize;
|
2022-09-10 19:21:49 +05:30
|
|
|
use serde::Serialize;
|
2021-10-29 20:23:07 +05:30
|
|
|
use url::Url;
|
|
|
|
|
2022-04-26 20:12:17 +05:30
|
|
|
use crate::errors::*;
|
2021-10-29 20:23:07 +05:30
|
|
|
use crate::page::Page;
|
|
|
|
|
2022-09-16 14:49:13 +05:30
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
2021-10-29 20:23:07 +05:30
|
|
|
pub struct Server {
|
|
|
|
pub port: u32,
|
|
|
|
pub ip: String,
|
2021-10-30 15:30:38 +05:30
|
|
|
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,
|
2021-10-29 20:23:07 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
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)]
|
2021-10-29 20:23:07 +05:30
|
|
|
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,
|
2021-10-29 20:23:07 +05:30
|
|
|
pub server: Server,
|
|
|
|
pub source_code: String,
|
2022-09-10 19:21:49 +05:30
|
|
|
pub database: Database,
|
2022-11-09 14:19:59 +05:30
|
|
|
pub page: PageConfig,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
|
|
pub struct PageConfig {
|
|
|
|
pub base_path: String,
|
2021-10-29 20:23:07 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
#[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();
|
|
|
|
|
2021-10-29 20:23:07 +05:30
|
|
|
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() {
|
2021-10-29 20:23:07 +05:30
|
|
|
// 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 {
|
2022-04-27 11:50:06 +05:30
|
|
|
warn!("configuration file not found");
|
2021-10-29 20:23:07 +05:30
|
|
|
}
|
|
|
|
|
2022-03-29 18:13:26 +05:30
|
|
|
s = s.add_source(Environment::with_prefix("PAGES").separator("__"));
|
2021-10-29 20:23:07 +05:30
|
|
|
|
|
|
|
match env::var("PORT") {
|
|
|
|
Ok(val) => {
|
2022-09-10 19:21:49 +05:30
|
|
|
s = s.set_override("server.port", val).unwrap();
|
2021-10-29 20:23:07 +05:30
|
|
|
}
|
|
|
|
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();
|
2022-04-27 11:50:06 +05:30
|
|
|
|
|
|
|
Ok(settings)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn init(&self) {
|
2022-11-10 16:26:19 +05:30
|
|
|
fn create_dir_util(path: &Path) {
|
|
|
|
if path.exists() && path.is_file() {
|
|
|
|
panic!("Path is a file, should be a directory: {:?}", path);
|
2021-10-29 20:23:07 +05:30
|
|
|
}
|
2022-11-09 14:19:59 +05:30
|
|
|
|
2022-11-10 16:26:19 +05:30
|
|
|
if !path.exists() {
|
|
|
|
std::fs::create_dir_all(&path).unwrap();
|
2022-04-26 20:12:17 +05:30
|
|
|
}
|
2021-10-29 20:23:07 +05:30
|
|
|
}
|
2022-11-10 16:26:19 +05:30
|
|
|
|
|
|
|
// create_dir_util(Path::new(&page.path));
|
|
|
|
create_dir_util(Path::new(&self.page.base_path));
|
|
|
|
|
|
|
|
// for (index, page) in self.pages.iter().enumerate() {
|
|
|
|
// Url::parse(&page.repo).unwrap();
|
|
|
|
//
|
|
|
|
// for (index2, page2) in self.pages.iter().enumerate() {
|
|
|
|
// if index2 == index {
|
|
|
|
// continue;
|
|
|
|
// }
|
|
|
|
// if page.secret == page2.secret {
|
|
|
|
// error!("{}", ServiceError::SecretTaken(page.clone(), page2.clone()));
|
|
|
|
// } else if page.repo == page2.repo {
|
|
|
|
// error!(
|
|
|
|
// "{}",
|
|
|
|
// ServiceError::DuplicateRepositoryURL(page.clone(), page2.clone(),)
|
|
|
|
// );
|
|
|
|
// } else if page.path == page2.path {
|
|
|
|
// error!("{}", ServiceError::PathTaken(page.clone(), page2.clone()));
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// if let Err(e) = page.update(&page.branch) {
|
|
|
|
// error!("{e}");
|
|
|
|
// }
|
|
|
|
// }
|
2021-10-29 20:23:07 +05:30
|
|
|
}
|
|
|
|
|
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");
|
|
|
|
}
|
2021-10-29 20:23:07 +05:30
|
|
|
}
|