Skip to content

Commit

Permalink
move database username and password to Rocket.toml
Browse files Browse the repository at this point in the history
  • Loading branch information
szabgab committed Sep 18, 2024
1 parent 063e27a commit ec3e937
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 15 deletions.
3 changes: 3 additions & 0 deletions Rocket.skeleton.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
23 changes: 14 additions & 9 deletions src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,13 @@ pub fn fairing() -> AdHoc {
AdHoc::on_ignite("Managed Database Connection", |rocket| async {
let config = rocket.state::<MyConfig>().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)
})
Expand All @@ -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<Client> {
pub async fn get_database(
username: &str,
password: &str,
db_name: &str,
db_namespace: &str,
) -> Surreal<Client> {
let address = "127.0.0.1:8000";
let dbh = Surreal::new::<Ws>(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();

Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down
12 changes: 6 additions & 6 deletions src/test_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ async fn test_db_get_empty_lists() {
let database_name = format!("test-name-{}", rand::random::<f64>());
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());
Expand All @@ -138,7 +138,7 @@ async fn test_db_get_none() {
let database_name = format!("test-name-{}", rand::random::<f64>());
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());
Expand All @@ -160,7 +160,7 @@ async fn test_db_user() {
let database_name = format!("test-name-{}", rand::random::<f64>());
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> = Utc::now();

Expand Down Expand Up @@ -285,7 +285,7 @@ async fn test_db_groups() {
let database_name = format!("test-name-{}", rand::random::<f64>());
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;
Expand Down Expand Up @@ -361,7 +361,7 @@ async fn test_db_events() {
let database_name = format!("test-name-{}", rand::random::<f64>());
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;
Expand Down Expand Up @@ -409,7 +409,7 @@ async fn test_db_increment() {
let database_name = format!("test-name-{}", rand::random::<f64>());
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);
Expand Down

0 comments on commit ec3e937

Please sign in to comment.