]> Untitled Git - lemmy.git/blobdiff - crates/db_queries/src/source/secret.rs
Moving settings and secrets to context.
[lemmy.git] / crates / db_queries / src / source / secret.rs
index eac0d7eb4905b39e5897d042ddcc78b9fdad9b2a..6181549fea88c7f2a89e4dddb297344fd23da23a 100644 (file)
@@ -1,36 +1,18 @@
 use diesel::{result::Error, *};
 use lemmy_db_schema::source::secret::Secret;
-use lemmy_utils::settings::structs::Settings;
-use std::sync::RwLock;
 
-use crate::get_database_url_from_env;
-
-lazy_static! {
-  static ref SECRET: RwLock<Secret> = RwLock::new(init().expect("Failed to load secrets from DB."));
-}
-
-pub trait SecretSingleton {
-  fn get() -> Secret;
+pub trait Secret_ {
+  fn init(conn: &PgConnection) -> Result<Secret, Error>;
 }
 
-impl SecretSingleton for Secret {
-  /// Returns the Secret as a struct
-  fn get() -> Self {
-    SECRET.read().expect("read secrets").to_owned()
+impl Secret_ for Secret {
+  /// Initialize the Secrets from the DB.
+  /// Warning: You should only call this once.
+  fn init(conn: &PgConnection) -> Result<Secret, Error> {
+    read_secrets(conn)
   }
 }
 
-/// Reads the secrets from the DB
-fn init() -> Result<Secret, Error> {
-  let db_url = match get_database_url_from_env() {
-    Ok(url) => url,
-    Err(_) => Settings::get().get_database_url(),
-  };
-
-  let conn = PgConnection::establish(&db_url).expect("Couldn't get DB connection for Secrets.");
-  read_secrets(&conn)
-}
-
 fn read_secrets(conn: &PgConnection) -> Result<Secret, Error> {
   use lemmy_db_schema::schema::secret::dsl::*;
   secret.first::<Secret>(conn)