diff --git a/Cargo.toml b/Cargo.toml index e6dcd84..33d01e7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,6 +21,7 @@ regex = "1.11" serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" simple_logger = "5.0.0" +sqlx = { version = "0.8", features = ["runtime-tokio", "tls-native-tls", "postgres"] } redis = { version = "0.31.0", features= ["tokio-comp"] } tokio-tungstenite = { version = "0.26", features = ["native-tls", "url"] } toml = "0.8" @@ -29,9 +30,6 @@ uuid = { version = "1.16", features = ["serde", "v7"] } random-string = "1.1" actix-ws = "0.3.0" futures-util = "0.3.31" -deadpool = "0.12" -diesel = "2.2" -diesel-async = { version = "0.5", features = ["deadpool", "postgres"] } [dependencies.tokio] version = "1.44" diff --git a/src/config.rs b/src/config.rs index 4e8fc21..65a5965 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,6 +1,7 @@ use crate::Error; use log::debug; use serde::Deserialize; +use sqlx::postgres::PgConnectOptions; use tokio::fs::read_to_string; #[derive(Debug, Deserialize)] @@ -80,24 +81,13 @@ pub struct Web { } impl Database { - pub fn url(&self) -> String { - let mut url = String::from("postgres://"); - - url += &self.username; - - url += ":"; - url += &self.password; - - url += "@"; - - url += &self.host; - url += ":"; - url += &self.port.to_string(); - - url += "/"; - url += &self.database; - - url + pub fn connect_options(&self) -> PgConnectOptions { + PgConnectOptions::new() + .database(&self.database) + .host(&self.host) + .username(&self.username) + .password(&self.password) + .port(self.port) } } diff --git a/src/main.rs b/src/main.rs index 9036665..fbad594 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,19 +3,14 @@ use actix_web::{App, HttpServer, web}; use argon2::Argon2; use clap::Parser; use simple_logger::SimpleLogger; -use diesel_async::pooled_connection::AsyncDieselConnectionManager; -use diesel_async::pooled_connection::deadpool::Pool; -use diesel_async::RunQueryDsl; +use sqlx::{PgPool, Pool, Postgres}; use std::time::SystemTime; mod config; use config::{Config, ConfigBuilder}; mod api; -type Conn = deadpool::managed::Object>; - pub mod structs; pub mod utils; -pub mod tables; type Error = Box; @@ -28,7 +23,7 @@ struct Args { #[derive(Clone)] pub struct Data { - pub pool: deadpool::managed::Pool, Conn>, + pub pool: Pool, pub cache_pool: redis::Client, pub _config: Config, pub argon2: Argon2<'static>, @@ -49,21 +44,17 @@ async fn main() -> Result<(), Error> { let web = config.web.clone(); - // create a new connection pool with the default config - let pool_config = AsyncDieselConnectionManager::::new(config.database.url()); - let pool = Pool::builder(pool_config).build()?; + let pool = PgPool::connect_with(config.database.connect_options()).await?; let cache_pool = redis::Client::open(config.cache_database.url())?; - let mut conn = pool.get().await?; - /* TODO: Figure out if a table should be used here and if not then what. Also figure out if these should be different types from what they currently are and if we should add more "constraints" TODO: References to time should be removed in favor of using the timestamp built in to UUIDv7 (apart from deleted_at in users) */ - diesel::sql_query( + sqlx::raw_sql( r#" CREATE TABLE IF NOT EXISTS users ( uuid uuid PRIMARY KEY NOT NULL, @@ -150,7 +141,7 @@ async fn main() -> Result<(), Error> { ); "#, ) - .execute(&mut conn) + .execute(&pool) .await?; /* diff --git a/src/structs.rs b/src/structs.rs index 7cec7c9..1b339b1 100644 --- a/src/structs.rs +++ b/src/structs.rs @@ -1,15 +1,14 @@ use std::str::FromStr; use actix_web::HttpResponse; -use diesel::Selectable; use log::error; use serde::{Deserialize, Serialize}; +use sqlx::{Pool, Postgres, prelude::FromRow}; use uuid::Uuid; -use crate::{Conn, Data, tables::*}; +use crate::Data; -#[derive(Serialize, Deserialize, Clone, Selectable)] -#[diesel(table_name = channels)] +#[derive(Serialize, Deserialize, Clone)] pub struct Channel { pub uuid: Uuid, pub guild_uuid: Uuid, @@ -18,7 +17,7 @@ pub struct Channel { pub permissions: Vec, } -#[derive(Serialize, Clone)] +#[derive(Serialize, Clone, FromRow)] struct ChannelPermissionBuilder { role_uuid: String, permissions: i32, @@ -33,8 +32,7 @@ impl ChannelPermissionBuilder { } } -#[derive(Serialize, Deserialize, Clone, Selectable)] -#[diesel(table_name = channel_permissions)] +#[derive(Serialize, Deserialize, Clone, FromRow)] pub struct ChannelPermission { pub role_uuid: Uuid, pub permissions: i32, @@ -42,10 +40,15 @@ pub struct ChannelPermission { impl Channel { pub async fn fetch_all( - conn: &mut Conn, + pool: &Pool, guild_uuid: Uuid, ) -> Result, HttpResponse> { - + let row = sqlx::query_as(&format!( + "SELECT CAST(uuid AS VARCHAR), name, description FROM channels WHERE guild_uuid = '{}'", + guild_uuid + )) + .fetch_all(pool) + .await; if let Err(error) = row { error!("{}", error); diff --git a/src/tables.rs b/src/tables.rs deleted file mode 100644 index 3dbd38b..0000000 --- a/src/tables.rs +++ /dev/null @@ -1,109 +0,0 @@ -use diesel::table; - -table! { - users (uuid) { - uuid -> Uuid, - username -> VarChar, - display_name -> Nullable, - password -> VarChar, - email -> VarChar, - email_verified -> Bool, - is_deleted -> Bool, - deleted_at -> Int8, - } -} - -table! { - instance_permissions (uuid) { - uuid -> Uuid, - administrator -> Bool, - } -} - -table! { - refresh_tokens (token) { - token -> VarChar, - uuid -> Uuid, - created_at -> Int8, - device_name -> VarChar, - } -} - -table! { - access_tokens (token) { - token -> VarChar, - refresh_token -> VarChar, - uuid -> Uuid, - created_at -> Int8 - } -} - -table! { - guilds (uuid) { - uuid -> Uuid, - owner_uuid -> Uuid, - name -> VarChar, - description -> VarChar - } -} - -table! { - guild_members (uuid) { - uuid -> Uuid, - guild_uuid -> Uuid, - user_uuid -> Uuid, - nickname -> VarChar, - } -} - -table! { - roles (uuid, guild_uuid) { - uuid -> Uuid, - guild_uuid -> Uuid, - name -> VarChar, - color -> Int4, - position -> Int4, - permissions -> Int8, - } -} - -table! { - role_members (role_uuid, member_uuid) { - role_uuid -> Uuid, - member_uuid -> Uuid, - } -} - -table! { - channels (uuid) { - uuid -> Uuid, - guild_uuid -> Uuid, - name -> VarChar, - description -> VarChar, - } -} - -table! { - channel_permissions (channel_uuid, role_uuid) { - channel_uuid -> Uuid, - role_uuid -> Uuid, - permissions -> Int8, - } -} - -table! { - messages (uuid) { - uuid -> Uuid, - channel_uuid -> Uuid, - user_uuid -> Uuid, - message -> VarChar, - } -} - -table! { - invites (id) { - id -> VarChar, - guild_uuid -> Uuid, - user_uuid -> Uuid, - } -}