simplify formatter code
This commit is contained in:
parent
4b26195c6a
commit
c578fcd9e2
|
@ -1,17 +1,22 @@
|
|||
use failure::*;
|
||||
use serde_json::{json, Value};
|
||||
|
||||
use hyper::{Body, Response, StatusCode};
|
||||
use hyper::header;
|
||||
|
||||
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";
|
||||
|
||||
match data {
|
||||
Ok(value) => {
|
||||
let result = json!({
|
||||
"data": data
|
||||
"data": value
|
||||
});
|
||||
|
||||
// todo: set result.total result.changes
|
||||
|
@ -20,30 +25,65 @@ fn json_format_result(data: &Value) -> (Vec<u8>, &'static str) {
|
|||
|
||||
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 {
|
||||
format_result: json_format_result,
|
||||
};
|
||||
|
||||
|
||||
fn extjs_format_result(data: &Value) -> (Vec<u8>, &'static str) {
|
||||
fn extjs_format_result(data: Result<Value, Error>) -> Response<Body> {
|
||||
|
||||
let content_type = "application/json;charset=UTF-8";
|
||||
|
||||
let result = match data {
|
||||
Ok(value) => {
|
||||
let result = json!({
|
||||
"data": data,
|
||||
"data": value,
|
||||
"success": true
|
||||
});
|
||||
|
||||
// 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 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 {
|
||||
|
|
|
@ -171,36 +171,10 @@ fn handle_sync_api_request(
|
|||
|
||||
let resp = 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)?;
|
||||
|
||||
Ok(res)
|
||||
|
||||
}).then(move |result| {
|
||||
match 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()))
|
||||
}
|
||||
Ok((formatter.format_result)(result))
|
||||
});
|
||||
|
||||
Box::new(resp)
|
||||
|
|
Loading…
Reference in New Issue