2019-05-08 15:36:19 +00:00
|
|
|
use failure::*;
|
|
|
|
use lazy_static::lazy_static;
|
|
|
|
|
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
use futures::*;
|
|
|
|
use hyper::{Body, Request, Response, StatusCode};
|
|
|
|
|
|
|
|
use crate::tools;
|
|
|
|
use crate::api_schema::router::*;
|
|
|
|
use crate::server::formatter::*;
|
|
|
|
use crate::server::WorkerTask;
|
|
|
|
|
|
|
|
use super::environment::*;
|
|
|
|
|
|
|
|
lazy_static!{
|
|
|
|
static ref BACKUP_ROUTER: Router = super::backup_api();
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct BackupService {
|
|
|
|
rpcenv: BackupEnvironment,
|
|
|
|
worker: Arc<WorkerTask>,
|
2019-05-29 07:35:21 +00:00
|
|
|
debug: bool,
|
2019-05-08 15:36:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl BackupService {
|
|
|
|
|
2019-05-29 07:35:21 +00:00
|
|
|
pub fn new(rpcenv: BackupEnvironment, worker: Arc<WorkerTask>, debug: bool) -> Self {
|
|
|
|
Self { rpcenv, worker, debug }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn debug<S: AsRef<str>>(&self, msg: S) {
|
|
|
|
if self.debug { self.worker.log(msg); }
|
2019-05-08 15:36:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn handle_request(&self, req: Request<Body>) -> BoxFut {
|
|
|
|
|
|
|
|
let (parts, body) = req.into_parts();
|
|
|
|
|
|
|
|
let method = parts.method.clone();
|
|
|
|
|
|
|
|
let (path, components) = match tools::normalize_uri_path(parts.uri.path()) {
|
|
|
|
Ok((p,c)) => (p, c),
|
|
|
|
Err(err) => return Box::new(future::err(http_err!(BAD_REQUEST, err.to_string()))),
|
|
|
|
};
|
|
|
|
|
2019-05-29 08:17:38 +00:00
|
|
|
self.debug(format!("{} {}", method, path));
|
2019-05-08 15:36:19 +00:00
|
|
|
|
|
|
|
let mut uri_param = HashMap::new();
|
|
|
|
|
|
|
|
match BACKUP_ROUTER.find_method(&components, method, &mut uri_param) {
|
|
|
|
MethodDefinition::None => {
|
|
|
|
let err = http_err!(NOT_FOUND, "Path not found.".to_string());
|
2019-05-09 16:01:24 +00:00
|
|
|
return Box::new(future::ok((self.rpcenv.formatter.format_error)(err)));
|
2019-05-08 15:36:19 +00:00
|
|
|
}
|
|
|
|
MethodDefinition::Simple(api_method) => {
|
2019-05-09 16:01:24 +00:00
|
|
|
return crate::server::rest::handle_sync_api_request(
|
|
|
|
self.rpcenv.clone(), api_method, self.rpcenv.formatter, parts, body, uri_param);
|
2019-05-08 15:36:19 +00:00
|
|
|
}
|
|
|
|
MethodDefinition::Async(async_method) => {
|
2019-05-09 16:01:24 +00:00
|
|
|
return crate::server::rest::handle_async_api_request(
|
|
|
|
self.rpcenv.clone(), async_method, self.rpcenv.formatter, parts, body, uri_param);
|
2019-05-08 15:36:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn log_response(worker: Arc<WorkerTask>, method: hyper::Method, path: &str, resp: &Response<Body>) {
|
|
|
|
|
|
|
|
let status = resp.status();
|
|
|
|
|
|
|
|
if !status.is_success() {
|
|
|
|
let reason = status.canonical_reason().unwrap_or("unknown reason");
|
|
|
|
|
|
|
|
let mut message = "request failed";
|
|
|
|
if let Some(data) = resp.extensions().get::<ErrorMessageExtension>() {
|
|
|
|
message = &data.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
worker.log(format!("{} {}: {} {}: {}", method.as_str(), path, status.as_str(), reason, message));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl hyper::service::Service for BackupService {
|
|
|
|
type ReqBody = Body;
|
|
|
|
type ResBody = Body;
|
2019-05-30 06:10:06 +00:00
|
|
|
type Error = Error;
|
2019-06-07 11:10:56 +00:00
|
|
|
type Future = Box<dyn Future<Item = Response<Body>, Error = Self::Error> + Send>;
|
2019-05-08 15:36:19 +00:00
|
|
|
|
|
|
|
fn call(&mut self, req: Request<Self::ReqBody>) -> Self::Future {
|
|
|
|
let path = req.uri().path().to_owned();
|
|
|
|
let method = req.method().clone();
|
|
|
|
let worker = self.worker.clone();
|
|
|
|
|
|
|
|
Box::new(self.handle_request(req).then(move |result| {
|
|
|
|
match result {
|
|
|
|
Ok(res) => {
|
|
|
|
Self::log_response(worker, method, &path, &res);
|
2019-05-30 06:10:06 +00:00
|
|
|
Ok::<_, Error>(res)
|
2019-05-08 15:36:19 +00:00
|
|
|
}
|
|
|
|
Err(err) => {
|
2019-05-30 06:10:06 +00:00
|
|
|
if let Some(apierr) = err.downcast_ref::<HttpError>() {
|
2019-05-08 15:36:19 +00:00
|
|
|
let mut resp = Response::new(Body::from(apierr.message.clone()));
|
2019-05-23 06:05:39 +00:00
|
|
|
resp.extensions_mut().insert(ErrorMessageExtension(apierr.message.clone()));
|
2019-05-08 15:36:19 +00:00
|
|
|
*resp.status_mut() = apierr.code;
|
|
|
|
Self::log_response(worker, method, &path, &resp);
|
|
|
|
Ok(resp)
|
|
|
|
} else {
|
|
|
|
let mut resp = Response::new(Body::from(err.to_string()));
|
2019-05-23 06:05:39 +00:00
|
|
|
resp.extensions_mut().insert(ErrorMessageExtension(err.to_string()));
|
2019-05-08 15:36:19 +00:00
|
|
|
*resp.status_mut() = StatusCode::BAD_REQUEST;
|
|
|
|
Self::log_response(worker, method, &path, &resp);
|
|
|
|
Ok(resp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
}
|