From f0db85659c01b5d920b4dabbcc1ae736736886b5 Mon Sep 17 00:00:00 2001 From: Jaken Herman Date: Mon, 14 Oct 2024 02:36:20 -0500 Subject: [PATCH] Modified connection string to be configurable via `.env` --- .gitignore | 3 ++- Cargo.lock | 40 +++++++--------------------------------- Cargo.toml | 3 ++- src/db.rs | 7 ++++++- 4 files changed, 17 insertions(+), 36 deletions(-) diff --git a/.gitignore b/.gitignore index 09c8b81..efd4542 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ target/ -*.sqlite \ No newline at end of file +*.sqlite +.env \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock index 5366080..35071a4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -253,17 +253,6 @@ dependencies = [ "syn", ] -[[package]] -name = "diesel_migrations" -version = "2.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8a73ce704bad4231f001bff3314d91dce4aba0770cee8b233991859abc15c1f6" -dependencies = [ - "diesel", - "migrations_internals", - "migrations_macros", -] - [[package]] name = "diesel_table_macro_syntax" version = "0.2.0" @@ -278,12 +267,18 @@ name = "dooly" version = "0.1.0" dependencies = [ "diesel", - "diesel_migrations", + "dotenv", "rocket", "serde", "serde_json", ] +[[package]] +name = "dotenv" +version = "0.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77c90badedccf4105eca100756a0b1289e191f6fcbdadd3cee1d2f614f97da8f" + [[package]] name = "dsl_auto_type" version = "0.1.2" @@ -682,27 +677,6 @@ version = "2.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" -[[package]] -name = "migrations_internals" -version = "2.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fd01039851e82f8799046eabbb354056283fb265c8ec0996af940f4e85a380ff" -dependencies = [ - "serde", - "toml", -] - -[[package]] -name = "migrations_macros" -version = "2.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ffb161cc72176cb37aa47f1fc520d3ef02263d67d661f44f05d05a079e1237fd" -dependencies = [ - "migrations_internals", - "proc-macro2", - "quote", -] - [[package]] name = "mime" version = "0.3.17" diff --git a/Cargo.toml b/Cargo.toml index a8ac656..b5b4257 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,4 +8,5 @@ edition = "2021" rocket = { version = "0.5.1", features = ["json"] } diesel = { version = "2.0", features = ["r2d2", "sqlite"] } serde = { version = "1.0.210", features = ["derive"] } -serde_json = "1.0.128" \ No newline at end of file +serde_json = "1.0.128" +dotenv = "0.15" \ No newline at end of file diff --git a/src/db.rs b/src/db.rs index f1e8e5f..9adb1e3 100644 --- a/src/db.rs +++ b/src/db.rs @@ -1,6 +1,8 @@ use diesel::prelude::*; use diesel::SqliteConnection; use rocket::serde::{Serialize, Deserialize}; +use dotenv::dotenv; +use std::env; use crate::schema::todos; @@ -19,6 +21,9 @@ pub struct NewTodoItem<'a> { } pub fn establish_connection() -> SqliteConnection { - let database_url = "db.sqlite"; + dotenv().ok(); + + let database_url = env::var("DATABASE_URL").expect("DATABASE_URL must be set"); + SqliteConnection::establish(&database_url).expect(&format!("Error connecting to {}", database_url)) }