Skip to content

Commit 4c2fdc0

Browse files
committed
fix client command info bug and .SPA-Multiple incorrectly write
1 parent 2cb751d commit 4c2fdc0

File tree

4 files changed

+76
-21
lines changed

4 files changed

+76
-21
lines changed

client/src/api.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ use anyhow::anyhow;
33
use reqwest::{header, multipart, StatusCode};
44
use serde_json::Value;
55
use spa_server::admin_server::request::{
6-
DeleteDomainVersionOption, DomainWithOptVersionOption, UpdateUploadingStatusOption,
6+
DeleteDomainVersionOption, DomainWithOptVersionOption, GetDomainOption,
7+
UpdateUploadingStatusOption,
78
};
89
use spa_server::domain_storage::{ShortMetaData, UploadDomainPosition};
910
use std::borrow::Cow;
@@ -74,10 +75,8 @@ impl API {
7475
}
7576

7677
pub async fn get_domain_info(&self, domain: Option<String>) -> anyhow::Result<Value> {
77-
let mut q = self.async_client.get(self.url("status"));
78-
if let Some(domain) = domain {
79-
q = q.query(&["domain", &domain])
80-
}
78+
let param = GetDomainOption { domain };
79+
let mut q = self.async_client.get(self.url("status")).query(&param);
8180
let resp = q.send().await?;
8281
json_resp!(resp)
8382
}

client/src/commands.rs

+15-3
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,19 @@ pub struct CliCommand {
1212

1313
#[derive(Subcommand, Debug)]
1414
pub enum Commands {
15-
Info { domain: Option<String> },
15+
Info {
16+
domain: Option<String>,
17+
},
1618
Upload(UploadArg),
1719
Release {
1820
domain: String,
19-
version: Option<u32> },
21+
version: Option<u32>,
22+
},
2023
Reload,
21-
Delete {domain: Option<String>, max_reserve:Option<u32>}
24+
Delete {
25+
domain: Option<String>,
26+
max_reserve: Option<u32>,
27+
},
2228
}
2329

2430
#[derive(Args, Debug)]
@@ -55,6 +61,12 @@ mod test {
5561
let c = CliCommand::parse_from(&["spa-client", "--config-dir=abc.conf", "info"]);
5662
//println!("{:?}", &c);
5763
assert_eq!(c.config_dir, Some(PathBuf::from("abc.conf")));
64+
let c = CliCommand::parse_from(&["spa-client", "info", "www.example.com"]);
65+
if let Commands::Info { domain } = c.commands {
66+
assert_eq!(domain.unwrap(), "www.example.com")
67+
} else {
68+
unreachable!()
69+
}
5870
}
5971

6072
#[test]

docs/develop/change-log.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
- [x] fix: command client upload interrupt, get error file key.
66
- [x] fix: admin server report os:9 error when upload existed file.
7-
- [x] ci: add ci for spa-server
7+
- [x] ci: add ci for spa-server, and GitHub action could run it
8+
- [x] fix: command client info incorrect handle domain query string
9+
- [x] fix: fix .SPA-Multiple would write sub dir more than once
810

911
### Version 2.2.0
1012

server/src/domain_storage.rs

