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) }