From f9efb062e04fb73b6f987e62773e1f08e1e8a372 Mon Sep 17 00:00:00 2001 From: realaravinth Date: Wed, 20 Jul 2022 17:42:02 +0530 Subject: [PATCH] feat: accept alt DB configuration --- config/default.toml | 1 + src/settings.rs | 86 ++++++++++++++++++++++++--------------------- 2 files changed, 47 insertions(+), 40 deletions(-) diff --git a/config/default.toml b/config/default.toml index 210da8dc..899c0e09 100644 --- a/config/default.toml +++ b/config/default.toml @@ -50,6 +50,7 @@ username = "postgres" password = "password" name = "postgres" pool = 4 +database_type="postgres" # "postgres", "maria" [redis] # This section deals with the database location and how to access it diff --git a/src/settings.rs b/src/settings.rs index 67c5f439..6a836894 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -18,8 +18,9 @@ use std::env; use std::path::Path; use config::{Config, ConfigError, Environment, File}; +use derive_more::Display; use log::{debug, warn}; -use serde::Deserialize; +use serde::{Deserialize, Serialize}; use url::Url; #[derive(Debug, Clone, Deserialize)] @@ -65,28 +66,47 @@ impl Server { } } -#[derive(Debug, Clone, Deserialize)] -struct DatabaseBuilder { - pub port: u32, - pub hostname: String, - pub username: String, - pub password: String, - pub name: String, +//#[derive(Debug, Clone, Deserialize)] +//struct DatabaseBuilder { +// pub port: u32, +// pub hostname: String, +// pub username: String, +// pub password: String, +// pub name: String, +//} + +//impl DatabaseBuilder { +// #[cfg(not(tarpaulin_include))] +// fn extract_database_url(url: &Url) -> Self { +// debug!("Databse name: {}", url.path()); +// let mut path = url.path().split('/'); +// path.next(); +// let name = path.next().expect("no database name").to_string(); +// DatabaseBuilder { +// port: url.port().expect("Enter database port").into(), +// hostname: url.host().expect("Enter database host").to_string(), +// username: url.username().into(), +// password: url.password().expect("Enter database password").into(), +// name, +// } +// } +//} + +#[derive(Deserialize, Serialize, Display, PartialEq, Clone, Debug)] +#[serde(rename_all = "lowercase")] +pub enum DBType { + #[display(fmt = "postgres")] + Postgres, + #[display(fmt = "maria")] + Maria, } -impl DatabaseBuilder { - #[cfg(not(tarpaulin_include))] - fn extract_database_url(url: &Url) -> Self { - debug!("Databse name: {}", url.path()); - let mut path = url.path().split('/'); - path.next(); - let name = path.next().expect("no database name").to_string(); - DatabaseBuilder { - port: url.port().expect("Enter database port").into(), - hostname: url.host().expect("Enter database host").to_string(), - username: url.username().into(), - password: url.password().expect("Enter database password").into(), - name, +impl DBType { + fn from_url(url: &Url) -> Result { + match url.scheme() { + "mysql" => Ok(Self::Maria), + "postgres" => Ok(Self::Postgres), + _ => Err(ConfigError::Message("Unknown database type".into())), } } } @@ -95,6 +115,7 @@ impl DatabaseBuilder { pub struct Database { pub url: String, pub pool: u32, + pub database_type: DBType, } #[derive(Debug, Clone, Deserialize)] @@ -153,14 +174,13 @@ impl Settings { match env::var("DATABASE_URL") { Ok(val) => { let url = Url::parse(&val).expect("couldn't parse Database URL"); - let database_conf = DatabaseBuilder::extract_database_url(&url); - set_from_database_url(&mut s, &database_conf); + s.set("database.url", url.to_string()).unwrap(); + } + Err(e) => { + set_database_url(&mut s); } - Err(e) => warn!("couldn't interpret DATABASE_URL: {}", e), } - set_database_url(&mut s); - // setting default values #[cfg(test)] s.set("database.pool", 2.to_string()) @@ -182,20 +202,6 @@ fn check_url(s: &Config) { Url::parse(&url).expect("Please enter a URL for source_code in settings"); } -#[cfg(not(tarpaulin_include))] -fn set_from_database_url(s: &mut Config, database_conf: &DatabaseBuilder) { - s.set("database.username", database_conf.username.clone()) - .expect("Couldn't set database username"); - s.set("database.password", database_conf.password.clone()) - .expect("Couldn't access database password"); - s.set("database.hostname", database_conf.hostname.clone()) - .expect("Couldn't access database hostname"); - s.set("database.port", database_conf.port as i64) - .expect("Couldn't access database port"); - s.set("database.name", database_conf.name.clone()) - .expect("Couldn't access database name"); -} - #[cfg(not(tarpaulin_include))] fn set_database_url(s: &mut Config) { s.set(