Updated easy cargo versions
This commit is contained in:
46
src/lib.rs
46
src/lib.rs
@@ -1,6 +1,4 @@
|
||||
use actix_web::{ResponseError, HttpResponse};
|
||||
use diesel::result::Error as DieselError;
|
||||
use reqwest::StatusCode;
|
||||
use serde::{Serialize, Deserialize};
|
||||
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 {
|
||||
fn from(error: redis::RedisError) -> ServiceError {
|
||||
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 {
|
||||
fn from(error: uuid::Error) -> ServiceError {
|
||||
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))
|
||||
}
|
||||
}
|
||||
|
||||
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 redis::{Client as RedisClient, aio::Connection as RedisConnection};
|
||||
use s3::{Region, creds::Credentials, Bucket, BucketConfiguration, request::ResponseData};
|
||||
use siren::ServiceError;
|
||||
use crate::diesel_migrations::MigrationHarness;
|
||||
use lazy_static::lazy_static;
|
||||
@@ -38,37 +37,11 @@ lazy_static! {
|
||||
let url = format!("redis://{}:{}", host, port);
|
||||
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() {
|
||||
lazy_static::initialize(&POOL);
|
||||
lazy_static::initialize(&REDIS);
|
||||
lazy_static::initialize(&BUCKET);
|
||||
create_bucket().await;
|
||||
let mut pool: DbConnection = connection().expect("Failed to get db connection");
|
||||
match pool.run_pending_migrations(MIGRATIONS) {
|
||||
Ok(_) => info!("Database initialized"),
|
||||
@@ -91,43 +64,3 @@ pub async fn redis_async_connection() -> Result<RedisConnection, ServiceError> {
|
||||
let conn = REDIS.get_async_connection().await?;
|
||||
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