Skip to content

Commit

Permalink
fix: centralized storage
Browse files Browse the repository at this point in the history
  • Loading branch information
ChecksumDev committed Oct 17, 2023
1 parent 9ac81de commit a2bf524
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
6 changes: 5 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ use sqlx::{
sqlite::{SqliteConnectOptions, SqlitePoolOptions},
Pool, Sqlite,
};
use storage::Storage;

struct AppData {
pool: Pool<Sqlite>,
storage: Storage,
}

#[tokio::main]
Expand All @@ -30,8 +32,10 @@ async fn main() -> Result<()> {
.await
.unwrap();

let storage = Storage::new(String::from("data")).await;

sqlx::migrate!().run(&pool).await.unwrap();
let data = Data::new(AppData { pool });
let data = Data::new(AppData { pool, storage });

println!("Lumen is running on {}", std::env::var("HOST").unwrap());
HttpServer::new(move || {
Expand Down
7 changes: 2 additions & 5 deletions src/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use uuid::Uuid;
use crate::{
encryption::Encryption,
models::{File, User},
storage::Storage,
AppData,
};

Expand Down Expand Up @@ -70,8 +69,7 @@ async fn upload(bytes: Bytes, req: HttpRequest, data: Data<AppData>) -> impl Res
let encoded_key = general_purpose::URL_SAFE_NO_PAD.encode(encryption.key);
let encoded_nonce = general_purpose::URL_SAFE_NO_PAD.encode(encryption.nonce);

let storage = Storage::new(String::from("data")).await;
storage
data.storage
.save(String::from(&uuid), &encrypted_bytes)
.await
.unwrap();
Expand Down Expand Up @@ -136,8 +134,7 @@ async fn download(
.unwrap(),
};

let storage = Storage::new(String::from("data")).await;
let encrypted_bytes = storage.load(id).await.unwrap();
let encrypted_bytes = data.storage.load(id).await.unwrap();

let bytes = encryption.decrypt(&encrypted_bytes);

Expand Down

0 comments on commit a2bf524

Please sign in to comment.