Skip to content

Commit

Permalink
Backend tweaks (#106)
Browse files Browse the repository at this point in the history
1. upgrade dependencies.
2. add index to db.
  • Loading branch information
CaviarChen authored Sep 14, 2024
1 parent 1fae2f2 commit 4f09674
Show file tree
Hide file tree
Showing 12 changed files with 1,965 additions and 899 deletions.
2,785 changes: 1,908 additions & 877 deletions server/Cargo.lock

Large diffs are not rendered by default.

20 changes: 10 additions & 10 deletions server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ edition = "2021"
members = [".", "entity", "migration"]

[dependencies]
tokio = { version = "1.19.2", features = ["full"] }
tokio = { version = "1.40.0", features = ["full"] }
serde = "1.0"
serde_json = "1.0"
reqwest = { version = "0.11", features = ["json"] }
rocket = { version = "0.5.0", features = ["json"] }
envconfig = "0.10"
reqwest = { version = "0.12", features = ["json"] }
rocket = { version = "0.5.1", features = ["json"] }
envconfig = "0.11"
dotenv = "0.15"
anyhow = "1.0"
entity = { path = "entity" }
Expand All @@ -25,14 +25,14 @@ hmac = "0.12"
sha2 = "0.10"
jwt = "0.16"
rand = "0.8.5"
email_address = "0.2.1"
email_address = "0.2.9"
rocket_cors = "0.6.0"
base64 = "0.13.0"
byte-unit = "4.0.14"
tempfile = "3.3.0"
base64 = "0.22.1"
byte-unit = "5.1.4"
tempfile = "3.12.0"
md5 = "0.7.0"
lazy_static = "1.4.0"
zip = "0.6.2"
lazy_static = "1.5.0"
zip = "2.2.0"
endorphin = "0.1.9"
log = "0.4"
sea-orm-rocket = "0.5.4"
2 changes: 1 addition & 1 deletion server/entity/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ path = "src/lib.rs"
[dependencies]
serde = "1.0"
serde_json = "1.0"
sea-orm = { version = "^0.9.2", features = [ "sqlx-postgres", "runtime-tokio-native-tls", "macros" ] }
sea-orm = { version = "^1.0.1", features = [ "sqlx-postgres", "runtime-tokio-native-tls", "macros" ] }
2 changes: 1 addition & 1 deletion server/entity/src/snapshot.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use sea_orm::entity::prelude::*;
use sea_orm::{entity::prelude::*, FromJsonQueryResult};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

Expand Down
2 changes: 1 addition & 1 deletion server/entity/src/snapshot_task.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use sea_orm::entity::prelude::*;
use sea_orm::{entity::prelude::*, FromJsonQueryResult};
use serde::{Deserialize, Serialize};

// NOTE: sea_orm doesn't seem to support `u16`
Expand Down
2 changes: 1 addition & 1 deletion server/migration/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ async-std = { version = "^1", features = ["attributes", "tokio1"] }
entity = { path = "../entity" }

[dependencies.sea-orm-migration]
version = "^0.9.0"
version = "^1.0.1"
features = [
"sqlx-postgres", "runtime-tokio-native-tls"
]
6 changes: 5 additions & 1 deletion server/migration/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
pub use sea_orm_migration::prelude::*;

mod m20220101_000001_create_table;
mod m20240914_025248_add_index;

pub struct Migrator;

#[async_trait::async_trait]
impl MigratorTrait for Migrator {
fn migrations() -> Vec<Box<dyn MigrationTrait>> {
vec![Box::new(m20220101_000001_create_table::Migration)]
vec![
Box::new(m20220101_000001_create_table::Migration),
Box::new(m20240914_025248_add_index::Migration),
]
}
}
31 changes: 31 additions & 0 deletions server/migration/src/m20240914_025248_add_index.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use sea_orm_migration::{
prelude::*,
sea_orm::{DbBackend, Schema},
};

#[derive(DeriveMigrationName)]
pub struct Migration;

#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
let schema = Schema::new(DbBackend::Postgres);
for stmt in schema.create_index_from_entity(entity::user::Entity) {
manager.create_index(stmt).await?;
}
for stmt in schema.create_index_from_entity(entity::snapshot::Entity) {
manager.create_index(stmt).await?;
}
for stmt in schema.create_index_from_entity(entity::snapshot_log::Entity) {
manager.create_index(stmt).await?;
}
for stmt in schema.create_index_from_entity(entity::snapshot_task::Entity) {
manager.create_index(stmt).await?;
}
Ok(())
}

async fn down(&self, _manager: &SchemaManager) -> Result<(), DbErr> {
Ok(())
}
}
3 changes: 2 additions & 1 deletion server/src/data_fetcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::file_storage;
use crate::limit;
use crate::user_handler::User;
use anyhow::Error;
use base64::Engine;
use chrono::prelude::*;
use chrono::Duration;
use entity::snapshot::SyncFiles;
Expand Down Expand Up @@ -102,7 +103,7 @@ pub enum ValidationError {
fn onedrive_api_of_link(url: &str) -> String {
format!(
"https://api.onedrive.com/v1.0/shares/u!{}",
base64::encode_config(url, base64::URL_SAFE_NO_PAD)
base64::engine::general_purpose::URL_SAFE_NO_PAD.encode(url)
)
}

Expand Down
4 changes: 2 additions & 2 deletions server/src/file_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use tempfile::TempDir;
// TODO: unit tests

pub fn byte_unit_to_string_hum(bytes: u64) -> String {
byte_unit::Byte::from_bytes(bytes.into())
.get_appropriate_unit(true)
byte_unit::Byte::from_u64(bytes)
.get_appropriate_unit(byte_unit::UnitType::Decimal)
.to_string()
}

Expand Down
3 changes: 1 addition & 2 deletions server/src/misc_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ async fn download<'r>(
};
let mut buf = Vec::new();
let mut zip = zip::ZipWriter::new(std::io::Cursor::new(&mut buf));
let options = zip::write::FileOptions::default()
let options = zip::write::SimpleFileOptions::default()
.compression_method(zip::CompressionMethod::Stored);
zip.add_directory("Sync/", options)?;

Expand All @@ -104,7 +104,6 @@ async fn download<'r>(
std::io::copy(&mut file, &mut zip)?;
}
zip.finish()?;
drop(zip);

Ok(FileResponse::Ok {
filename: String::from("Sync.zip"),
Expand Down
4 changes: 2 additions & 2 deletions server/src/snapshot_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ struct SnapshotJson {
async fn list_snapshots(
conn: Connection<'_, Db>,
user: User,
page: Option<usize>,
page_size: Option<usize>,
page: Option<u64>,
page_size: Option<u64>,
) -> APIResponse {
let db = conn.into_inner();

Expand Down

0 comments on commit 4f09674

Please sign in to comment.