From 36fba1dd1da3741c05e427f5055ab7810677599e Mon Sep 17 00:00:00 2001 From: MedzikUser Date: Sat, 23 Apr 2022 20:44:02 +0200 Subject: [PATCH] chore (database): move `utils/database` to `database` --- Cargo.lock | 150 ++++++++++++++++-- Cargo.toml | 2 +- core/Cargo.toml | 24 +-- core/src/main.rs | 2 +- database/Cargo.toml | 15 ++ {utils/src/database => database/src}/error.rs | 0 .../database/mod.rs => database/src/lib.rs | 0 .../src/database => database/src}/sqlite.rs | 6 +- {utils/src/database => database/src}/user.rs | 12 +- server/Cargo.toml | 30 +--- server/src/auth/login.rs | 4 +- server/src/auth/register.rs | 4 +- server/src/error.rs | 2 +- server/src/lib.rs | 3 +- types/Cargo.toml | 23 +-- utils/Cargo.toml | 33 ---- utils/src/lib.rs | 2 - 17 files changed, 185 insertions(+), 127 deletions(-) create mode 100644 database/Cargo.toml rename {utils/src/database => database/src}/error.rs (100%) rename utils/src/database/mod.rs => database/src/lib.rs (100%) rename {utils/src/database => database/src}/sqlite.rs (96%) rename {utils/src/database => database/src}/user.rs (79%) delete mode 100644 utils/Cargo.toml delete mode 100644 utils/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index eb0a8e3..4298345 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -28,12 +28,41 @@ dependencies = [ "version_check", ] +[[package]] +name = "alloc-no-stdlib" +version = "2.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "35ef4730490ad1c4eae5c4325b2a95f521d023e5c885853ff7aca0a6a1631db3" + +[[package]] +name = "alloc-stdlib" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "697ed7edc0f1711de49ce108c541623a0af97c6c60b2f6e2b65229847ac843c2" +dependencies = [ + "alloc-no-stdlib", +] + [[package]] name = "anyhow" version = "1.0.57" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "08f9b8508dccb7687a1d6c4ce66b2b0ecef467c94667de27d8d7fe1f8d2a9cdc" +[[package]] +name = "async-compression" +version = "0.3.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f2bf394cfbbe876f0ac67b13b6ca819f9c9f2fb9ec67223cceb1555fbab1c31a" +dependencies = [ + "brotli", + "flate2", + "futures-core", + "memchr", + "pin-project-lite", + "tokio", +] + [[package]] name = "async-trait" version = "0.1.53" @@ -115,7 +144,7 @@ dependencies = [ "cc", "cfg-if", "libc", - "miniz_oxide", + "miniz_oxide 0.4.4", "object", "rustc-demangle", ] @@ -151,6 +180,27 @@ dependencies = [ "generic-array", ] +[[package]] +name = "brotli" +version = "3.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1a0b1dbcc8ae29329621f8d4f0d835787c1c38bb1401979b49d13b0b305ff68" +dependencies = [ + "alloc-no-stdlib", + "alloc-stdlib", + "brotli-decompressor", +] + +[[package]] +name = "brotli-decompressor" +version = "2.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "59ad2d4653bf5ca36ae797b1f4bb4dbddb60ce49ca4aed8a2ce4829f60425b80" +dependencies = [ + "alloc-no-stdlib", + "alloc-stdlib", +] + [[package]] name = "bumpalo" version = "3.9.1" @@ -231,6 +281,15 @@ version = "1.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ccaeedb56da03b09f598226e25e80088cb4cd25f316e6e4df7d695f0feeb1403" +[[package]] +name = "crc32fast" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b540bd8bc810d3885c6ea91e2018302f68baba2129ab3e88f32389ee9370880d" +dependencies = [ + "cfg-if", +] + [[package]] name = "crossbeam-queue" version = "0.3.5" @@ -315,6 +374,18 @@ version = "2.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "77f3309417938f28bf8228fcff79a4a37103981e3e186d2ccd19c74b38f4eb71" +[[package]] +name = "flate2" +version = "1.0.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b39522e96686d38f4bc984b9198e3a0613264abaebaff2c5c918bfa6b6da09af" +dependencies = [ + "cfg-if", + "crc32fast", + "libc", + "miniz_oxide 0.5.1", +] + [[package]] name = "flume" version = "0.10.12" @@ -512,21 +583,34 @@ name = "homedisk-core" version = "0.0.0" dependencies = [ "better-panic", + "homedisk-database", "homedisk-server", "homedisk-types", - "homedisk-utils", "log", "simplelog", "tokio", ] +[[package]] +name = "homedisk-database" +version = "0.0.0" +dependencies = [ + "futures-util", + "log", + "rust_utilities", + "serde", + "sqlx", + "tokio", + "uuid", +] + [[package]] name = "homedisk-server" version = "0.0.0" dependencies = [ "axum", + "homedisk-database", "homedisk-types", - "homedisk-utils", "hyper", "log", "rust_utilities", @@ -549,19 +633,6 @@ dependencies = [ "zeroize", ] -[[package]] -name = "homedisk-utils" -version = "0.0.0" -dependencies = [ - "futures-util", - "log", - "rust_utilities", - "serde", - "sqlx", - "tokio", - "uuid", -] - [[package]] name = "http" version = "0.2.6" @@ -656,6 +727,15 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "iri-string" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f0f7638c1e223529f1bfdc48c8b133b9e0b434094d1d28473161ee48b235f78" +dependencies = [ + "nom", +] + [[package]] name = "itertools" version = "0.10.3" @@ -760,6 +840,16 @@ version = "0.3.16" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2a60c7ce501c71e03a9c9c0d35b861413ae925bd979cc7a4e30d060069aaac8d" +[[package]] +name = "mime_guess" +version = "2.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4192263c238a5f0d0c6bfd21f336a313a4ce1c450542449ca191bb657b4642ef" +dependencies = [ + "mime", + "unicase", +] + [[package]] name = "minimal-lexical" version = "0.2.1" @@ -776,6 +866,15 @@ dependencies = [ "autocfg", ] +[[package]] +name = "miniz_oxide" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2b29bd4bc3f33391105ebee3589c19197c4271e3e5a9ec9bfe8127eeff8f082" +dependencies = [ + "adler", +] + [[package]] name = "mio" version = "0.8.2" @@ -1581,6 +1680,8 @@ version = "0.2.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "aba3f3efabf7fb41fae8534fc20a817013dd1c12cb45441efb6c82e6556b4cd8" dependencies = [ + "async-compression", + "base64", "bitflags", "bytes", "futures-core", @@ -1588,10 +1689,18 @@ dependencies = [ "http", "http-body", "http-range-header", + "httpdate", + "iri-string", + "mime", + "mime_guess", + "percent-encoding", "pin-project-lite", + "tokio", + "tokio-util", "tower", "tower-layer", "tower-service", + "tracing", ] [[package]] @@ -1651,6 +1760,15 @@ version = "1.15.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dcf81ac59edc17cc8697ff311e8f5ef2d99fcbd9817b34cec66f90b6c3dfd987" +[[package]] +name = "unicase" +version = "2.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "50f37be617794602aabbeee0be4f259dc1778fabe05e2d67ee8f79326d5cb4f6" +dependencies = [ + "version_check", +] + [[package]] name = "unicode-bidi" version = "0.3.7" diff --git a/Cargo.toml b/Cargo.toml index b56579b..df23107 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,8 +1,8 @@ [workspace] members = [ "core", + "database", "server", "types", - "utils", ] resolver = "2" diff --git a/core/Cargo.toml b/core/Cargo.toml index b49019a..7dc097f 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -16,22 +16,8 @@ path = "./src/main.rs" [dependencies] better-panic = "0.3.0" simplelog = "0.12.0" - -[dependencies.tokio] -version = "1.17.0" -features = ["full"] - -[dependencies.log] -version = "0.4.16" -features = ["max_level_debug", "release_max_level_warn"] - -[dependencies.homedisk-utils] -path = "../utils" -features = ["full"] - -[dependencies.homedisk-server] -path = "../server" - -[dependencies.homedisk-types] -path = "../types" -features = ["config"] +tokio = { version = "1.17.0", features = ["full"] } +log = { version = "0.4.16", features = ["max_level_debug", "release_max_level_warn"] } +homedisk-database = { path = "../database" } +homedisk-server = { path = "../server" } +homedisk-types = { path = "../types", features = ["config"] } diff --git a/core/src/main.rs b/core/src/main.rs index 16a09f9..0dbcb52 100644 --- a/core/src/main.rs +++ b/core/src/main.rs @@ -1,7 +1,7 @@ mod init; +use homedisk_database::Database; use homedisk_types::config::types::Config; -use homedisk_utils::database::Database; #[tokio::main] async fn main() { diff --git a/database/Cargo.toml b/database/Cargo.toml new file mode 100644 index 0000000..2cbc91b --- /dev/null +++ b/database/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "homedisk-database" +version = "0.0.0" +edition = "2021" + +[dependencies] +log = "0.4.16" +futures-util = "0.3.21" +serde = { version = "1.0.136", features = ["derive"] } +sqlx = { version = "0.5.13", features = ["runtime-tokio-rustls", "sqlite"] } +uuid = { version = "1.0.0", features = ["v5"] } +rust_utilities = { version = "0.2.0", features = ["sha"] } + +[dev-dependencies] +tokio = { version = "1.17.0", features = ["full"] } diff --git a/utils/src/database/error.rs b/database/src/error.rs similarity index 100% rename from utils/src/database/error.rs rename to database/src/error.rs diff --git a/utils/src/database/mod.rs b/database/src/lib.rs similarity index 100% rename from utils/src/database/mod.rs rename to database/src/lib.rs diff --git a/utils/src/database/sqlite.rs b/database/src/sqlite.rs similarity index 96% rename from utils/src/database/sqlite.rs rename to database/src/sqlite.rs index fdb6e9a..796058a 100644 --- a/utils/src/database/sqlite.rs +++ b/database/src/sqlite.rs @@ -13,7 +13,7 @@ pub struct Database { impl Database { /// Open SQLite Database file /// ```ignore - /// use homedisk_utils::database::Database; + /// use homedisk_database::Database; /// /// Database::open("sqlite::memory:").await?; /// ``` @@ -27,7 +27,7 @@ impl Database { /// Create new User /// ```ignore - /// use homedisk_utils::database::{Database, User}; + /// use homedisk_database::{Database, User}; /// /// let user = User::new("username", "password"); /// db.create_user(&user).await?; @@ -74,7 +74,7 @@ mod tests { use sqlx::Executor; - use crate::database::{Database, User}; + use crate::{Database, User}; async fn open_db() -> Database { Database::open("sqlite::memory:").await.expect("open db") diff --git a/utils/src/database/user.rs b/database/src/user.rs similarity index 79% rename from utils/src/database/user.rs rename to database/src/user.rs index 3aeb40e..72e1f82 100644 --- a/utils/src/database/user.rs +++ b/database/src/user.rs @@ -1,4 +1,4 @@ -use rust_utilities::crypto::sha::{CryptographicHash, encode, Algorithm}; +use rust_utilities::crypto::sha::{encode, Algorithm, CryptographicHash}; use uuid::Uuid; #[derive(Debug, sqlx::FromRow)] @@ -14,7 +14,7 @@ impl User { /// This function creates a unique UUID for a user and creates a password hash using SHA-512 /// and returns in the User type /// ``` - /// use homedisk_utils::database::User; + /// use homedisk_database::User; /// /// let user = User::new("medzik", "SuperSecretPassword123!"); /// ``` @@ -26,7 +26,11 @@ impl User { let sha1_name = CryptographicHash::hash(Algorithm::SHA1, username.as_bytes()); let id = Uuid::new_v5(&Uuid::NAMESPACE_X500, &sha1_name).to_string(); - let password = encode(CryptographicHash::hash(Algorithm::SHA512, password.as_bytes())); + // hash password using SHA-512 + let password = encode(CryptographicHash::hash( + Algorithm::SHA512, + password.as_bytes(), + )); Self { id, @@ -48,7 +52,7 @@ mod tests { } #[test] - fn check_password_is_hashed() { + fn check_if_password_is_hashed() { let password = "password"; let user = User::new("test", password); diff --git a/server/Cargo.toml b/server/Cargo.toml index 403bf58..282d7ca 100644 --- a/server/Cargo.toml +++ b/server/Cargo.toml @@ -7,27 +7,9 @@ edition = "2021" axum = "0.5.3" log = "0.4.16" thiserror = "1.0.30" - -[dependencies.tower-http] -version = "0.2.5" -features = ["cors"] - -[dependencies.hyper] -version = "0.14.18" -features = ["full"] - -[dependencies.serde] -version = "1.0.136" -features = ["derive"] - -[dependencies.rust_utilities] -version = "0.2.0" -features = ["jsonwebtoken"] - -[dependencies.homedisk-utils] -path = "../utils" -features = ["database"] - -[dependencies.homedisk-types] -path = "../types" -features = ["axum"] +serde = { version = "1.0.136", features = ["derive"] } +tower-http = { version = "0.2.5", features = ["full"] } +hyper = { version = "0.14.18", features = ["full"] } +rust_utilities = { version = "0.2.0", features = ["jsonwebtoken"] } +homedisk-database = { path = "../database" } +homedisk-types = { path = "../types", features = ["axum"] } diff --git a/server/src/auth/login.rs b/server/src/auth/login.rs index ba1db25..2b32002 100644 --- a/server/src/auth/login.rs +++ b/server/src/auth/login.rs @@ -1,16 +1,16 @@ use axum::{Extension, Json}; +use homedisk_database::{Database, Error, User}; use homedisk_types::{ auth::login::{Request, Response}, config::types::Config, errors::{AuthError, ServerError}, }; -use homedisk_utils::database::{Database, Error, User}; use rust_utilities::crypto::jsonwebtoken::{Claims, Token}; pub async fn handle( db: Extension, config: Extension, - Json(request): Json, + request: Json, ) -> Result, ServerError> { let user = User::new(&request.username, &request.password); diff --git a/server/src/auth/register.rs b/server/src/auth/register.rs index 9af2401..5970f48 100644 --- a/server/src/auth/register.rs +++ b/server/src/auth/register.rs @@ -1,16 +1,16 @@ use axum::{Extension, Json}; +use homedisk_database::{Database, User}; use homedisk_types::{ auth::login::{Request, Response}, config::types::Config, errors::{AuthError, ServerError}, }; -use homedisk_utils::database::{Database, User}; use rust_utilities::crypto::jsonwebtoken::{Claims, Token}; pub async fn handle( db: Extension, config: Extension, - Json(request): Json, + request: Json, ) -> Result, ServerError> { let user = User::new(&request.username, &request.password); diff --git a/server/src/error.rs b/server/src/error.rs index e959978..ac1b349 100644 --- a/server/src/error.rs +++ b/server/src/error.rs @@ -32,7 +32,7 @@ impl fmt::Display for Error { match self { Error::Axum(err) => write!(f, "axum error: {}", err), Error::Hyper(err) => write!(f, "hyper error: {}", err), - Error::AddrParseError(err) => write!(f, "AddrParse error: {}", err), + Error::AddrParseError(err) => write!(f, "std::net::AddrParseError: {}", err), } } } diff --git a/server/src/lib.rs b/server/src/lib.rs index eb28591..a042dd9 100644 --- a/server/src/lib.rs +++ b/server/src/lib.rs @@ -1,9 +1,10 @@ pub mod auth; + mod error; use axum::{http::HeaderValue, routing::get, Extension, Router, Server}; +use homedisk_database::Database; use homedisk_types::config::types::Config; -use homedisk_utils::database::Database; use log::{debug, info}; use tower_http::cors::{CorsLayer, Origin}; diff --git a/types/Cargo.toml b/types/Cargo.toml index bacfff5..7e8e2a9 100644 --- a/types/Cargo.toml +++ b/types/Cargo.toml @@ -10,23 +10,10 @@ config = ["toml", "dirs"] thiserror = "1.0.30" uuid = "1.0.0" anyhow = "1.0.57" - -[dependencies.serde] -version = "1.0.136" -features = ["derive"] - -[dependencies.zeroize] -version = "1.5.4" -features = ["derive"] - -[dependencies.axum] -version = "0.5.3" -optional = true +serde = { version = "1.0.136", features = ["derive"] } +zeroize = { version = "1.5.4", features = ["derive"] } +axum = { version = "0.5.3", optional = true } # config -[dependencies.toml] -version = "0.5.9" -optional = true -[dependencies.dirs] -version = "4.0.0" -optional = true +toml = { version = "0.5.9", optional = true } +dirs = { version = "4.0.0", optional = true } diff --git a/utils/Cargo.toml b/utils/Cargo.toml deleted file mode 100644 index 6fe3e9c..0000000 --- a/utils/Cargo.toml +++ /dev/null @@ -1,33 +0,0 @@ -[package] -name = "homedisk-utils" -version = "0.0.0" -edition = "2021" - -[features] -full = ["database"] -database = ["sqlx", "uuid"] - -[dependencies] -log = "0.4.16" -futures-util = "0.3.21" - -[dependencies.serde] -version = "1.0.136" -features = ["derive"] - -# database -[dependencies.sqlx] -version = "0.5.13" -features = ["runtime-tokio-rustls", "sqlite"] -optional = true -[dependencies.uuid] -version = "1.0.0" -features = ["v5"] -optional = true -[dependencies.rust_utilities] -version = "0.2.0" -features = ["sha"] - -[dev-dependencies.tokio] -version = "1.17.0" -features = ["full"] diff --git a/utils/src/lib.rs b/utils/src/lib.rs deleted file mode 100644 index 67afb0b..0000000 --- a/utils/src/lib.rs +++ /dev/null @@ -1,2 +0,0 @@ -#[cfg(feature = "database")] -pub mod database;