Skip to content

Commit

Permalink
extract client and server common entity
Browse files Browse the repository at this point in the history
  • Loading branch information
timzaak committed Jul 1, 2024
1 parent ec42c07 commit 6003afa
Show file tree
Hide file tree
Showing 14 changed files with 176 additions and 77 deletions.
14 changes: 13 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
members = [
"server",
"client",
"tests"
"tests",
"entity",
]
resolver = "2"
2 changes: 1 addition & 1 deletion client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ path = "src/bin/main.rs"


[dependencies]
spa-server = { path = "../server" }
entity = {path = "../entity"}

# web request
reqwest = { version = "0.12", features = ["json", "blocking", "multipart", "stream", "rustls-tls"], default-features = false }
Expand Down
8 changes: 4 additions & 4 deletions client/src/api.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::Config;
use anyhow::anyhow;
use reqwest::{header, multipart, StatusCode};
use spa_server::admin_server::request::{DeleteDomainVersionOption, DomainWithOptVersionOption, DomainWithVersionOption, GetDomainOption, UpdateUploadingStatusOption};
use spa_server::domain_storage::{CertInfo, DomainInfo, ShortMetaData, UploadDomainPosition};
use entity::request::{DeleteDomainVersionOption, DomainWithOptVersionOption, DomainWithVersionOption, GetDomainOption, UpdateUploadingStatusOption};
use entity::storage::{CertInfo, DomainInfo, ShortMetaData, UploadDomainPosition};
use std::borrow::Cow;
use std::path::PathBuf;

