diff --git a/src/api2/admin/datastore/h2upload.rs b/src/api2/admin/datastore/h2upload.rs index 7d700f68..4e03b665 100644 --- a/src/api2/admin/datastore/h2upload.rs +++ b/src/api2/admin/datastore/h2upload.rs @@ -1,16 +1,21 @@ use failure::*; +use lazy_static::lazy_static; -use futures::{Future, Stream}; -use h2::server; +use std::collections::HashMap; + +use futures::*; use hyper::header::{HeaderValue, UPGRADE}; -use hyper::{Body, Response, StatusCode}; +use hyper::{Body, Request, Response, StatusCode}; use hyper::http::request::Parts; use hyper::rt; use serde_json::Value; +use crate::tools; use crate::api_schema::router::*; use crate::api_schema::*; +use crate::server::formatter::*; +use crate::server::RestEnvironment; pub fn api_method_upgrade_h2upload() -> ApiAsyncMethod { ApiAsyncMethod::new( @@ -20,12 +25,95 @@ pub fn api_method_upgrade_h2upload() -> ApiAsyncMethod { ) } +lazy_static!{ + static ref BACKUP_ROUTER: Router = backup_api(); +} + +pub struct BackupService { + rpcenv: RestEnvironment, +} + +impl BackupService { + + fn new(rpcenv: &RpcEnvironment) -> Self { + let mut rpcenv = RestEnvironment::new(rpcenv.env_type()); + rpcenv.set_user(rpcenv.get_user()); + Self { rpcenv } + } + + fn handle_request(&self, req: Request
) -> 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()))), + }; + + let formatter = &JSON_FORMATTER; + + println!("H2 REQUEST {} {}", method, path); + println!("H2 COMPO {:?}", components); + + 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()); + return Box::new(future::ok((formatter.format_error)(err))); + } + MethodDefinition::Simple(api_method) => { + return crate::server::rest::handle_sync_api_request(self.rpcenv.clone(), api_method, formatter, parts, body, uri_param); + } + MethodDefinition::Async(async_method) => { + return crate::server::rest::handle_async_api_request(self.rpcenv.clone(), async_method, formatter, parts, body, uri_param); + } + } + } +} + +impl hyper::service::Service for BackupService { + type ReqBody = Body; + type ResBody = Body; + type Error = hyper::Error; + type Future = Box