simplify formatter code
This commit is contained in:
		@ -1,49 +1,89 @@
 | 
				
			|||||||
use failure::*;
 | 
					use failure::*;
 | 
				
			||||||
use serde_json::{json, Value};
 | 
					use serde_json::{json, Value};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					use hyper::{Body, Response, StatusCode};
 | 
				
			||||||
 | 
					use hyper::header;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
pub struct OutputFormatter {
 | 
					pub struct OutputFormatter {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    pub format_result: fn(data: &Value) -> (Vec<u8>, &'static str),
 | 
					    pub format_result: fn(data: Result<Value, Error>) -> Response<Body>,
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
fn json_format_result(data: &Value) -> (Vec<u8>, &'static str) {
 | 
					fn json_format_result(data: Result<Value, Error>) -> Response<Body> {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    let content_type = "application/json;charset=UTF-8";
 | 
					    let content_type = "application/json;charset=UTF-8";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    let result = json!({
 | 
					    match data {
 | 
				
			||||||
        "data": data
 | 
					        Ok(value) => {
 | 
				
			||||||
    });
 | 
					            let result = json!({
 | 
				
			||||||
 | 
					                "data": value
 | 
				
			||||||
 | 
					            });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // todo: set result.total result.changes
 | 
					            // todo: set result.total result.changes
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    let json_str = result.to_string();
 | 
					            let json_str = result.to_string();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    let raw = json_str.into_bytes();
 | 
					            let raw = json_str.into_bytes();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    (raw, content_type)
 | 
					            let mut response = Response::new(raw.into());
 | 
				
			||||||
 | 
					            response.headers_mut().insert(
 | 
				
			||||||
 | 
					                header::CONTENT_TYPE,
 | 
				
			||||||
 | 
					                header::HeaderValue::from_static(content_type));
 | 
				
			||||||
 | 
					            return response;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        Err(err) => {
 | 
				
			||||||
 | 
					            let mut response = Response::new(Body::from(err.to_string()));
 | 
				
			||||||
 | 
					            response.headers_mut().insert(
 | 
				
			||||||
 | 
					                header::CONTENT_TYPE,
 | 
				
			||||||
 | 
					                header::HeaderValue::from_static(content_type));
 | 
				
			||||||
 | 
					            *response.status_mut() = StatusCode::BAD_REQUEST;
 | 
				
			||||||
 | 
					            return response;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
pub static JSON_FORMATTER: OutputFormatter = OutputFormatter {
 | 
					pub static JSON_FORMATTER: OutputFormatter = OutputFormatter {
 | 
				
			||||||
    format_result: json_format_result,
 | 
					    format_result: json_format_result,
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					fn extjs_format_result(data: Result<Value, Error>) -> Response<Body> {
 | 
				
			||||||
fn extjs_format_result(data: &Value) -> (Vec<u8>, &'static str) {
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    let content_type = "application/json;charset=UTF-8";
 | 
					    let content_type = "application/json;charset=UTF-8";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    let result = json!({
 | 
					    let result = match data {
 | 
				
			||||||
        "data": data,
 | 
					        Ok(value) => {
 | 
				
			||||||
        "success": true
 | 
					            let result = json!({
 | 
				
			||||||
    });
 | 
					                "data": value,
 | 
				
			||||||
 | 
					                "success": true
 | 
				
			||||||
 | 
					            });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // todo: set result.total result.changes
 | 
					            // todo: set result.total result.changes
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            result
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        Err(err) => {
 | 
				
			||||||
 | 
					            let mut errors = vec![];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            errors.push(err.to_string());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            let result = json!({
 | 
				
			||||||
 | 
					                "errors": errors,
 | 
				
			||||||
 | 
					                "success": false
 | 
				
			||||||
 | 
					            });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            result
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    let json_str = result.to_string();
 | 
					    let json_str = result.to_string();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    let raw = json_str.into_bytes();
 | 
					    let raw = json_str.into_bytes();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    (raw, content_type)
 | 
					    let mut response = Response::new(raw.into());
 | 
				
			||||||
 | 
					    response.headers_mut().insert(
 | 
				
			||||||
 | 
					        header::CONTENT_TYPE,
 | 
				
			||||||
 | 
					        header::HeaderValue::from_static(content_type));
 | 
				
			||||||
 | 
					    response
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
pub static EXTJS_FORMATTER: OutputFormatter = OutputFormatter {
 | 
					pub static EXTJS_FORMATTER: OutputFormatter = OutputFormatter {
 | 
				
			||||||
 | 
				
			|||||||
@ -171,36 +171,10 @@ fn handle_sync_api_request(
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    let resp = params
 | 
					    let resp = params
 | 
				
			||||||
        .and_then(move |params| {
 | 
					        .and_then(move |params| {
 | 
				
			||||||
 | 
					 | 
				
			||||||
            println!("GOT PARAMS {}", params);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            /*
 | 
					 | 
				
			||||||
            let when = Instant::now() + Duration::from_millis(3000);
 | 
					 | 
				
			||||||
            let task = Delay::new(when).then(|_| {
 | 
					 | 
				
			||||||
                println!("A LAZY TASK");
 | 
					 | 
				
			||||||
                ok(())
 | 
					 | 
				
			||||||
            });
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            tokio::spawn(task);
 | 
					 | 
				
			||||||
             */
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            let res = (info.handler)(params, info)?;
 | 
					            let res = (info.handler)(params, info)?;
 | 
				
			||||||
 | 
					 | 
				
			||||||
            Ok(res)
 | 
					            Ok(res)
 | 
				
			||||||
 | 
					 | 
				
			||||||
        }).then(move |result| {
 | 
					        }).then(move |result| {
 | 
				
			||||||
            match result {
 | 
					            Ok((formatter.format_result)(result))
 | 
				
			||||||
                Ok(ref value) => {
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
                    let (raw, content_type) = (formatter.format_result)(value);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
                    Ok(Response::builder()
 | 
					 | 
				
			||||||
                       .status(StatusCode::OK)
 | 
					 | 
				
			||||||
                       .header(header::CONTENT_TYPE, content_type)
 | 
					 | 
				
			||||||
                       .body(Body::from(raw))?)
 | 
					 | 
				
			||||||
                }
 | 
					 | 
				
			||||||
                Err(err) => Err(http_err!(BAD_REQUEST, err.to_string()))
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
        });
 | 
					        });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    Box::new(resp)
 | 
					    Box::new(resp)
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user