Skip to content

Commit ac6f581

Browse files
author
y-crew server
committed
add multiple web in single domain and integration tests
1 parent 14ef30e commit ac6f581

39 files changed

+1028
-267
lines changed

Cargo.lock

Lines changed: 26 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
[workspace]
22
members = [
33
"server",
4-
"client"
4+
"client",
5+
"tests"
56
]
6-
resolver = "2"
7+
resolver = "2"

client/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ path = "src/bin/main.rs"
2121
spa-server = { path = "../server" }
2222

2323
# web request
24-
reqwest = { version = "0.12.2", features = ["json", "blocking", "multipart", "stream"], default-features = false }
25-
tokio = { version = "1", features = ["macros", "rt-multi-thread", "io-std", "sync", "time", "tokio-macros"] }
24+
reqwest = { version = "0.12", features = ["json", "blocking", "multipart", "stream"], default-features = false }
25+
tokio = { version = "1", features = ["macros", "rt-multi-thread", "io-std", "sync", "time", "tokio-macros", "test-util"] }
2626
futures = "0.3"
2727

2828

client/src/api.rs

Lines changed: 30 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ use spa_server::domain_storage::{ShortMetaData, UploadDomainPosition};
99
use std::borrow::Cow;
1010
use std::path::PathBuf;
1111

12+
#[derive(Clone)]
1213
pub struct API {
13-
blocking_client: reqwest::blocking::Client,
1414
async_client: reqwest::Client,
1515
address: String,
1616
}
@@ -20,34 +20,34 @@ macro_rules! handle {
2020
if $resp.status() == StatusCode::OK {
2121
Ok(())
2222
} else {
23-
Err(anyhow!($resp.text()?))
23+
Err(anyhow!($resp.text().await?))
2424
}
2525
};
2626
}
2727
macro_rules! string_resp {
2828
($resp:ident) => {
2929
if $resp.status() == StatusCode::OK {
30-
Ok($resp.text()?)
30+
Ok($resp.text().await?)
3131
} else {
32-
Err(anyhow!($resp.text()?))
32+
Err(anyhow!($resp.text().await?))
3333
}
3434
};
3535
}
3636
macro_rules! json_resp {
3737
($resp:ident) => {
3838
if $resp.status() == StatusCode::OK {
39-
Ok($resp.json::<Value>()?)
39+
Ok($resp.json::<Value>().await?)
4040
} else {
41-
Err(anyhow!($resp.text()?))
41+
Err(anyhow!($resp.text().await?))
4242
}
4343
};
4444
($resp:ident, $t:ty) => {
4545
if $resp.status() == StatusCode::OK {
46-
let resp = $resp.json::<$t>()?;
46+
let resp = $resp.json::<$t>().await?;
4747
tracing::debug!("{:?}", &resp);
4848
Ok(resp)
4949
} else {
50-
Err(anyhow!($resp.text()?))
50+
Err(anyhow!($resp.text().await?))
5151
}
5252
};
5353
}
@@ -60,17 +60,10 @@ impl API {
6060
header::HeaderValue::from_str(format!("Bearer {}", &conf.server.auth_token).as_str())
6161
.unwrap(),
6262
);
63-
64-
let mut builder = reqwest::blocking::Client::builder();
65-
builder = builder.default_headers(headers.clone());
66-
67-
let blocking_client = builder.build()?;
68-
6963
let mut builder = reqwest::Client::builder();
7064
builder = builder.default_headers(headers);
7165
let async_client = builder.build()?;
7266
Ok(API {
73-
blocking_client,
7467
async_client,
7568
address: conf.server.address.clone(),
7669
})
@@ -80,58 +73,58 @@ impl API {
8073
format!("{}/{}", self.address, uri)
8174
}
8275

