More helpful error messages when creating adaptors
This commit is contained in:
parent
2ba96cc9cb
commit
b586b4c88d
8
backend/Cargo.lock
generated
8
backend/Cargo.lock
generated
|
|
@ -617,7 +617,7 @@ dependencies = [
|
|||
"chrono",
|
||||
"common",
|
||||
"datastore-adaptor",
|
||||
"dotenv",
|
||||
"dotenvy",
|
||||
"memory-adaptor",
|
||||
"punycode",
|
||||
"rand",
|
||||
|
|
@ -814,12 +814,6 @@ dependencies = [
|
|||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "dotenv"
|
||||
version = "0.15.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "77c90badedccf4105eca100756a0b1289e191f6fcbdadd3cee1d2f614f97da8f"
|
||||
|
||||
[[package]]
|
||||
name = "dotenvy"
|
||||
version = "0.15.7"
|
||||
|
|
|
|||
|
|
@ -20,7 +20,7 @@ common = { path = "common" }
|
|||
sql-adaptor = { path = "adaptors/sql" }
|
||||
datastore-adaptor = { path = "adaptors/datastore" }
|
||||
memory-adaptor = { path = "adaptors/memory" }
|
||||
dotenv = "0.15.0"
|
||||
dotenvy = "0.15.7"
|
||||
serde_json = "1.0.96"
|
||||
rand = "0.8.5"
|
||||
punycode = "0.4.1"
|
||||
|
|
|
|||
|
|
@ -203,19 +203,22 @@ impl Adaptor for DatastoreAdaptor {
|
|||
|
||||
impl DatastoreAdaptor {
|
||||
pub async fn new() -> Self {
|
||||
let project_name = env::var("GCP_PROJECT_NAME").unwrap();
|
||||
|
||||
// Load credentials
|
||||
let credentials: ApplicationCredentials =
|
||||
serde_json::from_str(&env::var("GCP_CREDENTIALS").unwrap()).unwrap();
|
||||
let credentials: ApplicationCredentials = serde_json::from_str(
|
||||
&env::var("GCP_CREDENTIALS").expect("Expected GCP_CREDENTIALS environment variable"),
|
||||
)
|
||||
.expect("GCP_CREDENTIALS environment variable is not valid JSON");
|
||||
|
||||
// Connect to datastore
|
||||
let client = Client::from_credentials(project_name.clone(), credentials)
|
||||
let client = Client::from_credentials(credentials.project_id.clone(), credentials.clone())
|
||||
.await
|
||||
.unwrap();
|
||||
.expect("Failed to setup datastore client");
|
||||
let client = Mutex::new(client);
|
||||
|
||||
println!("🎛️ Connected to datastore in project {}", project_name);
|
||||
println!(
|
||||
"🎛️ Connected to datastore in project {}",
|
||||
credentials.project_id
|
||||
);
|
||||
|
||||
Self { client }
|
||||
}
|
||||
|
|
|
|||
|
|
@ -158,23 +158,28 @@ async fn get_stats_row(db: &DatabaseConnection) -> Result<stats::ActiveModel, Db
|
|||
|
||||
impl SqlAdaptor {
|
||||
pub async fn new() -> Self {
|
||||
let connection_string = env::var("DATABASE_URL").unwrap();
|
||||
let connection_string =
|
||||
env::var("DATABASE_URL").expect("Expected DATABASE_URL environment variable");
|
||||
|
||||
// Connect to the database
|
||||
let db = Database::connect(&connection_string).await.unwrap();
|
||||
let db = Database::connect(&connection_string)
|
||||
.await
|
||||
.expect("Failed to connect to SQL database");
|
||||
println!(
|
||||
"{} Connected to database at {}",
|
||||
match db {
|
||||
DatabaseConnection::SqlxMySqlPoolConnection(_) => "🐬",
|
||||
DatabaseConnection::SqlxPostgresPoolConnection(_) => "🐘",
|
||||
DatabaseConnection::SqlxSqlitePoolConnection(_) => "🪶",
|
||||
DatabaseConnection::Disconnected => panic!("Failed to connect"),
|
||||
DatabaseConnection::Disconnected => panic!("Failed to connect to SQL database"),
|
||||
},
|
||||
connection_string
|
||||
);
|
||||
|
||||
// Setup tables
|
||||
Migrator::up(&db, None).await.unwrap();
|
||||
Migrator::up(&db, None)
|
||||
.await
|
||||
.expect("Failed to set up tables in the database");
|
||||
|
||||
Self { db }
|
||||
}
|
||||
|
|
|
|||
|
|
@ -35,7 +35,7 @@ async fn main() {
|
|||
tracing_subscriber::fmt::init();
|
||||
|
||||
// Load env
|
||||
dotenv::dotenv().ok();
|
||||
dotenvy::dotenv().ok();
|
||||
|
||||
let shared_state = Arc::new(Mutex::new(ApiState {
|
||||
adaptor: create_adaptor().await,
|
||||
|
|
|
|||
Loading…
Reference in a new issue