diff --git a/Rocket.skeleton.toml b/Rocket.skeleton.toml index 6cc1f87..cfe27e3 100644 --- a/Rocket.skeleton.toml +++ b/Rocket.skeleton.toml @@ -8,6 +8,9 @@ email_folder = "/path/to/email_folder" admins = ["admin@meet-os.com"] from_email = "no-reply@meet-os.com" from_name = "Meet-OS" + +database_username = "root" +database_password = "root" database_namespace = "meet-os-local-ns" database_name = "meet-os-local-db" diff --git a/src/db.rs b/src/db.rs index c2c4da9..6b3462a 100644 --- a/src/db.rs +++ b/src/db.rs @@ -20,7 +20,13 @@ pub fn fairing() -> AdHoc { AdHoc::on_ignite("Managed Database Connection", |rocket| async { let config = rocket.state::().unwrap(); - let dbh = get_database(&config.database_name, &config.database_namespace).await; + let dbh = get_database( + &config.database_username, + &config.database_password, + &config.database_name, + &config.database_namespace, + ) + .await; rocket.manage(dbh) }) @@ -29,17 +35,16 @@ pub fn fairing() -> AdHoc { /// # Panics /// /// Panics when it fails to create the database folder or set up the database. -pub async fn get_database(db_name: &str, db_namespace: &str) -> Surreal { +pub async fn get_database( + username: &str, + password: &str, + db_name: &str, + db_namespace: &str, +) -> Surreal { let address = "127.0.0.1:8000"; let dbh = Surreal::new::(address).await.unwrap(); - // TODO: get the credentials from Rocket.toml - dbh.signin(Root { - username: "root", - password: "root", - }) - .await - .unwrap(); + dbh.signin(Root { username, password }).await.unwrap(); dbh.use_ns(db_namespace).use_db(db_name).await.unwrap(); diff --git a/src/lib.rs b/src/lib.rs index 862ca90..7a44191 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -115,6 +115,8 @@ pub struct MyConfig { pub from_name: String, pub from_email: String, + pub database_username: String, + pub database_password: String, pub database_namespace: String, pub database_name: String, } diff --git a/src/test_db.rs b/src/test_db.rs index f6862c4..3b2c434 100644 --- a/src/test_db.rs +++ b/src/test_db.rs @@ -117,7 +117,7 @@ async fn test_db_get_empty_lists() { let database_name = format!("test-name-{}", rand::random::()); let database_namespace = "test-namespace-for-meet-os"; - let dbh = db::get_database(&database_name, &database_namespace).await; + let dbh = db::get_database("root", "root", &database_name, &database_namespace).await; let events = db::get_events(&dbh).await.unwrap(); assert!(events.is_empty()); @@ -138,7 +138,7 @@ async fn test_db_get_none() { let database_name = format!("test-name-{}", rand::random::()); let database_namespace = "test-namespace-for-meet-os"; - let dbh = db::get_database(&database_name, &database_namespace).await; + let dbh = db::get_database("root", "root", &database_name, &database_namespace).await; let event = db::get_event_by_eid(&dbh, 1).await.unwrap(); assert!(event.is_none()); @@ -160,7 +160,7 @@ async fn test_db_user() { let database_name = format!("test-name-{}", rand::random::()); let database_namespace = "test-namespace-for-meet-os"; - let dbh = db::get_database(&database_name, &database_namespace).await; + let dbh = db::get_database("root", "root", &database_name, &database_namespace).await; let utc: DateTime = Utc::now(); @@ -285,7 +285,7 @@ async fn test_db_groups() { let database_name = format!("test-name-{}", rand::random::()); let database_namespace = "test-namespace-for-meet-os"; - let dbh = db::get_database(&database_name, &database_namespace).await; + let dbh = db::get_database("root", "root", &database_name, &database_namespace).await; add_admin_helper(&dbh).await; add_owner_helper(&dbh).await; add_user_helper(&dbh).await; @@ -361,7 +361,7 @@ async fn test_db_events() { let database_name = format!("test-name-{}", rand::random::()); let database_namespace = "test-namespace-for-meet-os"; - let dbh = db::get_database(&database_name, &database_namespace).await; + let dbh = db::get_database("root", "root", &database_name, &database_namespace).await; add_admin_helper(&dbh).await; add_owner_helper(&dbh).await; @@ -409,7 +409,7 @@ async fn test_db_increment() { let database_name = format!("test-name-{}", rand::random::()); let database_namespace = "test-namespace-for-meet-os"; - let dbh = db::get_database(&database_name, &database_namespace).await; + let dbh = db::get_database("root", "root", &database_name, &database_namespace).await; let people = db::increment(&dbh, "people").await.unwrap(); assert_eq!(people, 1);