Compare commits
4 Commits
d80846ffb2
...
f440f30ea8
Author | SHA1 | Date |
---|---|---|
MedzikUser | f440f30ea8 | |
MedzikUser | 4a9f8ec2e8 | |
MedzikUser | bf10de2ca1 | |
MedzikUser | 6b5186b503 |
49
README.md
49
README.md
|
@ -1,36 +1,15 @@
|
||||||
# HomeDisk cloud server
|
<p align="center">
|
||||||
|
<img width="500" src="https://raw.githubusercontent.com/HomeDisk/.github/main/img/HomeDisk.svg" alt="HomeDisk Icon" />
|
||||||
|
</p>
|
||||||
|
|
||||||
> **Warning**
|
<p align="center">
|
||||||
> Currently, work on the project is suspended because I'm not good at frontend and don't have time :(
|
<a href="https://github.com/MedzikUser/HomeDisk"><img src="https://img.shields.io/badge/built_with-Rust-dca282.svg?style=flat-square"></a>
|
||||||
|
|
||||||
[docs-rs]: https://img.shields.io/badge/docs.rs-66c2a5?style=for-the-badge&labelColor=555555&logo=docs.rs
|
<a href="https://github.com/MedzikUser/HomeDisk"><img src="https://img.shields.io/badge/license-GPL_3.0-00bfff.svg?style=flat-square"></a>
|
||||||
[total-lines]: https://img.shields.io/tokei/lines/github/MedzikUser/HomeDisk?style=for-the-badge&logo=github&color=fede00
|
|
||||||
[code-size]: https://img.shields.io/github/languages/code-size/MedzikUser/HomeDisk?style=for-the-badge&color=c8df52&logo=github
|
<a href="https://github.com/MedzikUser/HomeDisk"><img src="https://img.shields.io/github/workflow/status/MedzikUser/HomeDisk/Rust/main?style=flat-square"></a>
|
||||||
[ci]: https://img.shields.io/github/workflow/status/MedzikUser/HomeDisk/Rust/main?style=for-the-badge
|
|
||||||
|
<a href="https://homedisk-doc.vercel.app"><img src="https://img.shields.io/badge/docs.rs-66c2a5?style=flat-square&labelColor=555555&logo=docs.rs"></a>
|
||||||
[screenshot-home]: https://raw.githubusercontent.com/HomeDisk/.github/main/screenshots/home.png
|
|
||||||
[screenshot-login]: https://raw.githubusercontent.com/HomeDisk/.github/main/screenshots/login.png
|
<a href="https://documenter.getpostman.com/view/23280189/VVk9dwRk"><img src="https://img.shields.io/badge/API_Docs-887BB0?style=flat-square&labelColor=555555&logo=postman"></a>
|
||||||
|
</p>
|
||||||
[![docs-rs]](https://homedisk-doc.vercel.app)
|
|
||||||
[![total-lines]](https://github.com/MedzikUser/HomeDisk)
|
|
||||||
[![code-size]](https://github.com/MedzikUser/HomeDisk)
|
|
||||||
[![ci]](https://github.com/MedzikUser/HomeDisk/actions/workflows/rust.yml)
|
|
||||||
|
|
||||||
![screenshot-home]
|
|
||||||
|
|
||||||
![screenshot-login]
|
|
||||||
|
|
||||||
## 👨💻 Building
|
|
||||||
|
|
||||||
First clone the repository: `git clone https://github.com/MedzikUser/HomeDisk.git`
|
|
||||||
|
|
||||||
### Requirements
|
|
||||||
- Rust
|
|
||||||
|
|
||||||
To build run the command: `cargo build --release`
|
|
||||||
|
|
||||||
The compiled binary can be found in `./target/release/homedisk`
|
|
||||||
|
|
||||||
## Configure
|
|
||||||
|
|
||||||
Go to [config](https://homedisk-doc.medzik.xyz/homedisk_types/config) module
|
|
||||||
|
|
|
@ -1,21 +1,19 @@
|
||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
|
|
||||||
/// Database Error
|
#[derive(Debug, Error, PartialEq, Eq, PartialOrd, Ord)]
|
||||||
#[derive(Debug, Error)]
|
|
||||||
pub enum Error {
|
pub enum Error {
|
||||||
#[error("user not found")]
|
#[error("User not found")]
|
||||||
UserNotFound,
|
UserNotFound,
|
||||||
#[error("failed to open database: {0}")]
|
#[error("Failed to open database: {0}")]
|
||||||
OpenDatabase(sqlx::Error),
|
OpenDatabase(String),
|
||||||
#[error("failed to connect to the database: {0}")]
|
#[error("Failed to open connection with database: {0}")]
|
||||||
ConnectDatabase(sqlx::Error),
|
ConnectDatabase(String),
|
||||||
#[error("failed to get row: {0}")]
|
#[error("Failed to get row: {0}")]
|
||||||
GetRow(sqlx::Error),
|
GetRow(String),
|
||||||
#[error("failed to create all required tables: {0}")]
|
#[error("Failed to create all required tables: {0}")]
|
||||||
CreateTables(sqlx::Error),
|
CreateTables(String),
|
||||||
#[error("failed to execute the query: {0}")]
|
#[error("Failed to execute the query: {0}")]
|
||||||
Execute(sqlx::Error),
|
Execute(String),
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Custom Result alias for a [enum@Error].
|
|
||||||
pub type Result<T> = std::result::Result<T, Error>;
|
pub type Result<T> = std::result::Result<T, Error>;
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
//! [SQLite database functions](sqlite::Database).
|
//! [SQLite database functions](sqlite::Database)
|
||||||
|
|
||||||
pub mod error;
|
pub mod error;
|
||||||
mod sqlite;
|
mod sqlite;
|
||||||
|
|
|
@ -19,14 +19,14 @@ pub struct Database {
|
||||||
pub pool: SqlitePool,
|
pub pool: SqlitePool,
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Check UUID
|
|
||||||
impl Database {
|
impl Database {
|
||||||
/// Open a SQLite database
|
/// Open a SQLite database
|
||||||
pub async fn open(path: &str) -> Result<Self> {
|
pub async fn open(path: &str) -> Result<Self> {
|
||||||
debug!("Opening SQLite database: {}", path);
|
debug!("Opening SQLite database: {}", path);
|
||||||
|
|
||||||
// sqlite connection options
|
// sqlite connection options
|
||||||
let mut options = SqliteConnectOptions::from_str(path).map_err(Error::OpenDatabase)?;
|
let mut options =
|
||||||
|
SqliteConnectOptions::from_str(path).map_err(|e| Error::OpenDatabase(e.to_string()))?;
|
||||||
|
|
||||||
// set log level to Debug
|
// set log level to Debug
|
||||||
options.log_statements(LevelFilter::Debug);
|
options.log_statements(LevelFilter::Debug);
|
||||||
|
@ -34,7 +34,7 @@ impl Database {
|
||||||
// create a database pool
|
// create a database pool
|
||||||
let pool = SqlitePool::connect_with(options)
|
let pool = SqlitePool::connect_with(options)
|
||||||
.await
|
.await
|
||||||
.map_err(Error::ConnectDatabase)?;
|
.map_err(|e| Error::ConnectDatabase(e.to_string()))?;
|
||||||
|
|
||||||
info!("Connected to database!");
|
info!("Connected to database!");
|
||||||
|
|
||||||
|
@ -46,7 +46,7 @@ impl Database {
|
||||||
self.pool
|
self.pool
|
||||||
.execute(include_str!("../../tables.sql"))
|
.execute(include_str!("../../tables.sql"))
|
||||||
.await
|
.await
|
||||||
.map_err(Error::CreateTables)
|
.map_err(|e| Error::CreateTables(e.to_string()))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Create new user in the database.
|
/// Create new user in the database.
|
||||||
|
@ -59,7 +59,10 @@ impl Database {
|
||||||
.bind(&user.username)
|
.bind(&user.username)
|
||||||
.bind(&user.password);
|
.bind(&user.password);
|
||||||
|
|
||||||
self.pool.execute(query).await.map_err(Error::Execute)
|
self.pool
|
||||||
|
.execute(query)
|
||||||
|
.await
|
||||||
|
.map_err(|e| Error::Execute(e.to_string()))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Search for a user.
|
/// Search for a user.
|
||||||
|
@ -78,7 +81,7 @@ impl Database {
|
||||||
let row = stream
|
let row = stream
|
||||||
.try_next()
|
.try_next()
|
||||||
.await
|
.await
|
||||||
.map_err(Error::Execute)?
|
.map_err(|e| Error::Execute(e.to_string()))?
|
||||||
.ok_or(Error::UserNotFound)?;
|
.ok_or(Error::UserNotFound)?;
|
||||||
|
|
||||||
Self::find(row)
|
Self::find(row)
|
||||||
|
@ -98,7 +101,7 @@ impl Database {
|
||||||
let row = stream
|
let row = stream
|
||||||
.try_next()
|
.try_next()
|
||||||
.await
|
.await
|
||||||
.map_err(Error::Execute)?
|
.map_err(|e| Error::Execute(e.to_string()))?
|
||||||
.ok_or(Error::UserNotFound)?;
|
.ok_or(Error::UserNotFound)?;
|
||||||
|
|
||||||
Self::find(row)
|
Self::find(row)
|
||||||
|
@ -106,11 +109,17 @@ impl Database {
|
||||||
|
|
||||||
fn find(row: SqliteRow) -> Result<User> {
|
fn find(row: SqliteRow) -> Result<User> {
|
||||||
// get `id` row
|
// get `id` row
|
||||||
let id = row.try_get("id").map_err(Error::GetRow)?;
|
let id = row
|
||||||
|
.try_get("id")
|
||||||
|
.map_err(|e| Error::GetRow(e.to_string()))?;
|
||||||
// get `username` row
|
// get `username` row
|
||||||
let username = row.try_get("username").map_err(Error::GetRow)?;
|
let username = row
|
||||||
|
.try_get("username")
|
||||||
|
.map_err(|e| Error::GetRow(e.to_string()))?;
|
||||||
// get `password` row
|
// get `password` row
|
||||||
let password = row.try_get("password").map_err(Error::GetRow)?;
|
let password = row
|
||||||
|
.try_get("password")
|
||||||
|
.map_err(|e| Error::GetRow(e.to_string()))?;
|
||||||
|
|
||||||
Ok(User {
|
Ok(User {
|
||||||
id,
|
id,
|
||||||
|
@ -155,7 +164,10 @@ mod tests {
|
||||||
async fn test_find_user() {
|
async fn test_find_user() {
|
||||||
let db = new_user().await;
|
let db = new_user().await;
|
||||||
|
|
||||||
let user = db.find_user(&User::new(USERNAME, PASSWORD, false)).await.unwrap();
|
let user = db
|
||||||
|
.find_user(&User::new(USERNAME, PASSWORD, false))
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
assert_eq!(user.username, USERNAME)
|
assert_eq!(user.username, USERNAME)
|
||||||
}
|
}
|
||||||
|
@ -169,6 +181,6 @@ mod tests {
|
||||||
.await
|
.await
|
||||||
.unwrap_err();
|
.unwrap_err();
|
||||||
|
|
||||||
assert_eq!(err.to_string(), "user not found")
|
assert_eq!(err, Error::UserNotFound)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,7 +3,7 @@ use serde::{Deserialize, Serialize};
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
/// SQL user entry
|
/// SQL user entry
|
||||||
#[derive(Debug, Serialize, Deserialize)]
|
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
|
||||||
pub struct User {
|
pub struct User {
|
||||||
pub id: String,
|
pub id: String,
|
||||||
pub username: String,
|
pub username: String,
|
||||||
|
|
27
src/main.rs
27
src/main.rs
|
@ -1,31 +1,6 @@
|
||||||
//! HomeDisk cloud server
|
//! HomeDisk cloud server
|
||||||
//!
|
//!
|
||||||
//! [github]: https://img.shields.io/badge/github-8da0cb?style=for-the-badge&labelColor=555555&logo=github
|
//! [Source code available on GitHub](https://github.com/MedzikUser/HomeDisk)
|
||||||
//! [docs-rs]: https://img.shields.io/badge/docs.rs-66c2a5?style=for-the-badge&labelColor=555555&logo=docs.rs
|
|
||||||
//! [total-lines]: https://img.shields.io/tokei/lines/github/MedzikUser/HomeDisk?style=for-the-badge&logo=github&color=fede00
|
|
||||||
//! [code-size]: https://img.shields.io/github/languages/code-size/MedzikUser/HomeDisk?style=for-the-badge&color=c8df52&logo=github
|
|
||||||
//! [ci]: https://img.shields.io/github/workflow/status/MedzikUser/HomeDisk/Rust/main?style=for-the-badge
|
|
||||||
//!
|
|
||||||
//! [home-screenshot]: https://raw.githubusercontent.com/HomeDisk/.github/main/screenshots/home.png
|
|
||||||
//!
|
|
||||||
//! [![github]](https://github.com/MedzikUser/HomeDisk)
|
|
||||||
//! [![docs-rs]](https://homedisk-doc.vercel.app)
|
|
||||||
//! [![total-lines]](https://github.com/MedzikUser/HomeDisk)
|
|
||||||
//! [![code-size]](https://github.com/MedzikUser/HomeDisk)
|
|
||||||
//! [![ci]](https://github.com/MedzikUser/HomeDisk/actions/workflows/rust.yml)
|
|
||||||
//!
|
|
||||||
//! ![home-screenshot]
|
|
||||||
//!
|
|
||||||
//! ## 👨💻 Building
|
|
||||||
//!
|
|
||||||
//! First clone the repository: `git clone https://github.com/MedzikUser/HomeDisk.git`
|
|
||||||
//!
|
|
||||||
//! ### Requirements
|
|
||||||
//! - [Rust](https://rust-lang.org)
|
|
||||||
//!
|
|
||||||
//! To build run the command: `cargo build --release`
|
|
||||||
//!
|
|
||||||
//! The compiled binary can be found in `./target/release/homedisk`
|
|
||||||
//!
|
//!
|
||||||
//! ## Configure
|
//! ## Configure
|
||||||
//!
|
//!
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
//! [Configuration file types](Config).
|
//! [Configuration file types](Config)
|
||||||
|
|
||||||
use std::fs;
|
use std::fs;
|
||||||
|
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
Loading…
Reference in New Issue