diff options
Diffstat (limited to 'src/db.rs')
-rw-r--r-- | src/db.rs | 26 |
1 files changed, 25 insertions, 1 deletions
@@ -1,8 +1,32 @@ | |||
1 | use std::env; | ||
2 | |||
1 | use serde::Serialize; | 3 | use serde::Serialize; |
4 | use sqlx::{PgPool, postgres::PgPoolOptions}; | ||
5 | use tracing::{debug, info}; | ||
2 | 6 | ||
3 | #[derive(Serialize)] | 7 | #[derive(Serialize)] |
4 | pub struct Device { | 8 | pub struct Device { |
5 | pub id: String, | 9 | pub id: String, |
6 | pub mac: String, | 10 | pub mac: String, |
7 | pub broadcast_addr: String | 11 | pub broadcast_addr: String |
8 | } \ No newline at end of file | 12 | } |
13 | |||
14 | pub async fn init_db_pool() -> PgPool { | ||
15 | #[cfg(not(debug_assertions))] | ||
16 | let db_url = SETTINGS.get_string("database.url").unwrap(); | ||
17 | |||
18 | #[cfg(debug_assertions)] | ||
19 | let db_url = env::var("DATABASE_URL").unwrap(); | ||
20 | |||
21 | debug!("attempt to connect dbPool to '{}'", db_url); | ||
22 | |||
23 | let pool = PgPoolOptions::new() | ||
24 | .max_connections(5) | ||
25 | .connect(&db_url) | ||
26 | .await | ||
27 | .unwrap(); | ||
28 | |||
29 | info!("dbPool successfully connected to '{}'", db_url); | ||
30 | |||
31 | pool | ||
32 | } | ||