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.
master
Aravinth Manivannan 2022-02-21 00:42:53 +05:30
parent b7fe7ea5d5
commit fc40706ca9
Signed by: realaravinth
GPG Key ID: AD9F0F08E855ED88
2 changed files with 20 additions and 2 deletions

View File

@ -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

View File

@ -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<Self, ConfigError> {
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)
}