Compare commits

..

No commits in common. "f1d5b4316eeccac7be3ceee0d5d8f2e57d2cdf9d" and "fd8d8234048c08d90aef2885c0c0d725b8ccb83f" have entirely different histories.

5 changed files with 26 additions and 153 deletions

View file

@ -21,6 +21,7 @@ regex = "1.11"
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0" serde_json = "1.0"
simple_logger = "5.0.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"] } redis = { version = "0.31.0", features= ["tokio-comp"] }
tokio-tungstenite = { version = "0.26", features = ["native-tls", "url"] } tokio-tungstenite = { version = "0.26", features = ["native-tls", "url"] }
toml = "0.8" toml = "0.8"
@ -29,9 +30,6 @@ uuid = { version = "1.16", features = ["serde", "v7"] }
random-string = "1.1" random-string = "1.1"
actix-ws = "0.3.0" actix-ws = "0.3.0"
futures-util = "0.3.31" futures-util = "0.3.31"
deadpool = "0.12"
diesel = "2.2"
diesel-async = { version = "0.5", features = ["deadpool", "postgres"] }
[dependencies.tokio] [dependencies.tokio]
version = "1.44" version = "1.44"

View file

@ -1,6 +1,7 @@
use crate::Error; use crate::Error;
use log::debug; use log::debug;
use serde::Deserialize; use serde::Deserialize;
use sqlx::postgres::PgConnectOptions;
use tokio::fs::read_to_string; use tokio::fs::read_to_string;
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
@ -80,24 +81,13 @@ pub struct Web {
} }
impl Database { impl Database {
pub fn url(&self) -> String { pub fn connect_options(&self) -> PgConnectOptions {
let mut url = String::from("postgres://"); PgConnectOptions::new()
.database(&self.database)
url += &self.username; .host(&self.host)
.username(&self.username)
url += ":"; .password(&self.password)
url += &self.password; .port(self.port)
url += "@";
url += &self.host;
url += ":";
url += &self.port.to_string();
url += "/";
url += &self.database;
url
} }
} }

View file

@ -3,19 +3,14 @@ use actix_web::{App, HttpServer, web};
use argon2::Argon2; use argon2::Argon2;
use clap::Parser; use clap::Parser;
use simple_logger::SimpleLogger; use simple_logger::SimpleLogger;
use diesel_async::pooled_connection::AsyncDieselConnectionManager; use sqlx::{PgPool, Pool, Postgres};
use diesel_async::pooled_connection::deadpool::Pool;
use diesel_async::RunQueryDsl;
use std::time::SystemTime; use std::time::SystemTime;
mod config; mod config;
use config::{Config, ConfigBuilder}; use config::{Config, ConfigBuilder};
mod api; mod api;
type Conn = deadpool::managed::Object<AsyncDieselConnectionManager<diesel_async::AsyncPgConnection>>;
pub mod structs; pub mod structs;
pub mod utils; pub mod utils;
pub mod tables;
type Error = Box<dyn std::error::Error>; type Error = Box<dyn std::error::Error>;
@ -28,7 +23,7 @@ struct Args {
#[derive(Clone)] #[derive(Clone)]
pub struct Data { pub struct Data {
pub pool: deadpool::managed::Pool<AsyncDieselConnectionManager<diesel_async::AsyncPgConnection>, Conn>, pub pool: Pool<Postgres>,
pub cache_pool: redis::Client, pub cache_pool: redis::Client,
pub _config: Config, pub _config: Config,
pub argon2: Argon2<'static>, pub argon2: Argon2<'static>,
@ -49,21 +44,17 @@ async fn main() -> Result<(), Error> {
let web = config.web.clone(); let web = config.web.clone();
// create a new connection pool with the default config let pool = PgPool::connect_with(config.database.connect_options()).await?;
let pool_config = AsyncDieselConnectionManager::<diesel_async::AsyncPgConnection>::new(config.database.url());
let pool = Pool::builder(pool_config).build()?;
let cache_pool = redis::Client::open(config.cache_database.url())?; 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. 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" 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) 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#" r#"
CREATE TABLE IF NOT EXISTS users ( CREATE TABLE IF NOT EXISTS users (
uuid uuid PRIMARY KEY NOT NULL, uuid uuid PRIMARY KEY NOT NULL,
@ -150,7 +141,7 @@ async fn main() -> Result<(), Error> {
); );
"#, "#,
) )
.execute(&mut conn) .execute(&pool)
.await?; .await?;
/* /*

View file

@ -1,15 +1,14 @@
use std::str::FromStr; use std::str::FromStr;
use actix_web::HttpResponse; use actix_web::HttpResponse;
use diesel::Selectable;
use log::error; use log::error;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use sqlx::{Pool, Postgres, prelude::FromRow};
use uuid::Uuid; use uuid::Uuid;
use crate::{Conn, Data, tables::*}; use crate::Data;
#[derive(Serialize, Deserialize, Clone, Selectable)] #[derive(Serialize, Deserialize, Clone)]
#[diesel(table_name = channels)]
pub struct Channel { pub struct Channel {
pub uuid: Uuid, pub uuid: Uuid,
pub guild_uuid: Uuid, pub guild_uuid: Uuid,
@ -18,7 +17,7 @@ pub struct Channel {
pub permissions: Vec<ChannelPermission>, pub permissions: Vec<ChannelPermission>,
} }
#[derive(Serialize, Clone)] #[derive(Serialize, Clone, FromRow)]
struct ChannelPermissionBuilder { struct ChannelPermissionBuilder {
role_uuid: String, role_uuid: String,
permissions: i32, permissions: i32,
@ -33,8 +32,7 @@ impl ChannelPermissionBuilder {
} }
} }
#[derive(Serialize, Deserialize, Clone, Selectable)] #[derive(Serialize, Deserialize, Clone, FromRow)]
#[diesel(table_name = channel_permissions)]
pub struct ChannelPermission { pub struct ChannelPermission {
pub role_uuid: Uuid, pub role_uuid: Uuid,
pub permissions: i32, pub permissions: i32,
@ -42,10 +40,15 @@ pub struct ChannelPermission {
impl Channel { impl Channel {
pub async fn fetch_all( pub async fn fetch_all(
conn: &mut Conn, pool: &Pool<Postgres>,
guild_uuid: Uuid, guild_uuid: Uuid,
) -> Result<Vec<Self>, HttpResponse> { ) -> Result<Vec<Self>, 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 { if let Err(error) = row {
error!("{}", error); error!("{}", error);

View file

@ -1,109 +0,0 @@
use diesel::table;
table! {
users (uuid) {
uuid -> Uuid,
username -> VarChar,
display_name -> Nullable<VarChar>,
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,
}
}