Add all connectors
This commit is contained in:
38
moneymgr_backend/src/connections/db_connection.rs
Normal file
38
moneymgr_backend/src/connections/db_connection.rs
Normal file
@@ -0,0 +1,38 @@
|
||||
use crate::app_config::AppConfig;
|
||||
use diesel::PgConnection;
|
||||
use diesel::r2d2::{ConnectionManager, Pool, PooledConnection};
|
||||
use diesel_migrations::{EmbeddedMigrations, MigrationHarness, embed_migrations};
|
||||
use lazy_static::lazy_static;
|
||||
use std::sync::Arc;
|
||||
|
||||
const MIGRATIONS: EmbeddedMigrations = embed_migrations!();
|
||||
|
||||
pub type DBConn = Arc<Pool<ConnectionManager<PgConnection>>>;
|
||||
|
||||
fn get_db_connection_pool() -> anyhow::Result<DBConn> {
|
||||
log::info!("Connect to PostgresSQL database...");
|
||||
let url = AppConfig::get().db_connection_chain();
|
||||
let manager = ConnectionManager::<PgConnection>::new(url);
|
||||
// Refer to the `r2d2` documentation for more methods to use
|
||||
// when building a connection pool
|
||||
Ok(Arc::new(
|
||||
Pool::builder().test_on_check_out(true).build(manager)?,
|
||||
))
|
||||
}
|
||||
|
||||
lazy_static! {
|
||||
static ref DB_POOL: DBConn = get_db_connection_pool().expect("Failed to connect to database");
|
||||
}
|
||||
|
||||
pub fn db() -> anyhow::Result<PooledConnection<ConnectionManager<PgConnection>>> {
|
||||
Ok(DB_POOL.clone().get()?)
|
||||
}
|
||||
|
||||
pub fn initialize_conn() -> anyhow::Result<()> {
|
||||
// Run pending diesel migrations
|
||||
let mut db = db()?;
|
||||
db.run_pending_migrations(MIGRATIONS)
|
||||
.expect("Failed to run DB migrations");
|
||||
|
||||
Ok(())
|
||||
}
|
2
moneymgr_backend/src/connections/mod.rs
Normal file
2
moneymgr_backend/src/connections/mod.rs
Normal file
@@ -0,0 +1,2 @@
|
||||
pub mod db_connection;
|
||||
pub mod s3_connection;
|
72
moneymgr_backend/src/connections/s3_connection.rs
Normal file
72
moneymgr_backend/src/connections/s3_connection.rs
Normal file
@@ -0,0 +1,72 @@
|
||||
use crate::app_config::AppConfig;
|
||||
use s3::error::S3Error;
|
||||
use s3::{Bucket, BucketConfiguration};
|
||||
|
||||
#[derive(thiserror::Error, Debug)]
|
||||
enum BucketServiceError {
|
||||
#[error("Failed to fetch bucket information!")]
|
||||
FailedFetchBucketInfo,
|
||||
}
|
||||
|
||||
/// Create S3 bucket if required
|
||||
pub async fn create_bucket_if_required() -> anyhow::Result<()> {
|
||||
if AppConfig::get().s3_skip_auto_create_bucket {
|
||||
log::debug!("Skipping bucket existence check");
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let bucket = AppConfig::get().s3_bucket()?;
|
||||
|
||||
match bucket.location().await {
|
||||
Ok(_) => {
|
||||
log::debug!("The bucket already exists.");
|
||||
return Ok(());
|
||||
}
|
||||
Err(S3Error::HttpFailWithBody(404, s)) if s.contains("<Code>NoSuchKey</Code>") => {
|
||||
log::warn!("Failed to fetch bucket location, but it seems that bucket exists.");
|
||||
return Ok(());
|
||||
}
|
||||
Err(S3Error::HttpFailWithBody(404, s)) if s.contains("<Code>NoSuchBucket</Code>") => {
|
||||
log::warn!("The bucket does not seem to exists, trying to create it!")
|
||||
}
|
||||
Err(e) => {
|
||||
log::error!("Got unexpected error when querying bucket info: {}", e);
|
||||
return Err(BucketServiceError::FailedFetchBucketInfo.into());
|
||||
}
|
||||
}
|
||||
|
||||
Bucket::create_with_path_style(
|
||||
&bucket.name,
|
||||
bucket.region,
|
||||
AppConfig::get().s3_credentials()?,
|
||||
BucketConfiguration::private(),
|
||||
)
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Upload a new file to the bucket
|
||||
pub async fn upload_file(path: &str, content: &[u8]) -> anyhow::Result<()> {
|
||||
let bucket = AppConfig::get().s3_bucket()?;
|
||||
|
||||
bucket.put_object(path, content).await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Get a file
|
||||
pub async fn get_file(path: &str) -> anyhow::Result<Vec<u8>> {
|
||||
let bucket = AppConfig::get().s3_bucket()?;
|
||||
|
||||
Ok(bucket.get_object(path).await?.to_vec())
|
||||
}
|
||||
|
||||
/// Delete a file, if it exists
|
||||
pub async fn delete_file_if_exists(path: &str) -> anyhow::Result<()> {
|
||||
let bucket = AppConfig::get().s3_bucket()?;
|
||||
|
||||
bucket.delete_object(path).await?;
|
||||
|
||||
Ok(())
|
||||
}
|
Reference in New Issue
Block a user