Skip to content

Commit 0340afc

Browse files
committed
add /_version
1 parent 61b2622 commit 0340afc

File tree

1 file changed

+48
-24
lines changed

1 file changed

+48
-24
lines changed

server/src/service.rs

Lines changed: 48 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
1+
use crate::cors::{cors_resp, resp_cors_request, Validated};
2+
use crate::DomainStorage;
3+
use futures_util::future::Either;
4+
use headers::HeaderMapExt;
5+
use hyper::header::LOCATION;
6+
use hyper::http::uri::{Authority, Scheme};
7+
use hyper::{Body, Request, Response, StatusCode};
18
use std::collections::HashMap;
29
use std::convert::Infallible;
310
use std::str::FromStr;
411
use std::sync::Arc;
5-
use futures_util::future::Either;
6-
use headers::{HeaderMapExt};
7-
use hyper::{Body, Request, Response, StatusCode};
8-
use hyper::header::LOCATION;
9-
use hyper::http::uri::{Authority, Scheme};
1012
use warp::fs::Conditionals;
11-
use crate::cors::{cors_resp, resp_cors_request, Validated};
12-
use crate::DomainStorage;
1313

1414
use crate::static_file_filter::{cache_or_file_reply, get_cache_file};
1515

16-
1716
pub struct ServiceConfig {
1817
pub default: DomainServiceConfig,
1918
pub inner: HashMap<String, DomainServiceConfig>,
@@ -30,24 +29,35 @@ impl ServiceConfig {
3029
}
3130
}
3231

33-
34-
pub async fn create_service(req: Request<Body>, service_config: Arc<ServiceConfig>, domain_storage: Arc<DomainStorage>) -> Result<Response<Body>, Infallible> {
32+
pub async fn create_service(
33+
req: Request<Body>,
34+
service_config: Arc<ServiceConfig>,
35+
domain_storage: Arc<DomainStorage>,
36+
) -> Result<Response<Body>, Infallible> {
3537
let from_uri = req.uri().authority().cloned();
3638

3739
// trick, need more check
38-
let authority_opt = from_uri.or_else(|| req.headers().get("host").map(|value|
39-
value.to_str().ok().map(|x| Authority::from_str(x).ok()).flatten()
40-
).flatten());
40+
let authority_opt = from_uri.or_else(|| {
41+
req.headers()
42+
.get("host")
43+
.map(|value| {
44+
value
45+
.to_str()
46+
.ok()
47+
.map(|x| Authority::from_str(x).ok())
48+
.flatten()
49+
})
50+
.flatten()
51+
});
4152

4253
if let Some(authority) = authority_opt {
4354
let host = authority.host();
4455
let service_config = service_config.get_domain_service_config(host);
4556
// cors
46-
let origin_opt =
47-
match resp_cors_request(req.method(), req.headers(), service_config.cors) {
48-
Either::Left(x) => Some(x),
49-
Either::Right(v) => return Ok(v)
50-
};
57+
let origin_opt = match resp_cors_request(req.method(), req.headers(), service_config.cors) {
58+
Either::Left(x) => Some(x),
59+
Either::Right(v) => return Ok(v),
60+
};
5161

5262
let scheme = req.uri().scheme();
5363
// redirect to https
@@ -58,8 +68,20 @@ pub async fn create_service(req: Request<Body>, service_config: Arc<ServiceConfi
5868
*resp.status_mut() = StatusCode::MOVED_PERMANENTLY;
5969
return Ok(resp);
6070
}
61-
// static file
71+
// get version
6272

73+
if req.uri().path() == "/_version" {
74+
let version = domain_storage
75+
.get_domain_info_by_domain(host)
76+
.map(|info| info.current_version)
77+
.flatten()
78+
.unwrap_or(0)
79+
.to_string();
80+
let resp = Body::from(version);
81+
let resp = Response::new(resp);
82+
return Ok(resp);
83+
}
84+
// static file
6385
let mut resp = match get_cache_file(req.uri().path(), host, domain_storage).await {
6486
Ok(item) => {
6587
let headers = req.headers();
@@ -69,14 +91,17 @@ pub async fn create_service(req: Request<Body>, service_config: Arc<ServiceConfi
6991
if_range: headers.typed_get(),
7092
range: headers.typed_get(),
7193
};
72-
let accept_encoding = headers.get("accept-encoding").map(|x| x.to_str().map(|x| x.to_string()).ok()).flatten();
94+
let accept_encoding = headers
95+
.get("accept-encoding")
96+
.map(|x| x.to_str().map(|x| x.to_string()).ok())
97+
.flatten();
7398
cache_or_file_reply(item, conditionals, accept_encoding).await
7499
}
75-
Err(resp) => Ok(resp)
100+
Err(resp) => Ok(resp),
76101
};
77102

78103
if let Some(Validated::Simple(origin)) = origin_opt {
79-
resp = resp.map(|r|cors_resp(r, origin));
104+
resp = resp.map(|r| cors_resp(r, origin));
80105
}
81106
resp
82107
} else {
@@ -86,7 +111,6 @@ pub async fn create_service(req: Request<Body>, service_config: Arc<ServiceConfi
86111
}
87112
}
88113

89-
90114
pub fn not_found() -> Response<Body> {
91115
let mut resp = Response::default();
92116
*resp.status_mut() = StatusCode::NOT_FOUND;
@@ -97,4 +121,4 @@ pub fn resp(code: StatusCode, str: &'static str) -> Response<Body> {
97121
let mut resp = Response::new(Body::from(str));
98122
*resp.status_mut() = code;
99123
resp
100-
}
124+
}

0 commit comments

Comments
 (0)