Skip to content

Commit

Permalink
Merge pull request #9 from JakenHerman/jaken/configurable-connection-…
Browse files Browse the repository at this point in the history
…string

Modified connection string to be configurable via `.env`
  • Loading branch information
JakenHerman authored Oct 14, 2024
2 parents a1412a4 + f0db856 commit fd7464f
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 36 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
target/
*.sqlite
*.sqlite
.env
40 changes: 7 additions & 33 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
serde_json = "1.0.128"
dotenv = "0.15"
7 changes: 6 additions & 1 deletion src/db.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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))
}

0 comments on commit fd7464f

Please sign in to comment.