]> Untitled Git - lemmy.git/commitdiff
Better DB default fields. (#1560)
authorDessalines <dessalines@users.noreply.github.com>
Thu, 8 Apr 2021 11:19:58 +0000 (07:19 -0400)
committerGitHub <noreply@github.com>
Thu, 8 Apr 2021 11:19:58 +0000 (11:19 +0000)
* Better DB default fields.

* Fixing clippy

crates/utils/src/settings/defaults.rs
crates/utils/src/settings/mod.rs
crates/utils/src/settings/structs.rs
src/main.rs

index 1ca15d248ed0583d128ebbac3369d7e2fb1b3e8d..3e7b344f23a86e26a892ff11171d4d36b16c3119 100644 (file)
@@ -21,20 +21,15 @@ impl Default for Settings {
   }
 }
 
-pub(in crate::settings) static DEFAULT_DATABASE_USER: &str = "lemmy";
-pub(in crate::settings) static DEFAULT_DATABASE_PORT: i32 = 5432;
-pub(in crate::settings) static DEFAULT_DATABASE_DB: &str = "lemmy";
-pub static DEFAULT_DATABASE_POOL_SIZE: u32 = 5;
-
 impl Default for DatabaseConfig {
   fn default() -> Self {
     Self {
-      user: Some(DEFAULT_DATABASE_USER.to_string()),
+      user: Some("lemmy".to_string()),
       password: "password".into(),
       host: "localhost".into(),
-      port: Some(DEFAULT_DATABASE_PORT),
-      database: Some(DEFAULT_DATABASE_DB.to_string()),
-      pool_size: Some(DEFAULT_DATABASE_POOL_SIZE),
+      port: Some(5432),
+      database: Some("lemmy".to_string()),
+      pool_size: Some(5),
     }
   }
 }
index dfdfc59338044de2610cab8c791be387f5037a44..406bf7de0c87aff19c8396fdcde859545914a84b 100644 (file)
@@ -1,16 +1,13 @@
 use crate::{
   location_info,
-  settings::{
-    defaults::{DEFAULT_DATABASE_DB, DEFAULT_DATABASE_PORT, DEFAULT_DATABASE_USER},
-    structs::{
-      CaptchaConfig,
-      DatabaseConfig,
-      EmailConfig,
-      FederationConfig,
-      RateLimitConfig,
-      Settings,
-      SetupConfig,
-    },
+  settings::structs::{
+    CaptchaConfig,
+    DatabaseConfig,
+    EmailConfig,
+    FederationConfig,
+    RateLimitConfig,
+    Settings,
+    SetupConfig,
   },
   LemmyError,
 };
@@ -63,15 +60,11 @@ impl Settings {
     let conf = self.database();
     format!(
       "postgres://{}:{}@{}:{}/{}",
-      conf
-        .user
-        .unwrap_or_else(|| DEFAULT_DATABASE_USER.to_string()),
+      conf.user(),
       conf.password,
       conf.host,
-      conf.port.unwrap_or(DEFAULT_DATABASE_PORT),
-      conf
-        .database
-        .unwrap_or_else(|| DEFAULT_DATABASE_DB.to_string()),
+      conf.port(),
+      conf.database(),
     )
   }
 
index 05c7ed476da534399571e0da6115833924b18cbc..3863f6e3c2ad3312dbdaffa551dd759895558f53 100644 (file)
@@ -27,12 +27,40 @@ pub struct CaptchaConfig {
 
 #[derive(Debug, Deserialize, Clone)]
 pub struct DatabaseConfig {
-  pub user: Option<String>,
+  pub(super) user: Option<String>,
   pub password: String,
   pub host: String,
-  pub port: Option<i32>,
-  pub database: Option<String>,
-  pub pool_size: Option<u32>,
+  pub(super) port: Option<i32>,
+  pub(super) database: Option<String>,
+  pub(super) pool_size: Option<u32>,
+}
+
+impl DatabaseConfig {
+  pub fn user(&self) -> String {
+    self
+      .user
+      .to_owned()
+      .unwrap_or_else(|| DatabaseConfig::default().user.expect("missing user"))
+  }
+  pub fn port(&self) -> i32 {
+    self
+      .port
+      .unwrap_or_else(|| DatabaseConfig::default().port.expect("missing port"))
+  }
+  pub fn database(&self) -> String {
+    self.database.to_owned().unwrap_or_else(|| {
+      DatabaseConfig::default()
+        .database
+        .expect("missing database")
+    })
+  }
+  pub fn pool_size(&self) -> u32 {
+    self.pool_size.unwrap_or_else(|| {
+      DatabaseConfig::default()
+        .pool_size
+        .expect("missing pool_size")
+    })
+  }
 }
 
 #[derive(Debug, Deserialize, Clone)]
index 0e3cb49ff03df04f1a2e57b5f05556e7ba0778a1..8cda9ff4a9f801ee541b630a281b96749fd4a3eb 100644 (file)
@@ -16,7 +16,7 @@ use lemmy_routes::{feeds, images, nodeinfo, webfinger};
 use lemmy_server::{api_routes, code_migrations::run_advanced_migrations, scheduled_tasks};
 use lemmy_utils::{
   rate_limit::{rate_limiter::RateLimiter, RateLimit},
-  settings::{defaults::DEFAULT_DATABASE_POOL_SIZE, structs::Settings},
+  settings::structs::Settings,
   LemmyError,
 };
 use lemmy_websocket::{chat_server::ChatServer, LemmyContext};
@@ -38,12 +38,7 @@ async fn main() -> Result<(), LemmyError> {
   };
   let manager = ConnectionManager::<PgConnection>::new(&db_url);
   let pool = Pool::builder()
-    .max_size(
-      settings
-        .database()
-        .pool_size
-        .unwrap_or(DEFAULT_DATABASE_POOL_SIZE),
-    )
+    .max_size(settings.database().pool_size())
     .build(manager)
     .unwrap_or_else(|_| panic!("Error connecting to {}", db_url));