Compare commits

...

10 Commits

Author SHA1 Message Date
MedzikUser adcbada699
feat: create storage directory if doesn't exists 2022-09-21 20:53:22 +02:00
MedzikUser e50ee66b0e
feat: add cli
Added cli and options --config and --database to pass path to the configuration and database file.
2022-09-21 18:49:06 +02:00
MedzikUser f5d8341234
feat(config): create default config file if doesn't exists
Create configuration file with default values if file doesn't exists.
2022-09-21 18:10:12 +02:00
MedzikUser c41b0478d6
ci: add target i686-unknown-linux-musl 2022-09-21 17:41:40 +02:00
MedzikUser 699009096d
ci: fix prepare artifact 2022-09-21 16:44:08 +02:00
MedzikUser b48fbc0b82
ci: fix cargo build 2022-09-21 16:34:45 +02:00
MedzikUser c79ddbcff3
ci: fix typo in filename 2022-09-21 16:32:58 +02:00
MedzikUser 8b912dbd78
ci(fix): fix filename 2022-09-21 16:30:46 +02:00
MedzikUser ea18d9aca6
chore: run ci on push 2022-09-21 16:29:47 +02:00
MedzikUser b5dd0b3291
ci: rewrite
Rewrite github actions inspirated from 3d2c40ce2f/.github/workflows
2022-09-21 16:28:47 +02:00
12 changed files with 359 additions and 112 deletions

View File