83-
pub fn get_domain_info(&self, domain: Option<String>) -> anyhow::Result<Value> {
84-
let mut q = self.blocking_client.get(self.url("status"));
76+
pub async fn get_domain_info(&self, domain: Option<String>) -> anyhow::Result<Value> {
77+
let mut q = self.async_client.get(self.url("status"));
8578
if let Some(domain) = domain {
8679
q = q.query(&["domain", &domain])
8780
}
88-
let resp = q.send()?;
81+
let resp = q.send().await?;
8982
json_resp!(resp)
9083
}
9184

92-
pub fn change_uploading_status(
85+
pub async fn change_uploading_status(
9386
&self,
9487
param: UpdateUploadingStatusOption,
9588
) -> anyhow::Result<()> {
9689
let resp = self
97-
.blocking_client
90+
.async_client
9891
.post(self.url("files/upload_status"))
9992
.json(&param)
100-
.send()?;
93+
.send().await?;
10194
handle!(resp)
10295
}
10396

104-
pub fn release_domain_version(
97+
pub async fn release_domain_version(
10598
&self,
10699
domain: String,
107100
version: Option<u32>,
108101
) -> anyhow::Result<String> {
109102
let resp = self
110-
.blocking_client
103+
.async_client
111104
.post(self.url("update_version"))
112105
.json(&DomainWithOptVersionOption { domain, version })
113-
.send()?;
106+
.send().await?;
114107
string_resp!(resp)
115108
}
116109

117-
pub fn reload_spa_server(&self) -> anyhow::Result<()> {
118-
let resp = self.blocking_client.post(self.url("reload")).send()?;
110+
pub async fn reload_spa_server(&self) -> anyhow::Result<()> {
111+
let resp = self.async_client.post(self.url("reload")).send().await?;
119112
handle!(resp)
120113
}
121114

122-
pub fn remove_files(
115+
pub async fn remove_files(
123116
&self,
124117
domain: Option<String>,
125118
max_reserve: Option<u32>,
126119
) -> anyhow::Result<()> {
127120
let resp = self
128-
.blocking_client
121+
.async_client
129122
.post(self.url("files/delete"))
130123
.json(&DeleteDomainVersionOption {
131124
domain,
132125
max_reserve,
133126
})
134-
.send()?;
127+
.send().await?;
135128
handle!(resp)
136129
}
137130

@@ -167,25 +160,25 @@ impl API {
167160
}
168161
}
169162

170-
pub fn get_file_metadata(
163+
pub async fn get_file_metadata(
171164
&self,
172165
domain: &str,
173166
version: u32,
174167
) -> anyhow::Result<Vec<ShortMetaData>> {
175168
let resp = self
176-
.blocking_client
169+
.async_client
177170
.get(self.url("files/metadata"))
178171
.query(&[("domain", domain), ("version", &version.to_string())])
179-
.send()?;
172+
.send().await?;
180173
json_resp!(resp, Vec<ShortMetaData>)
181174
}
182175

183-
pub fn get_upload_position(&self, domain: &str) -> anyhow::Result<UploadDomainPosition> {
176+
pub async fn get_upload_position(&self, domain: &str) -> anyhow::Result<UploadDomainPosition> {
184177
let resp = self
185-
.blocking_client
178+
.async_client
186179
.get(self.url("upload/position"))
187180
.query(&[("domain", domain), ("format", "Json")])
188-
.send()?;
181+
.send().await?;
189182
json_resp!(resp, UploadDomainPosition)
190183
}
191184
}
@@ -205,8 +198,8 @@ mod test {
205198
println!("{:?}", response);
206199
}
207200

208-
#[test]
209-
fn get_file_metadata() {
201+
#[tokio::test]
202+
async fn get_file_metadata() {
210203
let api = get_api();
211204
let r = api.get_file_metadata("self.noti.link", 1);
212205
println!("{:?}", r);

client/src/bin/main.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
11
use spa_client::run;
2+
use tracing::Level;
23
use tracing_subscriber::EnvFilter;
34

4-
fn main() {
5+
#[tokio::main]
6+
async fn main() {
57
tracing_subscriber::fmt()
6-
.with_env_filter(EnvFilter::from_default_env())
8+
.with_env_filter(
9+
EnvFilter::builder()
10+
.with_default_directive(Level::INFO.into())
11+
.from_env_lossy(),
12+
)
713
.init();
8-
run();
14+
let result = run().await;
15+
if let Some(err) = result.err() {
16+
eprintln!("{}", err);
17+
std::process::exit(-1);
18+
}
919
}

0 commit comments

Comments
 (0)