aboutsummaryrefslogtreecommitdiff
path: root/src/db.rs
diff options
context:
space:
mode:
authorFxQnLr <[email protected]>2024-02-12 14:58:08 +0100
committerFxQnLr <[email protected]>2024-02-12 14:58:08 +0100
commit8ed77d7ab484121e9d70158e14c9fd6c243f1c70 (patch)
treedabecfb3eaec1420782eb9d3987e54ba83612b18 /src/db.rs
parente4832b4cf36ba0eaed298ee458498eddd7176590 (diff)
downloadwebol-8ed77d7ab484121e9d70158e14c9fd6c243f1c70.tar
webol-8ed77d7ab484121e9d70158e14c9fd6c243f1c70.tar.gz
webol-8ed77d7ab484121e9d70158e14c9fd6c243f1c70.zip
Close #9. Config impl with struct and files
Diffstat (limited to 'src/db.rs')
-rw-r--r--src/db.rs16
1 files changed, 2 insertions, 14 deletions
diff --git a/src/db.rs b/src/db.rs
index 8a6b16e..489a000 100644
--- a/src/db.rs
+++ b/src/db.rs
@@ -1,13 +1,7 @@
1#[cfg(debug_assertions)]
2use std::env;
3
4use serde::Serialize; 1use serde::Serialize;
5use sqlx::{PgPool, postgres::PgPoolOptions}; 2use sqlx::{PgPool, postgres::PgPoolOptions};
6use tracing::{debug, info}; 3use tracing::{debug, info};
7 4
8#[cfg(not(debug_assertions))]
9use crate::config::SETTINGS;
10
11#[derive(Serialize, Debug)] 5#[derive(Serialize, Debug)]
12pub struct Device { 6pub struct Device {
13 pub id: String, 7 pub id: String,
@@ -17,18 +11,12 @@ pub struct Device {
17 pub times: Option<Vec<i64>> 11 pub times: Option<Vec<i64>>
18} 12}
19 13
20pub async fn init_db_pool() -> PgPool { 14pub async fn init_db_pool(db_url: &str) -> PgPool {
21 #[cfg(not(debug_assertions))]
22 let db_url = SETTINGS.get_string("database.url").unwrap();
23
24 #[cfg(debug_assertions)]
25 let db_url = env::var("DATABASE_URL").unwrap();
26
27 debug!("attempt to connect dbPool to '{}'", db_url); 15 debug!("attempt to connect dbPool to '{}'", db_url);
28 16
29 let pool = PgPoolOptions::new() 17 let pool = PgPoolOptions::new()
30 .max_connections(5) 18 .max_connections(5)
31 .connect(&db_url) 19 .connect(db_url)
32 .await 20 .await
33 .unwrap(); 21 .unwrap();
34 22