Merge branch 'main' into wip/images
This commit is contained in:
commit
149b81973d
54 changed files with 1201 additions and 1691 deletions
128
src/main.rs
128
src/main.rs
|
@ -2,17 +2,24 @@ use actix_cors::Cors;
|
|||
use actix_web::{App, HttpServer, web};
|
||||
use argon2::Argon2;
|
||||
use clap::Parser;
|
||||
use error::Error;
|
||||
use simple_logger::SimpleLogger;
|
||||
use sqlx::{PgPool, Pool, Postgres};
|
||||
use diesel_async::pooled_connection::AsyncDieselConnectionManager;
|
||||
use diesel_async::pooled_connection::deadpool::Pool;
|
||||
use std::time::SystemTime;
|
||||
mod config;
|
||||
use config::{Config, ConfigBuilder};
|
||||
mod api;
|
||||
use diesel_migrations::{embed_migrations, EmbeddedMigrations, MigrationHarness};
|
||||
|
||||
pub const MIGRATIONS: EmbeddedMigrations = embed_migrations!();
|
||||
|
||||
type Conn = deadpool::managed::Object<AsyncDieselConnectionManager<diesel_async::AsyncPgConnection>>;
|
||||
|
||||
mod api;
|
||||
pub mod structs;
|
||||
pub mod utils;
|
||||
|
||||
type Error = Box<dyn std::error::Error>;
|
||||
pub mod schema;
|
||||
pub mod error;
|
||||
|
||||
#[derive(Parser, Debug)]
|
||||
#[command(version, about, long_about = None)]
|
||||
|
@ -23,7 +30,7 @@ struct Args {
|
|||
|
||||
#[derive(Clone)]
|
||||
pub struct Data {
|
||||
pub pool: Pool<Postgres>,
|
||||
pub pool: deadpool::managed::Pool<AsyncDieselConnectionManager<diesel_async::AsyncPgConnection>, Conn>,
|
||||
pub cache_pool: redis::Client,
|
||||
pub config: Config,
|
||||
pub argon2: Argon2<'static>,
|
||||
|
@ -45,7 +52,9 @@ async fn main() -> Result<(), Error> {
|
|||
|
||||
let web = config.web.clone();
|
||||
|
||||
let pool = PgPool::connect_with(config.database.connect_options()).await?;
|
||||
// create a new connection pool with the default config
|
||||
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())?;
|
||||
|
||||
|
@ -53,103 +62,18 @@ async fn main() -> Result<(), Error> {
|
|||
|
||||
bunny_cdn.storage.init(config.bunny.endpoint.clone(), config.bunny.storage_zone.clone())?;
|
||||
|
||||
/*
|
||||
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"
|
||||
let database_url = config.database.url();
|
||||
|
||||
TODO: References to time should be removed in favor of using the timestamp built in to UUIDv7 (apart from deleted_at in users)
|
||||
*/
|
||||
sqlx::raw_sql(
|
||||
r#"
|
||||
CREATE TABLE IF NOT EXISTS users (
|
||||
uuid uuid PRIMARY KEY NOT NULL,
|
||||
username varchar(32) NOT NULL,
|
||||
display_name varchar(64) DEFAULT NULL,
|
||||
password varchar(512) NOT NULL,
|
||||
email varchar(100) NOT NULL,
|
||||
email_verified boolean NOT NULL DEFAULT FALSE,
|
||||
avatar varchar(100) DEFAULT NULL,
|
||||
is_deleted boolean NOT NULL DEFAULT FALSE,
|
||||
deleted_at int8 DEFAULT NULL,
|
||||
CONSTRAINT unique_username_active UNIQUE NULLS NOT DISTINCT (username, is_deleted),
|
||||
CONSTRAINT unique_email_active UNIQUE NULLS NOT DISTINCT (email, is_deleted)
|
||||
);
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS idx_unique_username_active
|
||||
ON users(username)
|
||||
WHERE is_deleted = FALSE;
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS idx_unique_email_active
|
||||
ON users(email)
|
||||
WHERE is_deleted = FALSE;
|
||||
CREATE TABLE IF NOT EXISTS instance_permissions (
|
||||
uuid uuid NOT NULL REFERENCES users(uuid),
|
||||
administrator boolean NOT NULL DEFAULT FALSE
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS refresh_tokens (
|
||||
token varchar(64) PRIMARY KEY UNIQUE NOT NULL,
|
||||
uuid uuid NOT NULL REFERENCES users(uuid),
|
||||
created_at int8 NOT NULL,
|
||||
device_name varchar(16) NOT NULL
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS access_tokens (
|
||||
token varchar(32) PRIMARY KEY UNIQUE NOT NULL,
|
||||
refresh_token varchar(64) UNIQUE NOT NULL REFERENCES refresh_tokens(token) ON UPDATE CASCADE ON DELETE CASCADE,
|
||||
uuid uuid NOT NULL REFERENCES users(uuid),
|
||||
created_at int8 NOT NULL
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS guilds (
|
||||
uuid uuid PRIMARY KEY NOT NULL,
|
||||
owner_uuid uuid NOT NULL REFERENCES users(uuid),
|
||||
name VARCHAR(100) NOT NULL,
|
||||
description VARCHAR(300),
|
||||
icon VARCHAR(100) DEFAULT NULL
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS guild_members (
|
||||
uuid uuid PRIMARY KEY NOT NULL,
|
||||
guild_uuid uuid NOT NULL REFERENCES guilds(uuid) ON DELETE CASCADE,
|
||||
user_uuid uuid NOT NULL REFERENCES users(uuid),
|
||||
nickname VARCHAR(100) DEFAULT NULL
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS roles (
|
||||
uuid uuid UNIQUE NOT NULL,
|
||||
guild_uuid uuid NOT NULL REFERENCES guilds(uuid) ON DELETE CASCADE,
|
||||
name VARCHAR(50) NOT NULL,
|
||||
color int NOT NULL DEFAULT 16777215,
|
||||
position int NOT NULL,
|
||||
permissions int8 NOT NULL DEFAULT 0,
|
||||
PRIMARY KEY (uuid, guild_uuid)
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS role_members (
|
||||
role_uuid uuid NOT NULL REFERENCES roles(uuid) ON DELETE CASCADE,
|
||||
member_uuid uuid NOT NULL REFERENCES guild_members(uuid) ON DELETE CASCADE,
|
||||
PRIMARY KEY (role_uuid, member_uuid)
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS channels (
|
||||
uuid uuid PRIMARY KEY NOT NULL,
|
||||
guild_uuid uuid NOT NULL REFERENCES guilds(uuid) ON DELETE CASCADE,
|
||||
name varchar(32) NOT NULL,
|
||||
description varchar(500) NOT NULL
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS channel_permissions (
|
||||
channel_uuid uuid NOT NULL REFERENCES channels(uuid) ON DELETE CASCADE,
|
||||
role_uuid uuid NOT NULL REFERENCES roles(uuid) ON DELETE CASCADE,
|
||||
permissions int8 NOT NULL DEFAULT 0,
|
||||
PRIMARY KEY (channel_uuid, role_uuid)
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS messages (
|
||||
uuid uuid PRIMARY KEY NOT NULL,
|
||||
channel_uuid uuid NOT NULL REFERENCES channels(uuid) ON DELETE CASCADE,
|
||||
user_uuid uuid NOT NULL REFERENCES users(uuid),
|
||||
message varchar(4000) NOT NULL
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS invites (
|
||||
id varchar(32) PRIMARY KEY NOT NULL,
|
||||
guild_uuid uuid NOT NULL REFERENCES guilds(uuid) ON DELETE CASCADE,
|
||||
user_uuid uuid NOT NULL REFERENCES users(uuid)
|
||||
);
|
||||
"#,
|
||||
)
|
||||
.execute(&pool)
|
||||
.await?;
|
||||
tokio::task::spawn_blocking(move || {
|
||||
use diesel::prelude::Connection;
|
||||
use diesel_async::async_connection_wrapper::AsyncConnectionWrapper;
|
||||
|
||||
|
||||
let mut conn = AsyncConnectionWrapper::<diesel_async::AsyncPgConnection>::establish(&database_url)?;
|
||||
|
||||
conn.run_pending_migrations(MIGRATIONS)?;
|
||||
Ok::<_, Box<dyn std::error::Error + Send + Sync>>(())
|
||||
}).await?.unwrap();
|
||||
|
||||
/*
|
||||
**Stored for later possible use**
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue