Skip to content

Commit

Permalink
Refactor storage library
Browse files Browse the repository at this point in the history
- Switch to SQLx
- Cleanup code
- Fix bugs
- Support custom database port
  • Loading branch information
TobiasDeBruijn committed Oct 9, 2023
1 parent 3c50504 commit 2c12eac
Show file tree
Hide file tree
Showing 19 changed files with 783 additions and 824 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ sqlSettings:
database: 'skinfixer_database'
username: 'skinfixer_user'
password: 'super secure password herer'
port: YOUR_DATABASE_PORT

# Should SkinFixer itegrate with Discord, if this is set to true you will need to configure discordSettigns
useDiscord: false
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ dependencies {
compileOnly 'org.spigotmc:spigot-api:1.17-R0.1-SNAPSHOT'

implementation 'dev.array21:httplib:1.2.2'
implementation 'dev.array21:classvalidator:1.0.0'
implementation 'dev.array21:classvalidator:1.0.1'
implementation 'dev.array21:bukkit-reflection-util:1.3.1'

// JDA and related dependencies
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
pluginVersion = 1.7.9
pluginVersion = 1.7.10
27 changes: 6 additions & 21 deletions lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "skinfixer"
version = "0.1.0"
authors = ["Tobias de Bruijn <[email protected]>"]
edition = "2018"
edition = "2021"

[lib]
name = "skinfixer"
Expand All @@ -14,26 +14,11 @@ jni = "0.19.0"
postgres = "0.19.1"
bincode = "1.3.3"
lazy_static = "1.4.0"

[dependencies.mysql]
version = "23.0.0"
default-features = false

[dependencies.flate2]
version = "1.0.22"
features = ["zlib"]

[dependencies.rusqlite]
version = "0.25.1"
features = ["bundled"]

[dependencies.serde]
version = "1.0.126"
features = ["derive"]

[dependencies.typenum]
version = "1.14.0"
features = ["no_std"]
sqlx = { version = "0.7.2", features = ["any", "mysql", "postgres", "sqlite", "runtime-tokio-rustls"] }
serde = { version = "1.0.188", features = ["derive"] }
typenum = { version = "1.17.0", features = ["no_std"] }
tokio = { version = "1.33.0", features = ["fs", "rt", "rt-multi-thread"] }
thiserror = "1.0.49"

[profile.release]
lto = true
Expand Down
160 changes: 0 additions & 160 deletions lib/src/config.rs

This file was deleted.

132 changes: 132 additions & 0 deletions lib/src/database/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
use crate::database::profile::BinProfile;
use serde::de::DeserializeOwned;
use serde::Serialize;
use sqlx::mysql::MySqlConnectOptions;
use sqlx::postgres::PgConnectOptions;
use sqlx::sqlite::SqliteConnectOptions;
use sqlx::{MySqlPool, PgPool, SqlitePool};
use std::path::{Path, PathBuf};
use thiserror::Error;
use tokio::fs;
use tokio::io::{AsyncReadExt, AsyncWriteExt};

mod profile;
pub use profile::*;

#[derive(Debug, Error)]
pub enum DatabaseError {
#[error("{0}")]
Sql(#[from] sqlx::Error),
#[error("{0}")]
Bin(#[from] BinError),
}

#[derive(Debug)]
pub enum Driver {
Mysql(MySqlPool),
Postgres(PgPool),
Sqlite(SqlitePool),
Bin(PathBuf),
}

pub enum DriverType<'a> {
Mysql(DatabaseOptions<'a>),
Postgres(DatabaseOptions<'a>),
Sqlite(PathBuf),
Bin(PathBuf),
}

pub struct DatabaseOptions<'a> {
pub name: &'a str,
pub user: &'a str,
pub passw: &'a str,
pub host: &'a str,
pub port: u16,
}

impl Driver {
pub async fn new(driver_type: DriverType<'_>) -> Result<Self, DatabaseError> {
let driver = match driver_type {
DriverType::Mysql(c) => Self::new_mysql(c).await?,
DriverType::Postgres(c) => Self::new_postgres(c).await?,
DriverType::Sqlite(c) => Self::new_sqlite(&c).await?,
DriverType::Bin(c) => Self::new_bin(c).await?,
};

Ok(driver)
}

async fn new_mysql(options: DatabaseOptions<'_>) -> Result<Self, sqlx::Error> {
let opts = MySqlConnectOptions::new()
.database(options.name)
.username(options.user)
.password(options.passw)
.host(options.host)
.port(options.port);

let pool = MySqlPool::connect_with(opts).await?;
Ok(Self::Mysql(pool))
}

async fn new_postgres(options: DatabaseOptions<'_>) -> Result<Self, sqlx::Error> {
let opts = PgConnectOptions::new()
.database(options.name)
.username(options.user)
.password(options.passw)
.host(options.host)
.port(options.port);

let pool = PgPool::connect_with(opts).await?;
Ok(Self::Postgres(pool))
}

async fn new_sqlite(path: &Path) -> Result<Self, sqlx::Error> {
let opts = SqliteConnectOptions::new().filename(path);

let pool = SqlitePool::connect_with(opts).await?;
Ok(Self::Sqlite(pool))
}

async fn new_bin(path: PathBuf) -> Result<Self, BinError> {
if !path.exists() {
if let Some(parent) = path.parent() {
fs::create_dir_all(parent).await?;
}

BinFile::write::<Vec<BinProfile>>(&path, &vec![]).await?;
}

Ok(Self::Bin(path))
}
}

#[derive(Debug, Error)]
pub enum BinError {
#[error("IO Error: {0}")]
Io(#[from] std::io::Error),
#[error("Binary (de)serialization error: {0}")]
Bin(#[from] bincode::Error),
}

pub struct BinFile;

impl BinFile {
async fn read<T: DeserializeOwned>(path: &Path) -> Result<T, BinError> {
let mut file = fs::File::open(path).await?;
let mut buf = Vec::new();
file.read_to_end(&mut buf).await?;

let deserialized: T = bincode::deserialize(&buf)?;

Ok(deserialized)
}

async fn write<T: Serialize>(path: &Path, content: &T) -> Result<(), BinError> {
let mut file = fs::File::create(path).await?;
let buf = bincode::serialize(content)?;

file.write_all(&buf).await?;

Ok(())
}
}
Loading

0 comments on commit 2c12eac

Please sign in to comment.