diff options
author | FxQnLr <[email protected]> | 2023-10-21 21:31:02 +0200 |
---|---|---|
committer | GitHub <[email protected]> | 2023-10-21 21:31:02 +0200 |
commit | e8a8b2d33eec5da6701b04181b14de755e8b8063 (patch) | |
tree | 5cd77421900fd8ec02f05d15e6fb195f28dfceb4 /src/db.rs | |
parent | 48943e0de28b99d7f108e96b7d250bbf1a6c5a5b (diff) | |
parent | bf1aeb7191bfaa75f1acf47c675bc68b9fac1cd8 (diff) | |
download | webol-e8a8b2d33eec5da6701b04181b14de755e8b8063.tar webol-e8a8b2d33eec5da6701b04181b14de755e8b8063.tar.gz webol-e8a8b2d33eec5da6701b04181b14de755e8b8063.zip |
Merge pull request #4 from FxQnLr/database-init
int db in process
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 | } | ||