diff options
author | fx <[email protected]> | 2023-10-21 20:46:31 +0200 |
---|---|---|
committer | fx <[email protected]> | 2023-10-21 20:46:31 +0200 |
commit | f5928b90748b0bb4c0c498ccc77ebde4eaec8841 (patch) | |
tree | e17f41b5722b1e6d36b4ae55332eb89fdf57b4f0 /src/main.rs | |
parent | 48943e0de28b99d7f108e96b7d250bbf1a6c5a5b (diff) | |
download | webol-f5928b90748b0bb4c0c498ccc77ebde4eaec8841.tar webol-f5928b90748b0bb4c0c498ccc77ebde4eaec8841.tar.gz webol-f5928b90748b0bb4c0c498ccc77ebde4eaec8841.zip |
add init function for db tables
Diffstat (limited to 'src/main.rs')
-rw-r--r-- | src/main.rs | 25 |
1 files changed, 3 insertions, 22 deletions
diff --git a/src/main.rs b/src/main.rs index b7306ea..8b4e9eb 100644 --- a/src/main.rs +++ b/src/main.rs | |||
@@ -3,11 +3,11 @@ use std::sync::Arc; | |||
3 | use axum::{Router, routing::post}; | 3 | use axum::{Router, routing::post}; |
4 | use axum::routing::{get, put}; | 4 | use axum::routing::{get, put}; |
5 | use sqlx::PgPool; | 5 | use sqlx::PgPool; |
6 | use sqlx::postgres::PgPoolOptions; | ||
7 | use time::util::local_offset; | 6 | use time::util::local_offset; |
8 | use tracing::{debug, info, level_filters::LevelFilter}; | 7 | use tracing::{info, level_filters::LevelFilter}; |
9 | use tracing_subscriber::{EnvFilter, fmt::{self, time::LocalTime}, prelude::*}; | 8 | use tracing_subscriber::{EnvFilter, fmt::{self, time::LocalTime}, prelude::*}; |
10 | use crate::config::SETTINGS; | 9 | use crate::config::SETTINGS; |
10 | use crate::db::{init_db_pool, setup_db}; | ||
11 | use crate::routes::device::{get_device, post_device, put_device}; | 11 | use crate::routes::device::{get_device, post_device, put_device}; |
12 | use crate::routes::start::start; | 12 | use crate::routes::start::start; |
13 | 13 | ||
@@ -41,6 +41,7 @@ async fn main() { | |||
41 | info!("start webol v{}", version); | 41 | info!("start webol v{}", version); |
42 | 42 | ||
43 | let db = init_db_pool().await; | 43 | let db = init_db_pool().await; |
44 | setup_db(&db).await.unwrap(); | ||
44 | 45 | ||
45 | let shared_state = Arc::new(AppState { db }); | 46 | let shared_state = Arc::new(AppState { db }); |
46 | 47 | ||
@@ -62,23 +63,3 @@ async fn main() { | |||
62 | pub struct AppState { | 63 | pub struct AppState { |
63 | db: PgPool | 64 | db: PgPool |
64 | } | 65 | } |
65 | |||
66 | async fn init_db_pool() -> PgPool { | ||
67 | #[cfg(not(debug_assertions))] | ||
68 | let db_url = SETTINGS.get_string("database.url").unwrap(); | ||
69 | |||
70 | #[cfg(debug_assertions)] | ||
71 | let db_url = env::var("DATABASE_URL").unwrap(); | ||
72 | |||
73 | debug!("attempt to connect dbPool to '{}'", db_url); | ||
74 | |||
75 | let pool = PgPoolOptions::new() | ||
76 | .max_connections(5) | ||
77 | .connect(&db_url) | ||
78 | .await | ||
79 | .unwrap(); | ||
80 | |||
81 | info!("dbPool successfully connected to '{}'", db_url); | ||
82 | |||
83 | pool | ||
84 | } | ||