From fc40706ca992f2b74df26dc415a4e71f52ec74b2 Mon Sep 17 00:00:00 2001 From: realaravinth Date: Mon, 21 Feb 2022 00:42:53 +0530 Subject: [PATCH] feat: get admin_email in settings and validate admin_email and source_code SUMMARY New field is added to settings to receive email of the admin of the instance. source_code is a link to the repository of the source code of a GitPad instance, set_source_code is defined to build a HTTP link to the exact commit from which the instance binary was built. NOTE Current Settings::set_source_code assumes the provided HTTP link to point to a forge belonging to the GitHub-family(GitHub, Gitea and GitLab), provisions must be made to accommodate other forges as well. --- config/default.toml | 1 + src/settings.rs | 21 +++++++++++++++++++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/config/default.toml b/config/default.toml index 487ddd4..a561eef 100644 --- a/config/default.toml +++ b/config/default.toml @@ -2,6 +2,7 @@ log = "info" # possible values: "info", "warn", "trace", "error", "debug" source_code = "https://github.com/realaravinth/gitpad" allow_registration = true # allow registration on server allow_demo = true # allow demo on server +admin_email = "admin@gitpad.example.com" [server] # The port at which you want authentication to listen to diff --git a/src/settings.rs b/src/settings.rs index 9e0b907..56e8060 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -22,6 +22,7 @@ use derive_more::Display; use log::warn; use serde::Deserialize; use url::Url; +use validator::Validate; #[derive(Debug, Clone, Deserialize)] pub struct Server { @@ -137,19 +138,32 @@ pub struct Database { pub database_type: DBType, } -#[derive(Debug, Clone, Deserialize)] +#[derive(Debug, Validate, Clone, Deserialize)] pub struct Settings { pub log: LogLevel, pub database: Database, pub allow_registration: bool, pub allow_demo: bool, pub server: Server, + #[validate(url)] pub source_code: String, pub repository: Repository, + #[validate(email)] + pub admin_email: String, } #[cfg(not(tarpaulin_include))] impl Settings { + fn set_source_code(&mut self) { + if !self.source_code.ends_with('/') { + self.source_code.push('/'); + } + let mut base = url::Url::parse(&self.source_code).unwrap(); + base = base.join("tree/").unwrap(); + base = base.join(crate::GIT_COMMIT_HASH).unwrap(); + self.source_code = base.into(); + } + pub fn new() -> Result { let mut s = Config::new(); @@ -194,10 +208,13 @@ impl Settings { set_database_url(&mut s); - let settings: Settings = s.try_into()?; + let mut settings: Settings = s.try_into()?; settings.log.set_log_level(); settings.repository.create_root_dir(); + settings.validate().unwrap(); + settings.set_source_code(); + settings.validate().unwrap(); Ok(settings) }