+54-12
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ impl DomainStorage {
215215
}
216216
}
217217
if max_version > 0 {
218-
tracing::info!("serve: {},version: {}", domain_dir_name, max_version);
218+
info!("serve: {},version: {}", domain_dir_name, max_version);
219219
return Ok((uploading_version, Some(max_version)));
220220
}
221221
return Ok((uploading_version, None));
@@ -283,17 +283,23 @@ impl DomainStorage {
283283
let multiple_file =
284284
self.prefix.join(&host).join(MULTIPLE_WEB_FILE_NAME);
285285
if multiple_file.exists() {
286-
let mut file =
287-
OpenOptions::new().append(true).open(multiple_file)?;
288-
writeln!(file, "{}", path.to_string())?;
286+
let mut file = OpenOptions::new()
287+
.append(true)
288+
.read(true)
289+
.open(multiple_file)?;
290+
let mut multiple_path = String::new();
291+
file.read_to_string(&mut multiple_path)?;
292+
if !multiple_path.lines().any(|x| x == path) {
293+
writeln!(file, "{}", path)?;
294+
}
289295
}
290296
map.insert(path.to_string(), (new_path.clone(), version));
291297
}
292298
};
293299
}
294300
None => {
295301
//TODO: check if MULTIPLE_WEB_FILE_NAME exists
296-
if path == "" {
302+
if path.is_empty() {
297303
self.meta.insert(
298304
host.to_string(),
299305
DomainMeta::OneWeb(new_path.clone(), version),
@@ -303,10 +309,16 @@ impl DomainStorage {
303309
let mut file = OpenOptions::new()
304310
.create(true)
305311
.append(true)
312+
.read(true)
306313
.open(&multiple_file)?;
307314

315+
let mut multiple_path = String::new();
316+
file.read_to_string(&mut multiple_path)?;
317+
if !multiple_path.lines().any(|x| x == path) {
318+
writeln!(file, "{}", path)?;
319+
}
308320
let map = DashMap::new();
309-
writeln!(file, "{}", path.to_string())?;
321+
310322
info!("create multiple_file {multiple_file:?}, append {path}");
311323
map.insert(path.to_string(), (new_path.clone(), version));
312324

@@ -597,7 +609,7 @@ impl DomainStorage {
597609
None => (domain.as_str(), ""),
598610
};
599611
let multiple = self.prefix.join(host).join(MULTIPLE_WEB_FILE_NAME);
600-
if path != "" {
612+
if !path.is_empty() {
601613
if !multiple.exists() {
602614
let parent = self.prefix.join(host);
603615
if !parent.exists() && fs::create_dir_all(&parent).is_err() {
@@ -721,9 +733,15 @@ pub struct UploadDomainPosition {
721733

722734
#[cfg(test)]
723735
mod test {
736+
use crate::config::Config;
724737
use crate::domain_storage::{DomainStorage, URI_REGEX_STR};
738+
use crate::file_cache::FileCache;
725739
use hyper::Uri;
726740
use regex::Regex;
741+
use std::env;
742+
use std::fs::OpenOptions;
743+
use std::io::Read;
744+
use std::io::Write;
727745
use std::ops::RangeInclusive;
728746
use std::path::PathBuf;
729747
use std::str::FromStr;
@@ -789,9 +807,33 @@ mod test {
789807
// println!("{:?}", path.join("usr/lib/pam/").to_str());
790808
// }
791809

792-
// would crea
793-
// #[test]
794-
// fn test_create_file() {
795-
// File::create_new(PathBuf::from("/tmp/bccskwef")).unwrap();
796-
// }
810+
#[test]
811+
fn test_domain_storage_get_domain_info() {
812+
let path = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../test/config.test.conf");
813+
env::set_var("SPA_CONFIG", path.display().to_string());
814+
let mut config = Config::load().unwrap();
815+
config.file_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
816+
.join("../test/data")
817+
.display()
818+
.to_string();
819+
let file_cache = FileCache::new(&config);
820+
let storage = DomainStorage::init(&config.file_dir, file_cache).unwrap();
821+
let result = storage.get_domain_info().unwrap();
822+
823+
println!("{:?}", result);
824+
}
825+
#[test]
826+
fn test_file_read() {
827+
let mut file = OpenOptions::new()
828+
.read(true)
829+
.create(true)
830+
.append(true)
831+
.write(true)
832+
.open("/tmp/cde.txt")
833+
.unwrap();
834+
let mut text = String::new();
835+
file.read_to_string(&mut text).unwrap();
836+
assert_eq!(&text, "");
837+
//writeln!(file, "{}", "abc").unwrap();
838+
}
797839
}

0 commit comments

Comments
 (0)