Updated easy cargo versions
This commit is contained in:
54
Cargo.toml
54
Cargo.toml
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "service"
|
name = "service"
|
||||||
version = "0.2.7"
|
version = "0.2.8"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
authors = ["Ben Sherriff <hello@bensherriff.com>"]
|
authors = ["Ben Sherriff <hello@bensherriff.com>"]
|
||||||
repository = "https://github.com/bensherriff/siren"
|
repository = "https://github.com/bensherriff/siren"
|
||||||
@@ -12,46 +12,20 @@ name = "siren"
|
|||||||
path = "src/lib.rs"
|
path = "src/lib.rs"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
actix-web = "4.5.1"
|
|
||||||
actix-cors = "0.7.0"
|
|
||||||
actix-web-httpauth = "0.8.1"
|
|
||||||
chrono = { version = "0.4.37", features = ["serde"] }
|
|
||||||
dotenv = "0.15.0"
|
dotenv = "0.15.0"
|
||||||
serde_json = "1.0.115"
|
log = "0.4.22"
|
||||||
log = "0.4.21"
|
env_logger = "0.11.5"
|
||||||
env_logger = "0.11.3"
|
serde = { version = "1.0.209", features = ["derive"] }
|
||||||
|
serde_json = "1.0.127"
|
||||||
|
serenity = { version = "0.11.6", default-features = false, features = ["client", "gateway", "rustls_backend", "model", "voice", "cache", "framework", "standard_framework"] }
|
||||||
|
songbird = { version = "0.3.2", features = ["builtin-queue", "yt-dlp"] }
|
||||||
|
diesel = { version = "2.1.5", default-features = false, features = ["postgres", "chrono", "r2d2", "32-column-tables", "serde_json", "with-deprecated"] }
|
||||||
diesel_migrations = { version = "2.1.0", features = ["postgres"] }
|
diesel_migrations = { version = "2.1.0", features = ["postgres"] }
|
||||||
r2d2 = "0.8.10"
|
r2d2 = "0.8.10"
|
||||||
lazy_static = "1.4.0"
|
chrono = { version = "0.4.38", features = ["serde"] }
|
||||||
uuid = { version = "1.8.0", features = ["serde", "v4"] }
|
reqwest = { version = "0.12.7", default-features = false, features = ["json"] }
|
||||||
argon2 = "0.5.3"
|
lazy_static = "1.5.0"
|
||||||
redis = { version = "0.23.3", features = ["tokio-comp", "connection-manager", "r2d2"] }
|
uuid = { version = "1.10.0", features = ["serde", "v4"] }
|
||||||
rust-s3 = "0.33.0"
|
redis = { version = "0.26.1", features = ["tokio-comp", "connection-manager", "r2d2"] }
|
||||||
actix-multipart = "0.6.1"
|
|
||||||
rand = "0.8.5"
|
rand = "0.8.5"
|
||||||
rand_chacha = "0.3.1"
|
tokio = { version = "1.40.0", features = ["macros", "rt-multi-thread"] }
|
||||||
jsonwebtoken = "9.3.0"
|
|
||||||
tokio = { version = "1.37.0", features = ["macros", "rt-multi-thread"] }
|
|
||||||
|
|
||||||
[dependencies.serde]
|
|
||||||
version = "1.0.197"
|
|
||||||
features = ["derive"]
|
|
||||||
|
|
||||||
[dependencies.reqwest]
|
|
||||||
version = "0.11.22"
|
|
||||||
default-features = false
|
|
||||||
features = ["json", "rustls-tls"]
|
|
||||||
|
|
||||||
[dependencies.diesel]
|
|
||||||
version = "2.1.5"
|
|
||||||
default-features = false
|
|
||||||
features = ["postgres", "chrono", "32-column-tables", "serde_json", "r2d2", "with-deprecated"]
|
|
||||||
|
|
||||||
[dependencies.serenity]
|
|
||||||
version = "0.11.6"
|
|
||||||
default-features = false
|
|
||||||
features = ["client", "gateway", "rustls_backend", "model", "voice", "cache", "framework", "standard_framework"]
|
|
||||||
|
|
||||||
[dependencies.songbird]
|
|
||||||
version = "0.3.2"
|
|
||||||
features = ["builtin-queue", "yt-dlp"]
|
|
||||||
|
|||||||
46
src/lib.rs
46
src/lib.rs
@@ -1,6 +1,4 @@
|
|||||||
use actix_web::{ResponseError, HttpResponse};
|
|
||||||
use diesel::result::Error as DieselError;
|
use diesel::result::Error as DieselError;
|
||||||
use reqwest::StatusCode;
|
|
||||||
use serde::{Serialize, Deserialize};
|
use serde::{Serialize, Deserialize};
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
|
||||||
@@ -100,33 +98,12 @@ impl From<serenity::Error> for ServiceError {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<argon2::password_hash::Error> for ServiceError {
|
|
||||||
fn from(error: argon2::password_hash::Error) -> ServiceError {
|
|
||||||
ServiceError::new(500, format!("Unknown argon2 error: {}", error))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<redis::RedisError> for ServiceError {
|
impl From<redis::RedisError> for ServiceError {
|
||||||
fn from(error: redis::RedisError) -> ServiceError {
|
fn from(error: redis::RedisError) -> ServiceError {
|
||||||
ServiceError::new(500, format!("Unknown redis error: {}", error))
|
ServiceError::new(500, format!("Unknown redis error: {}", error))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<s3::error::S3Error> for ServiceError {
|
|
||||||
fn from(error: s3::error::S3Error) -> ServiceError {
|
|
||||||
match error {
|
|
||||||
s3::error::S3Error::Http(code, message) => ServiceError::new(code, message),
|
|
||||||
_ => ServiceError::new(500, format!("Unknown s3 error: {}", error)),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<s3::creds::error::CredentialsError> for ServiceError {
|
|
||||||
fn from(error: s3::creds::error::CredentialsError) -> ServiceError {
|
|
||||||
ServiceError::new(500, format!("Unknown credentials error: {}", error))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<uuid::Error> for ServiceError {
|
impl From<uuid::Error> for ServiceError {
|
||||||
fn from(error: uuid::Error) -> ServiceError {
|
fn from(error: uuid::Error) -> ServiceError {
|
||||||
ServiceError::new(500, format!("Unknown uuid error: {}", error))
|
ServiceError::new(500, format!("Unknown uuid error: {}", error))
|
||||||
@@ -138,26 +115,3 @@ impl From<std::env::VarError> for ServiceError {
|
|||||||
ServiceError::new(500, format!("Unknown env error: {}", error))
|
ServiceError::new(500, format!("Unknown env error: {}", error))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<jsonwebtoken::errors::Error> for ServiceError {
|
|
||||||
fn from(error: jsonwebtoken::errors::Error) -> ServiceError {
|
|
||||||
ServiceError::new(500, format!("Unknown jsonwebtoken error: {}", error))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ResponseError for ServiceError {
|
|
||||||
fn error_response(&self) -> HttpResponse {
|
|
||||||
let status_code = match StatusCode::from_u16(self.status) {
|
|
||||||
Ok(status_code) => status_code,
|
|
||||||
Err(_) => StatusCode::INTERNAL_SERVER_ERROR,
|
|
||||||
};
|
|
||||||
|
|
||||||
let error_message = match status_code.as_u16() < 500 {
|
|
||||||
true => self.message.clone(),
|
|
||||||
false => "Internal server error".to_string(),
|
|
||||||
};
|
|
||||||
|
|
||||||
HttpResponse::build(status_code)
|
|
||||||
.json(serde_json::json!({ "status": status_code.as_u16(), "message": error_message }))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
use diesel::{r2d2::ConnectionManager as DieselConnectionManager, PgConnection};
|
use diesel::{r2d2::ConnectionManager as DieselConnectionManager, PgConnection};
|
||||||
use redis::{Client as RedisClient, aio::Connection as RedisConnection};
|
use redis::{Client as RedisClient, aio::Connection as RedisConnection};
|
||||||
use s3::{Region, creds::Credentials, Bucket, BucketConfiguration, request::ResponseData};
|
|
||||||
use siren::ServiceError;
|
use siren::ServiceError;
|
||||||
use crate::diesel_migrations::MigrationHarness;
|
use crate::diesel_migrations::MigrationHarness;
|
||||||
use lazy_static::lazy_static;
|
use lazy_static::lazy_static;
|
||||||
@@ -38,37 +37,11 @@ lazy_static! {
|
|||||||
let url = format!("redis://{}:{}", host, port);
|
let url = format!("redis://{}:{}", host, port);
|
||||||
RedisClient::open(url).expect("Failed to create redis client")
|
RedisClient::open(url).expect("Failed to create redis client")
|
||||||
};
|
};
|
||||||
static ref BUCKET: Bucket = {
|
|
||||||
let url = env::var("MINIO_HOST").unwrap_or("localhost".to_string());
|
|
||||||
let port = env::var("MINIO_PORT").unwrap_or("9000".to_string());
|
|
||||||
let user = env::var("MINIO_ROOT_USER").expect("MINIO_ROOT_USER is not set");
|
|
||||||
let password = env::var("MINIO_ROOT_PASSWORD").expect("MINIO_ROOT_PASSWORD is not set");
|
|
||||||
let base_url = format!("http://{}:{}", url, port);
|
|
||||||
|
|
||||||
let region = Region::Custom {
|
|
||||||
region: "".to_string(),
|
|
||||||
endpoint: base_url,
|
|
||||||
};
|
|
||||||
|
|
||||||
let credentials = Credentials {
|
|
||||||
access_key: Some(user),
|
|
||||||
secret_key: Some(password),
|
|
||||||
security_token: None,
|
|
||||||
session_token: None,
|
|
||||||
expiration: None,
|
|
||||||
};
|
|
||||||
|
|
||||||
Bucket::new("siren", region.clone(), credentials.clone())
|
|
||||||
.expect("Failed to create S3 Bucket")
|
|
||||||
.with_path_style()
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn init() {
|
pub async fn init() {
|
||||||
lazy_static::initialize(&POOL);
|
lazy_static::initialize(&POOL);
|
||||||
lazy_static::initialize(&REDIS);
|
lazy_static::initialize(&REDIS);
|
||||||
lazy_static::initialize(&BUCKET);
|
|
||||||
create_bucket().await;
|
|
||||||
let mut pool: DbConnection = connection().expect("Failed to get db connection");
|
let mut pool: DbConnection = connection().expect("Failed to get db connection");
|
||||||
match pool.run_pending_migrations(MIGRATIONS) {
|
match pool.run_pending_migrations(MIGRATIONS) {
|
||||||
Ok(_) => info!("Database initialized"),
|
Ok(_) => info!("Database initialized"),
|
||||||
@@ -91,43 +64,3 @@ pub async fn redis_async_connection() -> Result<RedisConnection, ServiceError> {
|
|||||||
let conn = REDIS.get_async_connection().await?;
|
let conn = REDIS.get_async_connection().await?;
|
||||||
Ok(conn)
|
Ok(conn)
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn create_bucket() {
|
|
||||||
let url = env::var("MINIO_URL").unwrap_or("localhost".to_string());
|
|
||||||
let port = env::var("MINIO_PORT").unwrap_or("9000".to_string());
|
|
||||||
let user = env::var("MINIO_ROOT_USER").expect("MINIO_ROOT_USER is not set");
|
|
||||||
let password = env::var("MINIO_ROOT_PASSWORD").expect("MINIO_ROOT_PASSWORD is not set");
|
|
||||||
let base_url = format!("http://{}:{}", url, port);
|
|
||||||
|
|
||||||
let region = Region::Custom {
|
|
||||||
region: "".to_string(),
|
|
||||||
endpoint: base_url,
|
|
||||||
};
|
|
||||||
|
|
||||||
let credentials = Credentials {
|
|
||||||
access_key: Some(user),
|
|
||||||
secret_key: Some(password),
|
|
||||||
security_token: None,
|
|
||||||
session_token: None,
|
|
||||||
expiration: None,
|
|
||||||
};
|
|
||||||
let _ =
|
|
||||||
Bucket::create_with_path_style("siren", region, credentials, BucketConfiguration::default())
|
|
||||||
.await;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn upload_file(path: &str, content: &[u8]) -> Result<ResponseData, ServiceError> {
|
|
||||||
let response = BUCKET.put_object(path, content).await?;
|
|
||||||
Ok(response)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn get_file(path: &str) -> Result<Vec<u8>, ServiceError> {
|
|
||||||
let response = BUCKET.get_object(path).await?;
|
|
||||||
let bytes = response.bytes();
|
|
||||||
Ok(bytes.to_vec())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn delete_file(path: &str) -> Result<ResponseData, ServiceError> {
|
|
||||||
let response = BUCKET.delete_object(path).await?;
|
|
||||||
Ok(response)
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user