Expand Down Expand Up @@ -205,8 +205,8 @@ impl API {
#[cfg(test)]
mod test {
use crate::api::API;
use spa_server::admin_server::request::UpdateUploadingStatusOption;
use spa_server::domain_storage::UploadingStatus;
use entity::request::UpdateUploadingStatusOption;
use entity::storage::UploadingStatus;
use crate::LOCAL_HOST;

fn get_api() -> API {
Expand Down
31 changes: 27 additions & 4 deletions client/src/upload_files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,20 @@ use console::style;
use futures::future::Either;
use futures::StreamExt;
use if_chain::if_chain;
use spa_server::admin_server::request::UpdateUploadingStatusOption;
use spa_server::domain_storage::{
md5_file, GetDomainPositionStatus, ShortMetaData, UploadingStatus,
use entity::request::UpdateUploadingStatusOption;
use entity::storage::{
GetDomainPositionStatus, ShortMetaData, UploadingStatus,
};
use std::borrow::Cow;
use std::collections::HashMap;
use std::fs;
use std::path::PathBuf;
use std::fs::File;
use std::io::Read;
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Arc;
use std::time::Duration;
use md5::{Digest, Md5};
use tracing::warn;
use walkdir::WalkDir;

Expand Down Expand Up @@ -204,3 +207,23 @@ async fn get_upload_version(api: &API, domain: &str, version: Option<u32>) -> an
Ok(resp.version)
}
}

fn md5_file(path: impl AsRef<Path>, byte_buffer: &mut Vec<u8>) -> Option<String> {
File::open(path)
.ok()
.map(|mut f| {
let mut hasher = Md5::new();
//if file_size > 1024 * 1024 {
//1Mb
loop {
let n = f.read(byte_buffer).ok()?;
let valid_buf_slice = &byte_buffer[..n];
if n == 0 {
break;
}
hasher.update(valid_buf_slice);
}
Some(format!("{:x}", hasher.finalize()))
})
.flatten()
}
3 changes: 2 additions & 1 deletion docs/develop/change-log.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Change Log
### Version 2.4.0
-

- improve: extract client and server common entity
- conf: remove hocon config

### Version 2.3.0
Expand Down
11 changes: 11 additions & 0 deletions entity/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "entity"
version = "0.1.0"
edition = "2021"

[dependencies]
serde = { version = "1.0", features = ["derive"] }
serde_repr = "0.1"
serde_json = "1"
duration-str = "0.11"
chrono = { version = "0.4", features = ["serde"] }
2 changes: 2 additions & 0 deletions entity/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod request;
pub mod storage;
54 changes: 54 additions & 0 deletions entity/src/request.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
use serde::{Deserialize, Serialize};
use crate::storage::UploadingStatus;

#[derive(Deserialize, Serialize)]
pub struct GetDomainOption {
pub domain: Option<String>,
}

#[derive(Deserialize, Serialize, Debug, Eq, PartialEq, Default)]
pub enum GetDomainPositionFormat {
#[default]
Path,
Json,
}

#[derive(Deserialize, Serialize, Debug)]
pub struct GetDomainPositionOption {
pub domain: String,
//#[serde(default="crate::admin_server::request::GetDomainPositionFormat::Path")]
#[serde(default)]
pub format: GetDomainPositionFormat,
}

#[derive(Deserialize, Serialize, Debug)]
pub struct UploadFileOption {
pub domain: String,
pub version: u32,
pub path: String,
}

#[derive(Deserialize, Serialize)]
pub struct DomainWithVersionOption {
pub domain: String,
pub version: u32,
}

#[derive(Deserialize, Serialize)]
pub struct DomainWithOptVersionOption {
pub domain: String,
pub version: Option<u32>,
}

#[derive(Deserialize, Serialize)]
pub struct UpdateUploadingStatusOption {
pub domain: String,
pub version: u32,
pub status: UploadingStatus,
}

#[derive(Deserialize, Serialize)]
pub struct DeleteDomainVersionOption {
pub domain: Option<String>,
pub max_reserve: Option<u32>,
}
49 changes: 49 additions & 0 deletions entity/src/storage.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
use std::path::PathBuf;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use serde_repr::{Deserialize_repr, Serialize_repr};

#[derive(Deserialize, Serialize, Debug)]
pub struct DomainInfo {
pub domain: String, // www.example.com|www.example.com/a/b
pub current_version: Option<u32>,
pub versions: Vec<u32>,
//pub uploading_version: Vec<u32>, //TODO: add uploading_versions
//pub web_path: Vec<String>, // [www.example.com/index.html|www.example.com/a/b/index.html,...]
}

#[derive(Deserialize, Serialize, Debug)]
pub struct ShortMetaData {
pub path: String,
pub md5: String,
pub length: u64,
}

#[derive(Serialize_repr, Deserialize_repr, PartialEq, Debug)]
#[repr(u8)]
pub enum UploadingStatus {
Uploading = 0,
Finish = 1,
}

#[derive(Serialize_repr, Deserialize_repr, PartialEq, Debug)]
#[repr(u8)]
pub enum GetDomainPositionStatus {
NewDomain = 0,
NewVersion = 1,
InUploading = 2,
}

#[derive(Deserialize, Serialize, Debug)]
pub struct UploadDomainPosition {
pub path: PathBuf,
pub version: u32,
pub status: GetDomainPositionStatus,
}

#[derive(Deserialize, Serialize, Debug)]
pub struct CertInfo {
pub begin: DateTime<Utc>,
pub end: DateTime<Utc>,
pub host: String,
}
1 change: 1 addition & 0 deletions server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ name = "spa-server"
path = "src/bin/main.rs"

[dependencies]
entity = {path = "../entity"}
# web
hyper = { version = "0.14", features = ["stream", "server", "http1", "http2", "tcp"] } # sync with warp
tokio = { version = "1.0", features = ["macros", "rt-multi-thread", "io-std", "sync", "time", "tokio-macros"] } # sync with warp
Expand Down
3 changes: 2 additions & 1 deletion server/src/acme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ use tracing::{debug, error, info, warn};
use walkdir::WalkDir;

use crate::config::{get_host_path_from_domain, ACMEConfig, ACMEType, Config};
use crate::domain_storage::{CertInfo, DomainStorage};
use entity::storage::{CertInfo};
use crate::domain_storage::DomainStorage;
use crate::tls::load_ssl_file;

const ACME_DIR: &str = "acme";
Expand Down
64 changes: 4 additions & 60 deletions server/src/admin_server.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::collections::HashMap;
use crate::acme::ACMEManager;
use crate::admin_server::request::{
use entity::request::{
DeleteDomainVersionOption, DomainWithOptVersionOption, DomainWithVersionOption,
GetDomainOption, GetDomainPositionOption, UpdateUploadingStatusOption, UploadFileOption,
};
Expand Down Expand Up @@ -225,12 +225,12 @@ impl AdminServer {
pub mod service {
use std::collections::HashMap;
use crate::acme::ACMEManager;
use crate::admin_server::request::{
use entity::request::{
DeleteDomainVersionOption, DomainWithOptVersionOption, DomainWithVersionOption,
GetDomainOption, GetDomainPositionFormat, GetDomainPositionOption,
UpdateUploadingStatusOption, UploadFileOption,
};
use crate::domain_storage::{DomainInfo, DomainStorage, URI_REGEX};
use crate::domain_storage::{DomainStorage, URI_REGEX};
use crate::{AdminConfig, HotReloadManager};
use anyhow::{anyhow, Context};
use bytes::Buf;
Expand All @@ -239,6 +239,7 @@ pub mod service {
use std::convert::Infallible;
use std::sync::Arc;
use tracing::error;
use entity::storage::DomainInfo;
use warp::http::StatusCode;
use warp::multipart::FormData;
use warp::reply::Response;
Expand Down Expand Up @@ -507,63 +508,6 @@ fn bad_resp(text:String) -> Response {
resp
}

pub mod request {
use crate::domain_storage::UploadingStatus;
use serde::{Deserialize, Serialize};

#[derive(Deserialize, Serialize)]
pub struct GetDomainOption {
pub domain: Option<String>,
}

#[derive(Deserialize, Serialize, Debug, Eq, PartialEq, Default)]
pub enum GetDomainPositionFormat {
#[default]
Path,
Json,
}

#[derive(Deserialize, Serialize, Debug)]
pub struct GetDomainPositionOption {
pub domain: String,
//#[serde(default="crate::admin_server::request::GetDomainPositionFormat::Path")]
#[serde(default)]
pub format: GetDomainPositionFormat,
}

#[derive(Deserialize, Serialize, Debug)]
pub struct UploadFileOption {
pub domain: String,
pub version: u32,
pub path: String,
}

#[derive(Deserialize, Serialize)]
pub struct DomainWithVersionOption {
pub domain: String,
pub version: u32,
}

#[derive(Deserialize, Serialize)]
pub struct DomainWithOptVersionOption {
pub domain: String,
pub version: Option<u32>,
}

#[derive(Deserialize, Serialize)]
pub struct UpdateUploadingStatusOption {
pub domain: String,
pub version: u32,
pub status: UploadingStatus,
}

#[derive(Deserialize, Serialize)]
pub struct DeleteDomainVersionOption {
pub domain: Option<String>,
pub max_reserve: Option<u32>,
}
}

fn build_async_job(
domain_storage: Arc<DomainStorage>,
max_reserve: Option<u32>,
Expand Down
Loading

0 comments on commit 6003afa

Please sign in to comment.