Added some logging in database code and fixed some warnings

This commit is contained in:
Gvidas Juknevičius 2024-03-25 23:32:33 +02:00
parent 2cb4acc604
commit 779381df2d
Signed by: MCorange
GPG Key ID: 12B1346D720B7FBB
3 changed files with 27 additions and 12 deletions

View File

@ -1,3 +1,4 @@
use anyhow::bail;
use sqlx::{postgres::PgPoolOptions, Postgres}; use sqlx::{postgres::PgPoolOptions, Postgres};
use crate::config::definition::Config; use crate::config::definition::Config;
@ -14,12 +15,24 @@ pub struct Database {
impl Database { impl Database {
pub async fn new(config: &Config) -> anyhow::Result<Self> { pub async fn new(config: &Config) -> anyhow::Result<Self> {
log::info!("Database connecting to {}", config.database.url);
let conn = PgPoolOptions::new() let conn = PgPoolOptions::new()
.max_connections(5) .max_connections(5)
.connect(&config.database.url).await?; .connect(&config.database.url).await;
match conn {
Ok(c) => {
log::info!("Connection successfull");
Ok(Self { Ok(Self {
connection: conn connection: c
}) })
},
Err(e) => {
log::error!("Failed to connect to database: {e}");
bail!(e)
},
}
} }
pub fn connection(&self) -> &sqlx::Pool<Postgres> { pub fn connection(&self) -> &sqlx::Pool<Postgres> {

View File

@ -30,6 +30,7 @@ impl Users {
.execute(db.connection()) .execute(db.connection())
.await?; .await?;
log::debug!("Created user with id '{id}'");
Ok(id) Ok(id)
} }
@ -126,6 +127,8 @@ impl Users {
.execute(db.connection()) .execute(db.connection())
.await?; .await?;
log::debug!("Deleted user with id '{}'", self.id);
Ok(()) Ok(())
} }
} }

View File

@ -4,7 +4,6 @@ mod templates;
use actix_web::{web, App, HttpServer}; use actix_web::{web, App, HttpServer};
use actix_files as actix_fs; use actix_files as actix_fs;
use sqlx::database;
use crate::{config::definition::Config, database::Database}; use crate::{config::definition::Config, database::Database};