diff --git a/src/api_server.rs b/src/api_server.rs index 7e5746fa..5894096c 100644 --- a/src/api_server.rs +++ b/src/api_server.rs @@ -9,8 +9,6 @@ use std::sync::Arc; use failure::*; use serde_json::{json, Value}; - - use futures::future::{self, Either}; //use tokio::prelude::*; //use tokio::timer::Delay; @@ -25,6 +23,63 @@ use hyper::service::{Service, NewService}; use hyper::rt::{Future, Stream}; use hyper::header; +pub struct RestServer { + pub api_config: Arc, +} + +impl RestServer { + + pub fn new(api_config: ApiConfig) -> Self { + Self { api_config: Arc::new(api_config) } + } +} + +impl NewService for RestServer +{ + type ReqBody = Body; + type ResBody = Body; + type Error = hyper::Error; + type InitError = hyper::Error; + type Service = ApiService; + type Future = Box + Send>; + fn new_service(&self) -> Self::Future { + Box::new(future::ok(ApiService { api_config: self.api_config.clone() })) + } +} + +pub struct ApiService { + pub api_config: Arc, +} + + +impl Service for ApiService { + type ReqBody = Body; + type ResBody = Body; + type Error = hyper::Error; + type Future = Box, Error = Self::Error> + Send>; + + fn call(&mut self, req: Request) -> Self::Future { + + Box::new(handle_request(self.api_config.clone(), req).then(|result| { + match result { + Ok(res) => Ok::<_, hyper::Error>(res), + Err(err) => { + if let Some(apierr) = err.downcast_ref::() { + let mut resp = Response::new(Body::from(apierr.message.clone())); + *resp.status_mut() = apierr.code; + Ok(resp) + } else { + let mut resp = Response::new(Body::from(err.to_string())); + *resp.status_mut() = StatusCode::BAD_REQUEST; + Ok(resp) + } + } + } + })) + } +} + + type BoxFut = Box, Error = failure::Error> + Send>; macro_rules! error_response { @@ -231,57 +286,3 @@ pub fn handle_request(api: Arc, req: Request) -> BoxFut { //Box::new(ok(Response::new(Body::from("RETURN WEB GUI\n")))) } -pub struct RestServer { - pub api_config: Arc, -} - -impl RestServer { - - pub fn new(api_config: ApiConfig) -> Self { - Self { api_config: Arc::new(api_config) } - } -} - -impl NewService for RestServer -{ - type ReqBody = Body; - type ResBody = Body; - type Error = hyper::Error; - type InitError = hyper::Error; - type Service = ApiService; - type Future = Box + Send>; - fn new_service(&self) -> Self::Future { - Box::new(future::ok(ApiService { api_config: self.api_config.clone() })) - } -} - -pub struct ApiService { - pub api_config: Arc, -} - -impl Service for ApiService { - type ReqBody = Body; - type ResBody = Body; - type Error = hyper::Error; - type Future = Box, Error = Self::Error> + Send>; - - fn call(&mut self, req: Request) -> Self::Future { - - Box::new(handle_request(self.api_config.clone(), req).then(|result| { - match result { - Ok(res) => Ok::<_, hyper::Error>(res), - Err(err) => { - if let Some(apierr) = err.downcast_ref::() { - let mut resp = Response::new(Body::from(apierr.message.clone())); - *resp.status_mut() = apierr.code; - Ok(resp) - } else { - let mut resp = Response::new(Body::from(err.to_string())); - *resp.status_mut() = StatusCode::BAD_REQUEST; - Ok(resp) - } - } - } - })) - } -}