|
|
@ -1,4 +1,4 @@
|
|
|
|
use std::{env, thread::sleep, time::Duration, error::Error};
|
|
|
|
use std::{env, error::Error, thread::sleep, time::Duration};
|
|
|
|
|
|
|
|
|
|
|
|
use cassandra_cpp::{stmt, Cluster, Session};
|
|
|
|
use cassandra_cpp::{stmt, Cluster, Session};
|
|
|
|
|
|
|
|
|
|
|
@ -9,16 +9,21 @@ pub mod user_repository;
|
|
|
|
type Result<T> = std::result::Result<T, Box<dyn Error>>;
|
|
|
|
type Result<T> = std::result::Result<T, Box<dyn Error>>;
|
|
|
|
|
|
|
|
|
|
|
|
/// Keyspace name if none is given via ENV: KEYSPACE_NAME
|
|
|
|
/// Keyspace name if none is given via ENV: KEYSPACE_NAME
|
|
|
|
static DEFAULT_KEYSPACE_NAME: &str = "rust_solidjs_cassandra";
|
|
|
|
static DEFAULT_CASSANDRA_KEYSPACE_NAME: &str = "rust_solidjs_cassandra";
|
|
|
|
|
|
|
|
/// Server address if none is given via ENV: CASSANDRA_SERVER_ADDRESS
|
|
|
|
|
|
|
|
static DEFAULT_CASSANDRA_SERVER_ADDRESS: &str = "127.0.0.1";
|
|
|
|
|
|
|
|
|
|
|
|
/// Waits for the cassandra database to become available -> then returns a session.
|
|
|
|
/// Waits for the cassandra database to become available -> then returns a session.
|
|
|
|
pub fn init() -> Session {
|
|
|
|
pub fn init() -> Session {
|
|
|
|
let keyspace_name = env::var("KEYSPACE_NAME").unwrap_or(DEFAULT_KEYSPACE_NAME.to_string());
|
|
|
|
let keyspace_name =
|
|
|
|
|
|
|
|
env::var("CASSANDRA_KEYSPACE_NAME").unwrap_or(DEFAULT_CASSANDRA_KEYSPACE_NAME.to_string());
|
|
|
|
|
|
|
|
let cassandra_server_addr = env::var("CASSANDRA_SERVER_ADDRESS")
|
|
|
|
|
|
|
|
.unwrap_or(DEFAULT_CASSANDRA_SERVER_ADDRESS.to_string());
|
|
|
|
// Definitely set it so other modules can use it
|
|
|
|
// Definitely set it so other modules can use it
|
|
|
|
env::set_var("KEYSPACE_NAME", &keyspace_name);
|
|
|
|
env::set_var("CASSANDRA_KEYSPACE_NAME", &keyspace_name);
|
|
|
|
|
|
|
|
|
|
|
|
let mut cluster = Cluster::default();
|
|
|
|
let mut cluster = Cluster::default();
|
|
|
|
cluster.set_contact_points("127.0.0.1").unwrap(); // Panic if not successful
|
|
|
|
cluster.set_contact_points(&cassandra_server_addr).unwrap(); // Panic if not successful
|
|
|
|
let mut count = 0;
|
|
|
|
let mut count = 0;
|
|
|
|
let session = loop {
|
|
|
|
let session = loop {
|
|
|
|
match cluster.connect() {
|
|
|
|
match cluster.connect() {
|
|
|
@ -27,7 +32,7 @@ pub fn init() -> Session {
|
|
|
|
count += 1;
|
|
|
|
count += 1;
|
|
|
|
log::warn!("Could not connect to database! Retrying... ({count})");
|
|
|
|
log::warn!("Could not connect to database! Retrying... ({count})");
|
|
|
|
sleep(Duration::new(5, 0));
|
|
|
|
sleep(Duration::new(5, 0));
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
let query = stmt!(&format!(
|
|
|
|
let query = stmt!(&format!(
|
|
|
|