diff --git a/src/main.rs b/src/main.rs index 3950777b..ef343a36 100644 --- a/src/main.rs +++ b/src/main.rs @@ -25,7 +25,7 @@ static USER_ID: Lazy>> = Lazy::new(|| Arc::new(Mutex::new(0))); lazy_static::lazy_static! { static ref DB_CLIENT: Arc> = { tokio::runtime::Runtime::new().unwrap().block_on(async { - return Arc::new(Mutex::new(DatabaseConnectix::default())); + return Arc::new(Mutex::new(DatabaseConnectix::new(std::env::var("DB_URL").unwrap()))); }) }; } diff --git a/src/user/auth.rs b/src/user/auth.rs index f76412de..532ce00b 100644 --- a/src/user/auth.rs +++ b/src/user/auth.rs @@ -1,4 +1,4 @@ -use anyhow::Error; +use anyhow::{Error, anyhow}; use sqlx::{query_as, query, postgres::{PgPoolOptions, PgPool}, types::Json}; use super::model::{Model, GlassModeration}; use uuid::Uuid; @@ -8,22 +8,6 @@ pub struct DatabaseConnectix { connection: PgPool } -impl Default for DatabaseConnectix { - fn default() -> Result { - tokio::runtime::Runtime::new().unwrap().block_on(async { - let uri = std::env::var("DB_URL")?; - let client = PgPoolOptions::new() - .max_connections(5) - .connect(&uri).await?; - - Ok(Self { - connection: client - }) - }) - - } -} - impl DatabaseConnectix { pub fn new(uri: &str) -> Result { tokio::runtime::Runtime::new().unwrap().block_on(async { @@ -38,23 +22,23 @@ impl DatabaseConnectix { } /// Gets a possible user id (if one exists) for a username. - pub async fn get_user_id(&mut self, username: &str) -> Result { + pub async fn get_user_id(&self, username: &str) -> Result { let user: Option = query_as( "select max(id) from users where username=$1 limit 1;" ) .bind(username) - .fetch_optional(&mut self.connection) + .fetch_optional(&self.connection) .await?; if user.is_none() { Ok(1) } else { - if user.id == 9999 { return Err(anyhow!("username is taken")); } - Ok(user.id+1) + if user.unwrap().id == 9999 { return Err(anyhow!("username is taken")); } + Ok(user.unwrap().id+1) } } - pub async fn post_user(&mut self, username: String, password: String) -> Result<(), Error> { + pub async fn post_user(&self, username: String, password: String) -> Result<(), Error> { let data: Model = Model { id: self.get_user_id(&username).await?, uuid: Uuid::new_v4(), @@ -66,17 +50,17 @@ impl DatabaseConnectix { let _ = sqlx::query("insert into users (id, uuid, username, password, mod) values ($1, $2, $3, $4, $5)") .bind(data.id).bind(data.uuid).bind(data.username).bind(data.password).bind(data.moderation_stats) - .execute(&mut self.connection) + .execute(&self.connection) .await?; Ok(()) } - pub async fn update_user(&mut self, username: &str, prev_username: &str, prev_id: i32) -> Result<(), Error> { + pub async fn update_user(&self, username: &str, prev_username: &str, prev_id: i32) -> Result<(), Error> { let id = self.get_user_id(username).await?; let _ = query("update users set username=$1, id=$2 where username=$3 and id=$4") .bind(username).bind(id).bind(prev_username).bind(prev_id) - .execute(&mut self.connection) + .execute(&self.connection) .await?; Ok(()) }