@ -1,25 +1,21 @@
name: Build release binaries (and publish them if this is a tag)
name: Build release binaries
on:
push:
tags:
- "*"
# pull_request:
workflow_dispatch:
workflow_call:
env:
CARGO_TERM_COLOR: always
jobs:
binaries:
build-binaries:
strategy:
fail-fast: false
matrix:
target:
- x86_64-unknown-linux-musl
- i686-unknown-linux-musl
- armv7-unknown-linux-musleabihf
- aarch64-unknown-linux-musl
- x86_64-pc-windows-msvc
@ -29,15 +25,23 @@ jobs:
include:
- os: ubuntu-latest
target: x86_64-unknown-linux-musl
artifact_name: target/x86_64-unknown-linux-musl/release/homedisk
artifact_path: target/x86_64-unknown-linux-musl/release/homedisk
release_name: x86_64-unknown-linux-musl
cross: true
strip: true
cargo_flags: ""
- os: ubuntu-latest
target: i686-unknown-linux-musl
artifact_path: target/i686-unknown-linux-musl/release/homedisk
release_name: i686-unknown-linux-musl
cross: true
strip: true
cargo_flags: ""
- os: ubuntu-latest
target: armv7-unknown-linux-musleabihf
artifact_name: target/armv7-unknown-linux-musleabihf/release/homedisk
artifact_path: target/armv7-unknown-linux-musleabihf/release/homedisk
release_name: armv7-unknown-linux-musleabihf
cross: true
strip: false
@ -45,7 +49,7 @@ jobs:
- os: ubuntu-latest
target: aarch64-unknown-linux-musl
artifact_name: target/aarch64-unknown-linux-musl/release/homedisk
artifact_path: target/aarch64-unknown-linux-musl/release/homedisk
release_name: aarch64-unknown-linux-musl
cross: true
strip: false
@ -53,7 +57,7 @@ jobs:
- os: windows-latest
target: x86_64-pc-windows-msvc
artifact_name: target/x86_64-pc-windows-msvc/release/homedisk.exe
artifact_path: target/x86_64-pc-windows-msvc/release/homedisk.exe
release_name: x86_64-pc-windows-msvc.exe
cross: false
strip: true
@ -61,7 +65,7 @@ jobs:
- os: macos-latest
target: x86_64-apple-darwin
artifact_name: target/x86_64-apple-darwin/release/homedisk
artifact_path: target/x86_64-apple-darwin/release/homedisk
release_name: x86_64-apple-darwin
cross: false
strip: true
@ -69,7 +73,7 @@ jobs:
- os: macos-latest
target: aarch64-apple-darwin
artifact_name: target/aarch64-apple-darwin/release/homedisk
artifact_path: target/aarch64-apple-darwin/release/homedisk
release_name: aarch64-apple-darwin
cross: false
strip: true
@ -102,27 +106,17 @@ jobs:
uses: actions-rs/cargo@v1
with:
command: build
args: --release --target=${{ matrix.target }} ${{ matrix.cargo_flags }}
args: --release --target=${{ matrix.target }}
use-cross: ${{ matrix.cross }}
- name: Prepare artifact
shell: bash
run: |
mkdir release
mv ${{ matrix.artifact_path }} release/${{ matrix.release_name }}
- name: Upload artifact
uses: actions/upload-artifact@v3
with:
name: ${{ matrix.target }}
path: ${{ matrix.artifact_name }}
- name: Get tag name
id: tag_name
run: |
echo ::set-output name=current_version::${GITHUB_REF#refs/tags/v}
shell: bash
if: startsWith(github.ref, 'refs/tags/v')
- name: Publish
uses: svenstaro/upload-release-action@v2
with:
repo_token: ${{ secrets.GITHUB_TOKEN }}
file: ${{ matrix.artifact_name }}
tag: ${{ github.ref }}
asset_name: cloud-$tag-${{ matrix.release_name }}
if: startsWith(github.ref, 'refs/tags/v')
name: release-${{ matrix.target }}
path: ./release/*

99
.github/workflows/ci.yml vendored Normal file
View File

@ -0,0 +1,99 @@
name: Build release binaries (and publish them if this is a tag)
on:
push:
# pull_request:
workflow_dispatch:
env:
CARGO_TERM_COLOR: always
jobs:
build-release:
uses: ./.github/workflows/build-releases.yml
clippy-and-tests:
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
rust: [stable, nightly]
name: clippy and tests ${{ matrix.rust }} on ${{ matrix.os }}
runs-on: ${{ matrix.os }}
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Setup Rust toolchain
id: rust-toolchain
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: ${{ matrix.rust }}
override: true
components: clippy
- name: Cache
uses: actions/cache@v3
id: cache
with:
path: |
~/.cargo/registry/cache/
target/
key: clippy-test-${{ runner.os }}-${{ matrix.rust }}-${{ steps.rust-toolchain.outputs.rustc_hash }}-${{ hashFiles('**/Cargo.lock') }}
- name: cargo clippy
uses: actions-rs/cargo@v1
with:
command: clippy
args: --no-deps -- -D warnings
- name: cargo test
uses: actions-rs/cargo@v1
with:
command: test
args: --all-features
upload-release:
runs-on: ubuntu-latest
needs: [build-release]
if: startsWith(github.ref, 'refs/tags/v')
steps:
- name: Download artifacts
uses: actions/download-artifact@v3
with:
path: ./
- name: Move artifacts
run: |
mkdir release
mv release-*/* release/
- name: Get tag name
id: tag_name
run: |
echo ::set-output name=current_version::${GITHUB_REF#refs/tags/v}
- name: Upload release files
uses: softprops/action-gh-release@17cd0d34deddf848fc0e7d9be5202c148c270a0a # 0.1.14
with:
token: ${{ secrets.GITHUB_TOKEN }}
prerelease: false
tag_name: ${{ github.ref }}
draft: true
fail_on_unmatched_files: true
name: ${{ github.ref }} Release
body: |
<!-- Write summary here -->
---
## Features
## Changes
## Bug Fixes
## Internal Changes
files: |
./release/*

53
.github/workflows/nightly.yml vendored Normal file
View File

@ -0,0 +1,53 @@
name: Nightly
on:
schedule:
- cron: "0 0 * * *"
workflow_dispatch:
env:
CARGO_TERM_COLOR: always
jobs:
build-release:
uses: ./.github/workflows/build-releases.yml
upload-release:
runs-on: ubuntu-latest
needs: [build-release]
steps:
- name: Download artifacts
uses: actions/download-artifact@v3
with:
path: ./
- name: Move artifacts
run: |
mkdir release
mv release-*/* release/
echo "Generated $(ls ./release | wc -l) files:"
du -h -d 0 ./release/*
- name: Delete tag and release
uses: dev-drprasad/delete-tag-and-release@085c6969f18bad0de1b9f3fe6692a3cd01f64fe5 # 0.2.0
with:
delete_release: true
tag_name: nightly
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Sleep for a few seconds to prevent timing issues between the deletion and creation of the release
run: sleep 10
- name: Upload all release files
uses: softprops/action-gh-release@17cd0d34deddf848fc0e7d9be5202c148c270a0a # 0.1.14
with:
token: ${{ secrets.GITHUB_TOKEN }}
prerelease: true
tag_name: "nightly"
draft: false
fail_on_unmatched_files: true
files: |
./release/*

View File

@ -1,63 +0,0 @@
name: Rust
on:
push:
branches:
- main
pull_request:
workflow_dispatch:
env:
CARGO_TERM_COLOR: always
jobs:
build:
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
rust: [stable, nightly]
name: ${{ matrix.rust }} on ${{ matrix.os }}
runs-on: ${{ matrix.os }}
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Setup Rust toolchain
id: rust-toolchain
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: ${{ matrix.rust }}
override: true
components: clippy
- name: Cache
uses: actions/cache@v3
id: cache
with:
path: |
~/.cargo/registry/cache/
target/
key: ${{ runner.os }}-${{ matrix.rust }}-${{ steps.rust-toolchain.outputs.rustc_hash }}-${{ hashFiles('**/Cargo.lock') }}
- name: cargo build
uses: actions-rs/cargo@v1
with:
command: build
- name: cargo clippy
uses: actions-rs/cargo@v1
with:
command: clippy
args: --no-deps -- -D warnings
- name: cargo test
uses: actions-rs/cargo@v1
with:
command: test
args: --all-features

111
Cargo.lock generated
View File

@ -107,6 +107,17 @@ dependencies = [
"num-traits",
]
[[package]]
name = "atty"
version = "0.2.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8"
dependencies = [
"hermit-abi",
"libc",
"winapi",
]
[[package]]
name = "autocfg"
version = "1.1.0"
@ -292,6 +303,45 @@ dependencies = [
"winapi",
]
[[package]]
name = "clap"
version = "3.2.22"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "86447ad904c7fb335a790c9d7fe3d0d971dc523b8ccd1561a520de9a85302750"
dependencies = [
"atty",
"bitflags",
"clap_derive",
"clap_lex",
"indexmap",
"once_cell",
"strsim",
"termcolor",
"textwrap",
]
[[package]]
name = "clap_derive"
version = "3.2.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ea0c8bce528c4be4da13ea6fead8965e95b6073585a2f05204bd8f4119f82a65"
dependencies = [
"heck",
"proc-macro-error",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "clap_lex"
version = "0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2850f2f5a82cbf437dd5af4d49848fbdfc27c157c3d010345776f952765261c5"
dependencies = [
"os_str_bytes",
]
[[package]]
name = "core-foundation-sys"
version = "0.8.3"
@ -717,6 +767,7 @@ dependencies = [
"axum-server",
"backtrace",
"byte-unit",
"clap",
"crypto-utils",
"futures-util",
"governor",
@ -1098,6 +1149,12 @@ version = "1.14.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2f7254b99e31cad77da24b08ebf628882739a608578bb1bcdfc1f9c21260d7c0"
[[package]]
name = "os_str_bytes"
version = "6.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9ff7415e9ae3fff1225851df9e0d9e4e5479f947619774677a63572e55e80eff"
[[package]]
name = "parking_lot"
version = "0.11.2"
@ -1211,6 +1268,30 @@ version = "0.2.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eb9f9e6e233e5c4a35559a617bf40a4ec447db2e84c20b55a6f83167b7e57872"
[[package]]
name = "proc-macro-error"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c"
dependencies = [
"proc-macro-error-attr",
"proc-macro2",
"quote",
"syn",
"version_check",
]
[[package]]
name = "proc-macro-error-attr"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869"
dependencies = [
"proc-macro2",
"quote",
"version_check",
]
[[package]]
name = "proc-macro2"
version = "1.0.43"
@ -1601,6 +1682,12 @@ dependencies = [
"unicode-normalization",
]
[[package]]
name = "strsim"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623"
[[package]]
name = "subtle"
version = "2.4.1"
@ -1624,6 +1711,21 @@ version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "20518fe4a4c9acf048008599e464deb21beeae3d3578418951a189c235a7a9a8"
[[package]]
name = "termcolor"
version = "1.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bab24d30b911b2376f3a13cc2cd443142f0c81dda04c118693e35b3835757755"
dependencies = [
"winapi-util",
]
[[package]]
name = "textwrap"
version = "0.15.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "949517c0cf1bf4ee812e2e07e08ab448e3ae0d23472aee8a06c985f0c8815b16"
[[package]]
name = "thiserror"
version = "1.0.35"
@ -2114,6 +2216,15 @@ version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
name = "winapi-util"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178"
dependencies = [
"winapi",
]
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"

View File

@ -1,5 +1,6 @@
[package]
name = "homedisk"
description = "Extremely fast and lightweight local cloud for your data written in Rust."
authors = ["MedzikUser <medzik@duck.com>"]
homepage = "https://github.com/HomeDisk/cloud"
repository = "https://github.com/HomeDisk/cloud"
@ -40,8 +41,11 @@ axum = { version = "0.6.0-rc.2", features = ["http2", "multipart"] }
axum-server = { version = "0.4", features = ["tls-rustls"] }
tower-http = { version = "0.3", features = ["full"] }
hyper = { version = "0.14", features = ["full"] }
byte-unit = "4.0.14"
byte-unit = "4.0"
# HTTP rate limiting
once_cell = "1.14.0"
governor = "0.5.0"
once_cell = "1.14"
governor = "0.5"
# CLI
clap = { version = "3.2", features = ["derive"] }

View File

@ -19,6 +19,8 @@ pub struct Database {
pub pool: SqlitePool,
}
pub const DATABASE_TABLES: &str = include_str!("../../sql/tables.sql");
impl Database {
/// Open a SQLite database
pub async fn open(path: &str) -> Result<Self> {
@ -44,7 +46,7 @@ impl Database {
/// Create all required tables for HomeDisk.
pub async fn create_tables(&self) -> Result<SqliteQueryResult> {
self.pool
.execute(include_str!("../../tables.sql"))
.execute(DATABASE_TABLES)
.await
.map_err(|e| Error::CreateTables(e.to_string()))
}

View File

@ -6,42 +6,83 @@
//!
//! Go to [config] module
use std::{fs::File, path::Path};
use std::{
fs::{self, File},
io::Write,
path::Path,
};
use clap::Parser;
use config::Config;
use tracing::{info, warn};
use crate::database::Database;
#[path = "./types/config.rs"]
mod config;
mod database;
mod logger;
mod server;
mod variables;
/// Default SQLite file path for the database.
pub const DATABASE_FILE: &str = "homedisk.db";
/// Default configuration file.
pub const CONFIG_FILE: &str = "config.toml";
pub use variables::*;
#[derive(Debug, Parser)]
#[clap(
name = env!("CARGO_PKG_NAME"),
about = env!("CARGO_PKG_DESCRIPTION"),
version = env!("CARGO_PKG_VERSION"),
)]
struct Cli {
#[clap(short = 'c', long = "config", help = "Configuration file path", default_value = DEFAULT_CONFIG_FILE, display_order = 1)]
config: String,
#[clap(short = 'd', long = "database", help = "SQLite database path", default_value = DEFAULT_DATABASE_FILE, display_order = 2)]
database: String,
}
#[tokio::main]
async fn main() {
logger::init();
let config = Config::parse(CONFIG_FILE).expect("notrace - Failed to parse configuration file");
let args = Cli::parse();
let config_path = args.config;
let database_path = args.database;
// if configuration file doesn't exist, create it
if !Path::new(&config_path).exists() {
warn!("Configuration file doesn't exists.");
let mut file =
File::create(config_path).expect("notrace - Failed to create configuration file");
file.write_all(DEFAULT_CONFIG_CONTENT)
.expect("notrace - Failed to write default configuration to config file");
info!("Created configuration file. Exiting...");
std::process::exit(0);
}
let config = Config::parse(&config_path).expect("notrace - Failed to parse configuration file");
// if storage directory doesn't exist, create it
if !Path::new(&config.storage.path).exists() {
fs::create_dir_all(&config.storage.path)
.expect("notrace - Failed to create storage directory");
}
// open database connection
let db =
// if database file doesn't exists create it
if !Path::new(DATABASE_FILE).exists() {
if !Path::new(&database_path).exists() {
warn!("Database file doesn't exists.");
info!("Creating database file...");
// create an empty database file
File::create(DATABASE_FILE).expect("notrace - Failed to create a database file");
File::create(&database_path).expect("notrace - Failed to create a database file");
// open database file
let db = Database::open(DATABASE_FILE)
let db = Database::open(&database_path)
.await
.expect("notrace - Failed to open database file");
@ -54,7 +95,7 @@ async fn main() {
}
// if database file exists
else {
Database::open(DATABASE_FILE)
Database::open(&database_path)
.await
.expect("notrace - Failed to open database file")
};

View File

@ -89,7 +89,7 @@ async fn redirect_http_to_https(config: Config) {
}
};
debug!("🚀 Http redirect listening on http://{host}");
debug!("🚀 HTTPS redirect listening on http://{host}");
axum::Server::bind(&host.parse().unwrap())
.serve(redirect.into_make_service())

6
src/variables.rs Normal file
View File

@ -0,0 +1,6 @@
/// Default SQLite file path for the database.
pub const DEFAULT_DATABASE_FILE: &str = "homedisk.db";
/// Default configuration file.
pub const DEFAULT_CONFIG_FILE: &str = "config.toml";
/// Default configuration file content.
pub static DEFAULT_CONFIG_CONTENT: &[u8] = include_bytes!("../config.toml");